import {
    useLayoutEffect,
    useState
} from "react";
import {
    View
} from "react-native";
import stylesheet from "./stylesheet";
import {
    NCoreUIKitLocalize,
    NCoreUIKitTheme,
    PageContainer,
    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 {
    ISelectBoxProps
} from "ncore-ui-kit";

const TYPES: Array<ISelectBoxProps<unknown>["type"]> = [
    "default",
    "danger",
    "warning",
    "question",
    "success",
    "info"
];
const SPREAD_BEHAVIOURS: Array<ISelectBoxProps<unknown>["spreadBehaviour"]> = [
    "baseline",
    "stretch",
    "free"
];

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

const SelectBoxPage = () => {
    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 [
        subTitle,
        setSubTitle
    ] = useState<string>("");

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

    const [
        isSearchable,
        setIsSearchable
    ] = useState<boolean>(false);

    const [
        isCleanEnabled,
        setIsCleanEnabled
    ] = useState<boolean>(true);

    const [
        isMultiSelect,
        setIsMultiSelect
    ] = useState<boolean>(false);

    const [
        isOptional,
        setIsOptional
    ] = useState<boolean>(false);

    useLayoutEffect(() => {
        navigation.setOptions({
            header: () => <Header
                title={localize("selectBox-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("selectBox-desc")}
            </Text>
            <View
                style={[
                    stylesheet.previewContainer,
                    {
                        borderColor: colors.content.border.subtle,
                        marginBottom: spaces.spacingMd,
                        borderRadius: radiuses.form,
                        borderWidth: borders.line,
                        padding: spaces.spacingMd
                    }
                ]}
            >
                <SelectBox
                    spreadBehaviour={SPREAD_BEHAVIOURS[spreadIndex]}
                    title={
                        title || localize("selectBox-exampleTitle")
                    }
                    placeholder={localize("selectBox-placeholder")}
                    titleExtractor={(item) => item.__title}
                    keyExtractor={(item) => item.__key}
                    maxChoice={isMultiSelect ? 3 : 1}
                    isCleanEnabled={isCleanEnabled}
                    data={[
                        {
                            __title: "Option 1",
                            __key: "1"
                        },
                        {
                            __title: "Option 2",
                            __key: "2"
                        },
                        {
                            __title: "Option 3",
                            __key: "3"
                        },
                        {
                            __title: "Option 4",
                            __key: "4"
                        }
                    ]}
                    isSearchable={isSearchable}
                    type={TYPES[typeIndex]}
                    isDisabled={isDisabled}
                    isOptional={isOptional}
                    subTitle={subTitle}
                />
            </View>
            <View
                style={{
                    marginBottom: spaces.spacingMd
                }}
            >
                <Text
                    style={{
                        marginBottom: spaces.spacingSm
                    }}
                    variant="headlineSmallSize"
                >
                    {localize("usage")}
                </Text>
                <CodeViewer
                    code={"import {\n    SelectBox\n} from \"ncore-ui-kit\";\n\n<SelectBox\n    title=\"SelectBox\"\n    data={[{__title: \"A\", __key: \"1\"}]}\n    titleExtractor={(item) => item.__title}\n    keyExtractor={(item) => item.__key}\n/>"}
                    language="tsx"
                />
            </View>
            <TextInput
                placeholder={localize("selectBox-enterTitle")}
                title={localize("selectBox-titleLabel")}
                style={{
                    marginBottom: spaces.spacingMd
                }}
                spreadBehaviour="stretch"
                onChangeText={setTitle}
                value={title}
            />
            <TextInput
                placeholder={localize("selectBox-enterSubTitle")}
                title={localize("selectBox-subTitleLabel")}
                style={{
                    marginBottom: spaces.spacingMd
                }}
                onChangeText={setSubTitle}
                spreadBehaviour="stretch"
                value={subTitle}
            />
            <View
                style={[
                    stylesheet.rowContainer,
                    {
                        gap: spaces.spacingMd
                    }
                ]}
            >
                <SelectBox
                    onChange={(selectedItems) => {
                        if (selectedItems.length > 0) {
                            const index = TYPES.indexOf(
                                    selectedItems[0]!
                                        .__key as unknown as ISelectBoxProps<unknown>["type"],
                            );
                            if (index !== -1) setTypeIndex(index);
                        }
                    }}
                    title={localize("selectBox-changeType")}
                    titleExtractor={(item) => item.__title}
                    keyExtractor={(item) => item.__key}
                    initialSelectedItems={

                        TYPES_DATA[typeIndex]

                            ? [
                                TYPES_DATA[typeIndex]!
                            ]

                            : []

                    }
                    spreadBehaviour="free"
                    data={TYPES_DATA}
                />
                <Button
                    title={
                        `${localize("selectBox-changeSpread")}: ${SPREAD_BEHAVIOURS[spreadIndex]}`
                    }
                    onPress={() => {
                        setSpreadIndex((prev) =>
                            prev + 1 > SPREAD_BEHAVIOURS.length - 1
                                ? 0
                                : prev + 1,
                        );
                    }}
                    spreadBehaviour="free"
                />
                <View
                    style={stylesheet.switchesContainer}
                >
                    <Switch
                        title={localize("selectBox-toggleDisabled")}
                        onPress={() => setIsDisabled(!isDisabled)}
                        spreadBehaviour="free"
                        isActive={isDisabled}
                    />
                    <Switch
                        onPress={() => setIsSearchable(!isSearchable)}
                        title={localize(
                            "selectBox-toggleSearchable",
                        )}
                        isActive={isSearchable}
                        spreadBehaviour="free"
                    />
                    <Switch
                        onPress={() => setIsCleanEnabled(!isCleanEnabled)}
                        title={localize("selectBox-toggleClean")}
                        isActive={isCleanEnabled}
                        spreadBehaviour="free"
                    />
                    <Switch
                        onPress={() => setIsMultiSelect(!isMultiSelect)}
                        title={localize(
                            "selectBox-toggleMultiSelect",
                        )}
                        isActive={isMultiSelect}
                        spreadBehaviour="free"
                    />
                    <Switch
                        title={localize("selectBox-toggleOptional")}
                        onPress={() => setIsOptional(!isOptional)}
                        spreadBehaviour="free"
                        isActive={isOptional}
                    />
                </View>
            </View>
        </View>
    </PageContainer>;
};
export default SelectBoxPage;
