import {
    type TextStyle,
    type ViewStyle,
    StyleSheet
} from "react-native";
import {
    type CheckBoxDynamicStyleType,
    type CheckBoxTypeConstantType,
    type CheckBoxTypes,
    type CheckBoxType
} from "./type";
import type {
    Mutable
} from "../../types";

export const CHECK_BOX_TYPE_STYLES: Record<
    CheckBoxType,
    {
        containerColor: keyof NCoreUIKit.ContainerContentColors;
        indicatorColor: keyof NCoreUIKit.BorderContentColors;
        subTitleColor: keyof NCoreUIKit.TextContentColors;
        borderColor: keyof NCoreUIKit.BorderContentColors;
        titleColor: keyof NCoreUIKit.TextContentColors;
    }
> = {
    primary: {
        indicatorColor: "emphasized",
        borderColor: "emphasized",
        containerColor: "mid",
        subTitleColor: "low",
        titleColor: "mid"
    },
    danger: {
        subTitleColor: "dangerLow",
        indicatorColor: "danger",
        containerColor: "danger",
        borderColor: "danger",
        titleColor: "danger"
    },
    success: {
        subTitleColor: "successLow",
        indicatorColor: "success",
        containerColor: "success",
        borderColor: "success",
        titleColor: "success"
    },
    warning: {
        subTitleColor: "warningLow",
        indicatorColor: "warning",
        containerColor: "warning",
        borderColor: "warning",
        titleColor: "warning"
    },
    info: {
        subTitleColor: "infoLow",
        indicatorColor: "info",
        containerColor: "info",
        borderColor: "info",
        titleColor: "info"
    },
    neutral: {
        indicatorColor: "emphasized",
        containerColor: "mid",
        borderColor: "subtle",
        subTitleColor: "low",
        titleColor: "mid"
    }
};

export const getCheckBoxType = ({
    type
}: CheckBoxTypeConstantType): CheckBoxTypes => {
    const currentType = CHECK_BOX_TYPE_STYLES[type];

    return currentType;
};

const stylesheet = StyleSheet.create({
    container: {
        backgroundColor: "transparent",
        borderColor: "transparent",
        flexDirection: "row",
        borderStyle: "solid",
        alignItems: "center",
        display: "flex"
    },
    indicatorContainer: {
        position: "relative"
    },
    contentContainer: {
        justifyContent: "center",
        flexDirection: "column"
    },
    titleContainer: {
        justifyContent: "flex-start",
        flexDirection: "row",
        alignItems: "center"
    },
    title: {
        textAlign: "left",
        margin: 0
    },
    subTitle: {
        textAlign: "left"
    },
    loading: {},
    overlay: {
        position: "absolute",
        display: "none",
        zIndex: 999,
        bottom: -2,
        right: -2,
        left: -2,
        top: -2
    }
});

export const useStyles = ({
    displayBehaviourWhileLoading,
    spreadBehaviour,
    inlineSpaces,
    currentType,
    isDisabled,
    isLoading,
    isChecked,
    radiuses,
    borders,
    colors,
    isFlip,
    spaces,
    type
}: CheckBoxDynamicStyleType) => {
    const styleType = type === "danger" ? "error" : type;

    const styles = {
        container: {
            padding: spaces.spacingSm
        } as Mutable<ViewStyle>,
        indicatorContainer: {
            backgroundColor: colors.content.container[currentType.containerColor],
            borderColor: colors.content.border[currentType.borderColor],
            borderRadius: radiuses.actions,
            borderWidth: borders.line,
            padding: spaces.spacingXs
        } as Mutable<ViewStyle>,
        contentContainer: {
            marginLeft: spaces.spacingSm
        } as Mutable<ViewStyle>,
        titleContainer: {

        } as Mutable<ViewStyle>,
        title: {

        } as Mutable<TextStyle>,
        subTitle: {

        } as Mutable<TextStyle>,
        loading: {
        } as Mutable<ViewStyle>,
        overlay: {
            borderRadius: radiuses.actions
        } as Mutable<ViewStyle>,
        optionalText: {
            marginLeft: inlineSpaces.subTitle
        } as Mutable<TextStyle>
    };

    if(styleType === "neutral") {
        styles.indicatorContainer.borderColor = colors.content.border.default;
    }

    if (isLoading) {
        if (displayBehaviourWhileLoading === "disabled") {
            styles.indicatorContainer.borderColor = colors.system.state.overlay.disabled[styleType];

            styles.overlay.backgroundColor = colors.system.state.overlay.disabled[styleType];
            styles.overlay.display = "flex";
        }
    }

    if (isFlip) {
        styles.contentContainer.marginRight = spaces.spacingSm;
        styles.titleContainer.justifyContent = "flex-end";
        styles.contentContainer.marginLeft = 0;
        styles.subTitle.textAlign = "right";
        styles.title.textAlign = "right";
    }

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

    if(isChecked) {
        if(styleType === "neutral") {
            styles.indicatorContainer.backgroundColor = colors.content.container.selected;
            styles.indicatorContainer.borderColor = colors.content.border.emphasized;
        }
    }

    if (isDisabled) {
        if(styleType === "neutral" && isChecked) {
            styles.indicatorContainer.backgroundColor = colors.system.state.border.disabled.primary;
            styles.indicatorContainer.borderColor = colors.system.state.border.disabled.primary;
        } else {
            styles.indicatorContainer.borderColor = colors.system.state.border.disabled[styleType];
        }

        styles.overlay.backgroundColor = colors.system.state.overlay.disabled[styleType];
        styles.overlay.display = "flex";
    }

    return styles;
};
export default stylesheet;
