import {
    type TextStyle,
    type ViewStyle,
    StyleSheet
} from "react-native";
import {
    type ButtonVariantConstantType,
    type ButtonTypeConstantType,
    type ButtonSizeConstantType,
    type ButtonDynamicStyleType,
    type ButtonMeasuresKeys,
    type ButtonMeasures,
    type ButtonVariants,
    type ButtonVariant,
    type ButtonTypes,
    type ButtonSize,
    type ButtonType
} from "./type";
import type {
    Mutable
} from "../../types";

export const BUTTON_SIZES: Record<ButtonSize, ButtonMeasuresKeys> = {
    large: {
        paddingHorizontal: "spacingXl",
        paddingVertical: "spacingLg",
        fontSize: "labelLargeSize"
    },
    medium: {
        paddingHorizontal: "spacingLg",
        paddingVertical: "spacingMd",
        fontSize: "labelMediumSize"
    },
    small: {
        paddingHorizontal: "spacingMd",
        paddingVertical: "spacingSm",
        fontSize: "labelSmallSize"
    }
};

export const BUTTON_VARIANT_STYLES: Record<
    ButtonVariant,
    {
        titleColor: "type" | keyof NCoreUIKit.TextContentColors;
        iconColor: "type" | keyof NCoreUIKit.ColorsMerged;
        borderColor: "transparent" | "type" | "container";
        iconColorScheme?: keyof NCoreUIKit.ThemeTokens;
        containerColor: "transparent" | "type";
    }
> = {
    ghost: {
        containerColor: "transparent",
        borderColor: "transparent",
        titleColor: "type",
        iconColor: "type"
    },
    outline: {
        containerColor: "transparent",
        borderColor: "type",
        titleColor: "type",
        iconColor: "type"
    },
    filled: {
        borderColor: "container",
        containerColor: "type",
        titleColor: "type",
        iconColor: "type"
    }
};

export const BUTTON_TYPE_STYLES: Record<
    ButtonType,
    {
        containerColor: keyof NCoreUIKit.ContainerContentColors;
        borderColor: keyof NCoreUIKit.BorderContentColors;
        titleColor: keyof NCoreUIKit.TextContentColors;
        iconColor: keyof NCoreUIKit.IconContentColors;
    }
> = {
    primary: {
        containerColor: "primary",
        borderColor: "emphasized",
        titleColor: "onPrimary",
        iconColor: "onPrimary"
    },
    success: {
        containerColor: "success",
        borderColor: "success",
        titleColor: "success",
        iconColor: "success"
    },
    warning: {
        containerColor: "warning",
        borderColor: "warning",
        titleColor: "warning",
        iconColor: "warning"
    },
    danger: {
        containerColor: "danger",
        borderColor: "danger",
        titleColor: "danger",
        iconColor: "danger"
    },
    neutral: {
        containerColor: "subtle",
        borderColor: "subtle",
        titleColor: "mid",
        iconColor: "mid"
    },
    info: {
        containerColor: "info",
        borderColor: "info",
        titleColor: "info",
        iconColor: "info"
    }
};

export const getButtonType = ({
    type
}: ButtonTypeConstantType): ButtonTypes => {
    const currentType = BUTTON_TYPE_STYLES[type];

    return currentType;
};

export const getButtonVariant = ({
    variant
}: ButtonVariantConstantType): ButtonVariants => {
    const currentVariant = BUTTON_VARIANT_STYLES[variant];

    return currentVariant;
};

export const getButtonSize = ({
    spaces,
    size
}: ButtonSizeConstantType): ButtonMeasures => {
    const currentSize = BUTTON_SIZES[size];

    return {
        paddingHorizontal: spaces[currentSize.paddingHorizontal],
        paddingVertical: spaces[currentSize.paddingVertical],
        fontSize: currentSize.fontSize
    };
};

const stylesheet = StyleSheet.create({
    container: {
        backgroundColor: "transparent",
        borderColor: "transparent",
        flexDirection: "row",
        borderStyle: "solid",
        alignItems: "center",
        position: "relative",
        display: "flex"
    },
    overlay: {
        position: "absolute",
        display: "none",
        bottom: 0,
        right: 0,
        left: 0,
        top: 0
    },
    title: {
        textAlign: "center",
        margin: 0
    },
    loading: {}
});

