import {
    type FC
} from "react";
import {
    TouchableOpacity,
    View
} from "react-native";
import type IRadioButtonProps from "./type";
import stylesheet, {
    getRadioButtonType,
    useStyles
} from "./stylesheet";
import {
    NCoreUIKitLocalize,
    NCoreUIKitTheme
} from "../../core/hooks";
import type ITextProps from "../text/type";
import Loading from "../loading";
import Text from "../text";

const RadioButton: FC<IRadioButtonProps> = ({
    displayBehaviourWhileLoading = "disabled",
    spreadBehaviour = "baseline",
    isOptional = false,
    isDisabled = false,
    type = "neutral",
    isFlip = false,
    customLocalize,
    subTitleStyle,
    optionalText,
    customTheme,
    titleStyle,
    isChecked,
    isLoading,
    subTitle,
    onPress,
    title,
    style,
    ...props
}) => {
    const {
        inlineSpaces,
        radiuses,
        borders,
        colors,
        spaces
    } = NCoreUIKitTheme.useContext(customTheme);

    const {
        localize
    } = NCoreUIKitLocalize.useContext(customLocalize);

    const currentType = getRadioButtonType({
        type
    });

    const RADIO_INDICATOR_SIZE = 16;

    const {
        indicatorContainer: indicatorContainerDynamicStyle,
        contentContainer: contentContainerDynamicStyle,
        titleContainer: titleContainerDynamicStyle,
        optionalText: optionalTextDynamicStyle,
        container: containerDynamicStyle,
        indicator: indicatorDynamicStyle,
        subTitle: subTitleDynamicStyle,
        loading: loadingDynamicStyle,
        overlay: overlayDynamicStyle,
        title: titleDynamicStyle
    } = useStyles({
        displayBehaviourWhileLoading,
        RADIO_INDICATOR_SIZE,
        spreadBehaviour,
        inlineSpaces,
        currentType,
        isDisabled,
        isChecked,
        isLoading,
        radiuses,
        borders,
        isFlip,
        colors,
        spaces,
        type
    });

    const titleProps: ITextProps = {
        color: currentType.titleColor,
    };

    const subTitleProps: ITextProps = {
        color: currentType.subTitleColor,
    };

    const indicatorIconProps: {
        color: keyof NCoreUIKit.IconContentColors;
        customColor?: string;
    } = {
        color: currentType.indicatorColor
    };

    if (isDisabled || isLoading) {
        const stateType = type === "danger" ? "error" : type;

        subTitleProps.customColor = colors.system.state.content.disabled[stateType];
        titleProps.customColor = colors.system.state.content.disabled[stateType];

        if(isChecked && stateType === "neutral") {
            indicatorIconProps.customColor = colors.system.state.border.disabled.primary;
        } else {
            indicatorIconProps.customColor = colors.system.state.border.disabled[stateType];
        }
    }

    const renderOptionalText = () => {
        if(!isOptional && !optionalText) {
            return null;
        }

        return <Text
            variant="labelLargeSize"
            color={titleProps.color}
            style={[
                optionalTextDynamicStyle
            ]}
            {...titleProps}
        >
            ( {isOptional ? localize("is-optional") : optionalText} )
        </Text>;
    };

    const renderTitle = () => {
        if (!title) {
            return null;
        }

        return <View
            style={[
                stylesheet.titleContainer,
                titleContainerDynamicStyle
            ]}
        >
            <Text
                variant="bodyMediumSize"
                style={[
                    titleStyle,
                    stylesheet.title,
                    titleDynamicStyle
                ]}
                {...titleProps}
            >
                {title}
            </Text>
            {renderOptionalText()}
        </View>;
    };

    const renderIndicator = () => {
        if(!isChecked) {
            return <View
                style={[
                    indicatorDynamicStyle,
                    {
                        backgroundColor: "transparent"
                    }
                ]}
            />;
        }

        return <View
            style={[
                stylesheet.indicator,
                indicatorDynamicStyle
            ]}
        />;
    };

    const renderIndicatorContainer = () => {
        if (isLoading) {
            return <Loading
                style={[
                    loadingDynamicStyle
                ]}
            />;
        }

        return <View
            style={[
                stylesheet.indicatorContainer,
                indicatorContainerDynamicStyle
            ]}
        >
            {renderIndicator()}

            {renderOverlay()}
        </View>;
    };

    const renderOverlay = () => {
        return <View
            style={[
                stylesheet.overlay,
                overlayDynamicStyle
            ]}
        />;
    };

    const renderSubtitle = () => {
        if(!subTitle) {
            return null;
        }

        return <Text
            {...subTitleProps}
            style={[
                subTitleStyle,
                stylesheet.subTitle,
                subTitleDynamicStyle
            ]}
        >
            {subTitle}
        </Text>;
    };

    const renderContent = () => {
        if(!title) {
            return null;
        }

        return <View
            style={[
                stylesheet.contentContainer,
                contentContainerDynamicStyle
            ]}
        >
            {renderTitle()}
            {renderSubtitle()}
        </View>;
    };

    return (
        <TouchableOpacity
            {...props}
            onPress={isDisabled || isLoading ? () => null : onPress ? onPress : undefined}
            disabled={isDisabled || isLoading || !onPress}
            style={[
                style,
                stylesheet.container,
                containerDynamicStyle
            ]}
        >
            {isFlip ? null : renderIndicatorContainer()}

            {renderContent()}

            {isFlip ? renderIndicatorContainer() : null}
        </TouchableOpacity>
    );
};
export default RadioButton;
