import {
    useLayoutEffect,
    useState
} from "react";
import {
    View
} from "react-native";
import stylesheet from "./stylesheet";
import {
    NCoreUIKitLocalize,
    NCoreUIKitTheme,
    PageContainer,
    CodeViewer,
    SelectBox,
    TextInput,
    Header,
    Switch,
    Chip,
    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 {
    IChipProps
} from "ncore-ui-kit";

const CHIP_TYPES: Array<IChipProps["type"]> = [
    "primary",
    "danger",
    "warning",
    "success",
    "info"
];
const CHIP_SPREAD_BEHAVIOURS: Array<IChipProps["spreadBehaviour"]> = [
    "baseline",
    "free"
];
const CHIP_SIZES: Array<IChipProps["size"]> = [
    "small",
    "medium",
    "large"
];

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

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

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

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

    const {
        localize
    } = NCoreUIKitLocalize.useContext();

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

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

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

    const [
        sizeIndex,
        setSizeIndex
    ] = useState<number>(1);

    const [
        isClosable,
        setIsClosable
    ] = useState<boolean>(true);

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

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

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

    useLayoutEffect(() => {
        navigation.setOptions({
            header: () => <Header
                title={localize("chip-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("chip-desc")}
            </Text>
            <View
                style={[
                    stylesheet.previewContainer,
                    {
                        borderColor: colors.content.border.subtle,
                        marginBottom: spaces.spacingMd,
                        borderRadius: radiuses.form,
                        borderWidth: borders.line,
                        padding: spaces.spacingMd
                    }
                ]}
            >
                <Chip
                    spreadBehaviour={CHIP_SPREAD_BEHAVIOURS[spreadIndex]}
                    type={CHIP_TYPES[typeIndex]}
                    size={CHIP_SIZES[sizeIndex]}
                    isClosable={isClosable}
                    isDisabled={isDisabled}
                    isLoading={isLoading}
                    title={title}
                />
            </View>
            <View
                style={{
                    marginBottom: spaces.spacingMd
                }}
            >
                <Text
                    style={{
                        marginBottom: spaces.spacingSm
                    }}
                    variant="headlineSmallSize"
                >
                    {localize("usage")}
                </Text>
                <CodeViewer
                    code={"import {\n    Chip\n} from \"ncore-ui-kit\";\n\n<Chip\n    title=\"Chip Title\"\n    type=\"primary\"\n    size=\"medium\"\n/>"}
                    language="tsx"
                />
            </View>
            <TextInput
                placeholder={localize("chip-enterText")}
                title={localize("chip-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 = CHIP_TYPES.indexOf(
                                    selectedItems[0]!
                                        .__key as IChipProps["type"],
                            );
                            if (index !== -1) setTypeIndex(index);
                        }
                    }}
                    initialSelectedItems={

                        CHIP_TYPES_DATA[typeIndex]

                            ? [
                                CHIP_TYPES_DATA[typeIndex]!
                            ]

                            : []

                    }
                    titleExtractor={(item) => item.__title}
                    title={localize("chip-changeType")}
                    keyExtractor={(item) => item.__key}
                    spreadBehaviour="free"
                    data={CHIP_TYPES_DATA}
                />
                <SelectBox
                    onChange={(selectedItems) => {
                        if (selectedItems.length > 0) {
                            const index = CHIP_SPREAD_BEHAVIOURS.indexOf(
                                    selectedItems[0]!
                                        .__key as IChipProps["spreadBehaviour"],
                            );
                            if (index !== -1) setSpreadIndex(index);
                        }
                    }}
                    initialSelectedItems={

                        CHIP_SPREAD_BEHAVIOURS_DATA[spreadIndex]

                            ? [
                                CHIP_SPREAD_BEHAVIOURS_DATA[spreadIndex]!
                            ]

                            : []

                    }
                    titleExtractor={(item) => item.__title}
                    title={localize("chip-changeSpread")}
                    keyExtractor={(item) => item.__key}
                    data={CHIP_SPREAD_BEHAVIOURS_DATA}
                    spreadBehaviour="free"
                />
                <SelectBox
                    onChange={(selectedItems) => {
                        if (selectedItems.length > 0) {
                            const index = CHIP_SIZES.indexOf(
                                    selectedItems[0]!
                                        .__key as IChipProps["size"],
                            );
                            if (index !== -1) setSizeIndex(index);
                        }
                    }}
                    initialSelectedItems={

                        CHIP_SIZES_DATA[sizeIndex]

                            ? [
                                CHIP_SIZES_DATA[sizeIndex]!
                            ]

                            : []

                    }
                    titleExtractor={(item) => item.__title}
                    title={localize("chip-changeSize")}
                    keyExtractor={(item) => item.__key}
                    data={CHIP_SIZES_DATA}
                    spreadBehaviour="free"
                />
                <View
                    style={stylesheet.switchesContainer}
                >
                    <Switch
                        title={localize("chip-toggleClosable")}
                        onPress={() => {
                            setIsClosable(!isClosable);
                        }}
                        spreadBehaviour="free"
                        isActive={isClosable}
                    />
                    <Switch
                        title={localize("chip-toggleLoading")}
                        onPress={() => {
                            setIsLoading(!isLoading);
                        }}
                        spreadBehaviour="free"
                        isActive={isLoading}
                    />
                    <Switch
                        title={localize("chip-toggleDisabled")}
                        onPress={() => {
                            setIsDisabled(!isDisabled);
                        }}
                        spreadBehaviour="free"
                        isActive={isDisabled}
                    />
                </View>
            </View>
        </View>
    </PageContainer>;
};
export default ChipPage;