export const useStyles = ({
    verticalLocationForStretchFix,
    displayBehaviourWhileLoading,
    isFixVerticalStretch,
    customBorderColor,
    isCustomPadding,
    spreadBehaviour,
    currentVariant,
    iconDirection,
    currentSize,
    currentType,
    customColor,
    isDisabled,
    isLoading,
    radiuses,
    borders,
    variant,
    colors,
    spaces,
    title,
    icon,
    type
}: ButtonDynamicStyleType) => {
    const styleType = type === "danger" ? "error" : type;

    const styles = {
        container: {
            alignSelf: isFixVerticalStretch ? verticalLocationForStretchFix === "center" ? verticalLocationForStretchFix : `flex-${verticalLocationForStretchFix}` : "auto",
            paddingRight: currentSize.paddingHorizontal,
            paddingBottom: currentSize.paddingVertical,
            paddingLeft: currentSize.paddingHorizontal,
            paddingTop: currentSize.paddingVertical,
            borderRadius: radiuses.actions,
            borderWidth: borders.line
        } as Mutable<ViewStyle>,
        overlay: {
            borderRadius: radiuses.actions - 2
        } as Mutable<ViewStyle>,
        title: {
        } as Mutable<TextStyle>,
        loading: {
        } as Mutable<ViewStyle>
    };

    if (currentVariant.containerColor === "type") {
        styles.container.backgroundColor = colors.content.container[currentType.containerColor];
    } else {
        styles.container.backgroundColor = "transparent";
    }

    if (currentVariant.borderColor === "type") {
        styles.container.borderColor = colors.content.border[currentType.borderColor];
    } else if (currentVariant.borderColor === "container") {
        styles.container.borderColor = styles.container.backgroundColor;
    } else {
        styles.container.borderColor = "transparent";
    }

    if (isLoading) {
        styles.title.marginLeft = spaces.spacingSm;

        styles.overlay.display = "flex";

        if (displayBehaviourWhileLoading === "disabled") {
            if (variant !== "ghost") {
                styles.container.borderColor = colors.system.state.overlay.disabled[styleType];

                if (variant === "filled") {
                    styles.overlay.backgroundColor = colors.system.state.overlay.disabled[styleType];
                }
            }
        }
    }

    if (isLoading && spreadBehaviour === "stretch") {
        styles.title.marginLeft = spaces.spacingSm;
        styles.title.margin = undefined;
    }

    if (icon && !isLoading) {
        styles.title.marginLeft = spaces.spacingSm;
        styles.title.margin = undefined;
    }

    if (spreadBehaviour === "baseline") {
        styles.container.alignSelf = spreadBehaviour;
        styles.container.width = "auto";
    } else if (spreadBehaviour === "stretch") {
        styles.container.alignSelf = spreadBehaviour;
        styles.container.justifyContent = "center";
        styles.container.flexShrink = 1;
        styles.container.width = "100%";
    }

    if (isDisabled) {
        if (variant !== "ghost") {
            styles.container.borderColor = colors.system.state.overlay.disabled[styleType];

            if (variant === "filled") {
                styles.overlay.backgroundColor = colors.system.state.overlay.disabled[styleType];
            }
        }
    }
    if (icon && title) {
        if (iconDirection === "left") {
            styles.title.margin = undefined;
            styles.title.marginLeft = spaces.spacingSm;
        } else {
            styles.title.margin = undefined;
            styles.title.marginRight = spaces.spacingSm;
        }
    }

    if(icon && !title) {
        styles.container.paddingBottom = currentSize.paddingVertical;
        styles.container.paddingRight = currentSize.paddingVertical;
        styles.container.paddingLeft = currentSize.paddingVertical;
        styles.container.paddingTop = currentSize.paddingVertical;
    }

    if(isCustomPadding) {
        delete styles.container.paddingBottom;
        delete styles.container.paddingRight;
        delete styles.container.paddingLeft;
        delete styles.container.paddingTop;
    }

    if(customColor) {
        const _customContainerColor = customColor as keyof NCoreUIKit.ContainerContentColors;
        const _customColor = customColor as keyof NCoreUIKit.ProjectColorPalette;

        styles.container.backgroundColor = colors.project[_customColor] ? colors.project[_customColor] : colors.content.container[_customContainerColor] ? colors.content.container[_customContainerColor] : customColor;
    }

    if(customBorderColor) {
        const _customContentBorderColor = customBorderColor as keyof NCoreUIKit.BorderContentColors;
        const _customBorderColor = customBorderColor as keyof NCoreUIKit.ProjectColorPalette;

        styles.container.borderColor = colors.project[_customBorderColor] ? colors.project[_customBorderColor] : colors.content.border[_customContentBorderColor] ? colors.content.border[_customContentBorderColor] : customBorderColor;
    }

    return styles;
};
export default stylesheet;
