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

const TYPES: Array<INumericInputProps["type"]> = [
    "default",
    "danger",
    "warning",
    "question",
    "success",
    "info"
];
const VARIANTS: Array<INumericInputProps["variant"]> = [
    "text",
    "hidden"
];
const SPREAD_BEHAVIOURS: Array<INumericInputProps["spreadBehaviour"]> = [
    "baseline",
    "stretch",
    "free"
];

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

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

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

    const {
        localize
    } = NCoreUIKitLocalize.useContext();

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

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

    const [
        variantIndex,
        setVariantIndex
    ] = useState<number>(0);

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

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

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

    const [
        showIcon,
        setShowIcon
    ] = useState<boolean>(false);

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

    const [
        isRequired,
        setIsRequired
    ] = useState<boolean>(false);

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

    const [
        val,
        setVal
    ] = useState<string>("");

    useLayoutEffect(() => {
        navigation.setOptions({
            header: () => <Header
                title={localize("numericInput-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("numericInput-desc")}
            </Text>
            <View
                style={[
                    stylesheet.previewContainer,
                    {
                        borderColor: colors.content.border.subtle,
                        marginBottom: spaces.spacingMd,
                        borderRadius: radiuses.form,
                        borderWidth: borders.line,
                        padding: spaces.spacingMd
                    }
                ]}
            >
                <NumericInput
                    spreadBehaviour={SPREAD_BEHAVIOURS[spreadIndex]}
                    icon={
                        showIcon
                            ? ({
                                color,
                                size
                            }) => <HomeIcon
                                color={colors.content.icon[color]}
                                size={size}
                            />

                            : undefined
                    }
                    title={
                        title ||
                            localize("numericInput-exampleTitle")
                    }
                    variant={VARIANTS[variantIndex]}
                    placeholder={localize(
                        "numericInput-placeholder",
                    )}
                    isCleanEnabled={isCleanEnabled}
                    type={TYPES[typeIndex]}
                    isDisabled={isDisabled}
                    isRequired={isRequired}
                    isOptional={isOptional}
                    onChangeText={setVal}
                    value={val}
                />
            </View>
            <View
                style={{
                    marginBottom: spaces.spacingMd
                }}
            >
                <Text
                    style={{
                        marginBottom: spaces.spacingSm
                    }}
                    variant="headlineSmallSize"
                >
                    {localize("usage")}
                </Text>
                <CodeViewer
                    code={"import {\n    NumericInput\n} from \"ncore-ui-kit\";\n\n<NumericInput\n    title=\"Title\"\n    placeholder=\"Placeholder\"\n    value={val}\n    onChangeText={setVal}\n/>"}
                    language="tsx"
                />
            </View>
            <TextInput
                placeholder={localize("numericInput-enterTitle")}
                title={localize("numericInput-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 INumericInputProps["type"],
                            );
                            if (index !== -1) setTypeIndex(index);
                        }
                    }}
                    title={localize("numericInput-changeType")}
                    titleExtractor={(item) => item.__title}
                    keyExtractor={(item) => item.__key}
                    initialSelectedItems={

                        TYPES_DATA[typeIndex]

                            ? [
                                TYPES_DATA[typeIndex]!
                            ]

                            : []

                    }
                    spreadBehaviour="free"
                    data={TYPES_DATA}
                />
                <SelectBox
                    onChange={(selectedItems) => {
                        if (selectedItems.length > 0) {
                            const index = VARIANTS.indexOf(
                                    selectedItems[0]!
                                        .__key as unknown as INumericInputProps["variant"],
                            );
                            if (index !== -1) setVariantIndex(index);
                        }
                    }}
                    title={localize("numericInput-changeVariant")}
                    initialSelectedItems={

                        VARIANTS_DATA[variantIndex]

                            ? [
                                VARIANTS_DATA[variantIndex]!
                            ]

                            : []

                    }
                    titleExtractor={(item) => item.__title}
                    keyExtractor={(item) => item.__key}
                    spreadBehaviour="free"
                    data={VARIANTS_DATA}
                />
                <Button
                    title={
                        `${localize("numericInput-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("numericInput-toggleIcon")}
                        onPress={() => setShowIcon(!showIcon)}
                        spreadBehaviour="free"
                        isActive={showIcon}
                    />
                    <Switch
                        onPress={() => setIsDisabled(!isDisabled)}
                        title={localize(
                            "numericInput-toggleDisabled",
                        )}
                        spreadBehaviour="free"
                        isActive={isDisabled}
                    />
                    <Switch
                        onPress={() => setIsCleanEnabled(!isCleanEnabled)}
                        title={localize("numericInput-toggleClean")}
                        isActive={isCleanEnabled}
                        spreadBehaviour="free"
                    />
                    <Switch
                        onPress={() => setIsRequired(!isRequired)}
                        title={localize(
                            "numericInput-toggleRequired",
                        )}
                        spreadBehaviour="free"
                        isActive={isRequired}
                    />
                    <Switch
                        onPress={() => setIsOptional(!isOptional)}
                        title={localize(
                            "numericInput-toggleOptional",
                        )}
                        spreadBehaviour="free"
                        isActive={isOptional}
                    />
                </View>
            </View>
        </View>
    </PageContainer>;
};
export default NumericInputPage;
