import {
    useLayoutEffect,
    useState
} from "react";
import {
    View
} from "react-native";
import stylesheet from "./stylesheet";
import {
    NCoreUIKitLocalize,
    NCoreUIKitTheme,
    PageContainer,
    RadioButton,
    CodeViewer,
    SelectBox,
    TextInput,
    Button,
    Header,
    Switch,
    Text
} from "ncore-ui-kit";
import {
    useNavigation
} from "@react-navigation/native";
import type RootStackParamList from "../../../navigation/type";
import type {
    NativeStackNavigationProp
} from "@react-navigation/native-stack";
import type {
    IRadioButtonProps
} from "ncore-ui-kit";

const TYPES: Array<IRadioButtonProps["type"]> = [
    "primary",
    "danger",
    "warning",
    "success",
    "info"
];
const SPREAD_BEHAVIOURS: Array<IRadioButtonProps["spreadBehaviour"]> = [
    "baseline",
    "stretch",
    "free"
];

const TYPES_DATA = TYPES.map((item) => ({
    __title: item as string,
    __key: item as string
}));

const RadioButtonPage = () => {
    const {
        radiuses,
        borders,
        spaces,
        colors
    } = NCoreUIKitTheme.useContext();

    const {
        localize
    } = NCoreUIKitLocalize.useContext();

    const navigation = useNavigation<NativeStackNavigationProp<RootStackParamList>>();

    const [
        typeIndex,
        setTypeIndex
    ] = useState<number>(0);

    const [
        spreadIndex,
        setSpreadIndex
    ] = useState<number>(0);

    const [
        title,
        setTitle
    ] = useState<string>("");

    const [
        isDisabled,
        setIsDisabled
    ] = useState<boolean>(false);

    const [
        isLoading,
        setIsLoading
    ] = useState<boolean>(false);

    const [
        isSelected,
        setIsSelected
    ] = useState<boolean>(false);

    useLayoutEffect(() => {
        navigation.setOptions({
            header: () => <Header
                title={localize("radioButton-title")}
                isWrapSafeareaContext={false}
                navigation={navigation}
                isGoBackEnable={true}
            />,
            headerShown: true
        });
    }, []);

    return <PageContainer
        scrollViewProps={{
            contentContainerStyle: stylesheet.scrollContent
        }}
        isWorkWithHeaderSpace={false}
        scrollViewStyle={[
            stylesheet.container
        ]}
        isScrollable={true}
    >
        <View
            style={[
                stylesheet.contentContainer
            ]}
        >
            <Text
                style={stylesheet.descText}
                variant="bodyMediumSize"
                color="mid"
            >
                {localize("radioButton-desc")}
            </Text>
            <View
                style={[
                    stylesheet.previewContainer,
                    {
                        borderColor: colors.content.border.subtle,
                        marginBottom: spaces.spacingMd,
                        borderRadius: radiuses.form,
                        borderWidth: borders.line,
                        padding: spaces.spacingMd
                    }
                ]}
            >
                <RadioButton
                    title={
                        title || localize("radioButton-exampleTitle")
                    }
                    spreadBehaviour={SPREAD_BEHAVIOURS[spreadIndex]}
                    onPress={() => setIsSelected(!isSelected)}
                    type={TYPES[typeIndex]}
                    isDisabled={isDisabled}
                    isChecked={isSelected}
                    isLoading={isLoading}
                />
            </View>
            <View
                style={{
                    marginBottom: spaces.spacingMd
                }}
            >
                <Text
                    style={{
                        marginBottom: spaces.spacingSm
                    }}
                    variant="headlineSmallSize"
                >
                    {localize("usage")}
                </Text>
                <CodeViewer
                    code={"import {\n    RadioButton\n} from \"ncore-ui-kit\";\n\n<RadioButton\n    title=\"Example Title\"\n    isChecked={false}\n    onPress={() => {}}\n/>"}
                    language="tsx"
                />
            </View>
            <TextInput
                placeholder={localize("radioButton-enterTitle")}
                title={localize("radioButton-titleLabel")}
                style={{
                    marginBottom: spaces.spacingMd
                }}
                spreadBehaviour="stretch"
                onChangeText={setTitle}
                value={title}
            />
            <View
                style={[
                    stylesheet.rowContainer,
                    {
                        gap: spaces.spacingMd
                    }
                ]}
            >
                <SelectBox
                    onChange={(selectedItems) => {
                        if (selectedItems.length > 0) {
                            const index = TYPES.indexOf(
                                    selectedItems[0]!
                                        .__key as unknown as IRadioButtonProps["type"],
                            );
                            if (index !== -1) setTypeIndex(index);
                        }
                    }}
                    title={localize("radioButton-changeType")}
                    titleExtractor={(item) => item.__title}
                    keyExtractor={(item) => item.__key}
                    initialSelectedItems={

                        TYPES_DATA[typeIndex]

                            ? [
                                TYPES_DATA[typeIndex]!
                            ]

                            : []

                    }
                    spreadBehaviour="free"
                    data={TYPES_DATA}
                />
                <Button
                    title={
                        `${localize("radioButton-changeSpread")}: ${SPREAD_BEHAVIOURS[spreadIndex]}`
                    }
                    onPress={() => {
                        setSpreadIndex((prev) =>
                            prev + 1 > SPREAD_BEHAVIOURS.length - 1
                                ? 0
                                : prev + 1,
                        );
                    }}
                    spreadBehaviour="free"
                />
                <View
                    style={stylesheet.switchesContainer}
                >
                    <Switch
                        onPress={() => setIsDisabled(!isDisabled)}
                        title={localize(
                            "radioButton-toggleDisabled",
                        )}
                        spreadBehaviour="free"
                        isActive={isDisabled}
                    />
                    <Switch
                        title={localize("radioButton-toggleLoading")}
                        onPress={() => setIsLoading(!isLoading)}
                        spreadBehaviour="free"
                        isActive={isLoading}
                    />
                    <Switch
                        onPress={() => setIsSelected(!isSelected)}
                        title={localize(
                            "radioButton-toggleSelected",
                        )}
                        spreadBehaviour="free"
                        isActive={isSelected}
                    />
                </View>
            </View>
        </View>
    </PageContainer>;
};
export default RadioButtonPage;
