import {
    type TextStyle,
    type ViewStyle,
    StyleSheet
} from "react-native";
import {
    type SwitchDynamicStyleType,
    type SwitchTypeConstantType,
    type SwitchTypes,
    type SwitchType
} from "./type";
import type {
    Mutable
} from "../../types";

export const SWITCH_TYPE_STYLES: Record<
    SwitchType,
    {
        containerColor: keyof NCoreUIKit.ContainerContentColors;
        indicatorColor: keyof NCoreUIKit.IconContentColors;
        subTitleColor: keyof NCoreUIKit.TextContentColors;
        titleColor: keyof NCoreUIKit.TextContentColors;
    }
> = {
    primary: {
        indicatorColor: "default",
        containerColor: "mid",
        subTitleColor: "low",
        titleColor: "mid"
    },
    danger: {
        subTitleColor: "dangerLow",
        indicatorColor: "danger",
        containerColor: "danger",
        titleColor: "danger"
    },
    success: {
        subTitleColor: "successLow",
        indicatorColor: "success",
        containerColor: "success",
        titleColor: "success"
    },
    warning: {
        subTitleColor: "warningLow",
        indicatorColor: "warning",
        containerColor: "warning",
        titleColor: "warning"
    },
    info: {
        subTitleColor: "infoLow",
        indicatorColor: "info",
        containerColor: "info",
        titleColor: "info"
    },
    neutral: {
        indicatorColor: "default",
        containerColor: "mid",
        subTitleColor: "low",
        titleColor: "mid"
    }
};

export const getSwitchType = ({
    type
}: SwitchTypeConstantType): SwitchTypes => {
    const currentType = SWITCH_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"
    },
    indicator: {

    },
    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: 0,
        right: 0,
        left: 0,
        top: 0
    }
});

export const useStyles = ({
    displayBehaviourWhileLoading,
    SWITCH_INDICATOR_SIZE,
    spreadBehaviour,
    inlineSpaces,
    currentType,
    isDisabled,
    isLoading,
    isActive,
    colors,
    isFlip,
    spaces,
    type
}: SwitchDynamicStyleType) => {
    const styleType = type === "danger" ? "error" : type;

    const styles = {
        container: {
            padding: spaces.spacingSm
        } as Mutable<ViewStyle>,
        indicatorContainer: {
            backgroundColor: colors.content.container[currentType.containerColor],
            borderRadius: (SWITCH_INDICATOR_SIZE + (spaces.spacingSm * 2)) / 2,
            width: (SWITCH_INDICATOR_SIZE * 1.5) + (spaces.spacingSm * 2),
            padding: spaces.spacingXs
        } as Mutable<ViewStyle>,
        indicator: {
            backgroundColor: colors.content.icon[currentType.indicatorColor],
            borderRadius: SWITCH_INDICATOR_SIZE / 2,
            height: SWITCH_INDICATOR_SIZE,
            width: SWITCH_INDICATOR_SIZE
        } 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: (SWITCH_INDICATOR_SIZE + (spaces.spacingSm * 2)) / 2
        } as Mutable<ViewStyle>,
        optionalText: {
            marginLeft: inlineSpaces.subTitle
        } as Mutable<TextStyle>
    };

    if (isLoading) {
        if (displayBehaviourWhileLoading === "disabled") {
            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(isActive) {
        if(styleType === "neutral") {
            styles.indicatorContainer.backgroundColor = colors.content.container.emphasized;
            styles.indicator.backgroundColor = colors.content.icon.emphasized;
        }
    }

    if (isDisabled) {
        if(styleType === "neutral" && isActive) {
            styles.indicatorContainer.backgroundColor = colors.system.state.border.disabled.primary;
        }

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

    return styles;
};
export default stylesheet;
