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

const DATE_TIME_PICKER_TYPES: Array<IDateTimePickerProps["type"]> = [
    "default",
    "danger",
    "warning",
    "success",
    "info",
    "question"
];
const DATE_TIME_PICKER_SPREAD_BEHAVIOURS: Array<IDateTimePickerProps["spreadBehaviour"]> = [
    "baseline",
    "stretch",
    "free"
];
const DATE_TIME_PICKER_PICKER_TYPES: Array<IDateTimePickerProps["pickerType"]> = [
    "date",
    "time",
    "date-time"
];
const DATE_TIME_PICKER_VARIANTS: Array<IDateTimePickerProps["variant"]> = [
    "single",
    "range",
    "rrule"
];

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

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

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

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

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

    const {
        localize
    } = NCoreUIKitLocalize.useContext();

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

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

    const [
        pickerTypeIndex,
        setPickerTypeIndex
    ] = useState<number>(0);

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

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

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

    useLayoutEffect(() => {
        navigation.setOptions({
            header: () => <Header
                title={localize("dateTimePicker-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("dateTimePicker-desc")}
            </Text>
            <View
                style={[
                    stylesheet.previewContainer,
                    {
                        borderColor: colors.content.border.subtle,
                        marginBottom: spaces.spacingMd,
                        borderRadius: radiuses.form,
                        borderWidth: borders.line,
                        padding: spaces.spacingMd
                    }
                ]}
            >
                <DateTimePicker
                    spreadBehaviour={
                        DATE_TIME_PICKER_SPREAD_BEHAVIOURS[spreadIndex]
                    }
                    pickerType={
                        DATE_TIME_PICKER_PICKER_TYPES[pickerTypeIndex]
                    }
                    variant={DATE_TIME_PICKER_VARIANTS[variantIndex]}
                    title={localize("dateTimePicker-title")}
                    type={DATE_TIME_PICKER_TYPES[typeIndex]}
                    isDisabled={isDisabled}
                />
            </View>
            <View
                style={{
                    marginBottom: spaces.spacingMd
                }}
            >
                <Text
                    style={{
                        marginBottom: spaces.spacingSm
                    }}
                    variant="headlineSmallSize"
                >
                    {localize("usage")}
                </Text>
                <CodeViewer
                    code={"import {\n    DateTimePicker\n} from \"ncore-ui-kit\";\n\n<DateTimePicker\n    title=\"Title\"\n    pickerType=\"date\"\n    variant=\"single\"\n    type=\"default\"\n/>"}
                    language="tsx"
                />
            </View>
            <View
                style={[
                    stylesheet.rowContainer,
                    {
                        gap: spaces.spacingMd
                    }
                ]}
            >
                <SelectBox
                    onChange={(selectedItems) => {
                        if (selectedItems.length > 0) {
                            const index = DATE_TIME_PICKER_TYPES.indexOf(
                                    selectedItems[0]!
                                        .__key as IDateTimePickerProps["type"],
                            );
                            if (index !== -1) setTypeIndex(index);
                        }
                    }}
                    initialSelectedItems={

                        DATE_TIME_PICKER_TYPES_DATA[typeIndex]

                            ? [
                                DATE_TIME_PICKER_TYPES_DATA[typeIndex]!
                            ]

                            : []

                    }
                    title={localize("dateTimePicker-changeType")}
                    titleExtractor={(item) => item.__title}
                    keyExtractor={(item) => item.__key}
                    data={DATE_TIME_PICKER_TYPES_DATA}
                    spreadBehaviour="free"
                />
                <SelectBox
                    onChange={(selectedItems) => {
                        if (selectedItems.length > 0) {
                            const index =
                                    DATE_TIME_PICKER_SPREAD_BEHAVIOURS.indexOf(
                                        selectedItems[0]!
                                            .__key as IDateTimePickerProps["spreadBehaviour"],
                                    );
                            if (index !== -1) setSpreadIndex(index);
                        }
                    }}
                    initialSelectedItems={
                        DATE_TIME_PICKER_SPREAD_BEHAVIOURS_DATA[spreadIndex]
                            ? [
                                    DATE_TIME_PICKER_SPREAD_BEHAVIOURS_DATA[
                                        spreadIndex
                                    ]!
                            ]
                            : []
                    }
                    title={localize("dateTimePicker-changeSpread")}
                    data={DATE_TIME_PICKER_SPREAD_BEHAVIOURS_DATA}
                    titleExtractor={(item) => item.__title}
                    keyExtractor={(item) => item.__key}
                    spreadBehaviour="free"
                />
                <SelectBox
                    onChange={(selectedItems) => {
                        if (selectedItems.length > 0) {
                            const index =
                                    DATE_TIME_PICKER_PICKER_TYPES.indexOf(
                                        selectedItems[0]!
                                            .__key as DateTimePickerPickerType,
                                    );
                            if (index !== -1) setPickerTypeIndex(index);
                        }
                    }}
                    initialSelectedItems={
                        DATE_TIME_PICKER_PICKER_TYPES_DATA[pickerTypeIndex]
                            ? [
                                    DATE_TIME_PICKER_PICKER_TYPES_DATA[
                                        pickerTypeIndex
                                    ]!
                            ]
                            : []
                    }
                    data={DATE_TIME_PICKER_PICKER_TYPES_DATA}
                    titleExtractor={(item) => item.__title}
                    title={localize(
                        "dateTimePicker-changePickerType",
                    )}
                    keyExtractor={(item) => item.__key}
                    spreadBehaviour="free"
                />
                <SelectBox
                    onChange={(selectedItems) => {
                        if (selectedItems.length > 0) {
                            const index = DATE_TIME_PICKER_VARIANTS.indexOf(
                                    selectedItems[0]!
                                        .__key as IDateTimePickerProps["variant"],
                            );
                            if (index !== -1) setVariantIndex(index);
                        }
                    }}
                    initialSelectedItems={

                        DATE_TIME_PICKER_VARIANTS_DATA[variantIndex]

                            ? [
                                DATE_TIME_PICKER_VARIANTS_DATA[variantIndex]!
                            ]

                            : []

                    }
                    title={localize("dateTimePicker-changeVariant")}
                    titleExtractor={(item) => item.__title}
                    data={DATE_TIME_PICKER_VARIANTS_DATA}
                    keyExtractor={(item) => item.__key}
                    spreadBehaviour="free"
                />
                <View
                    style={stylesheet.switchesContainer}
                >
                    <Switch
                        title={localize(
                            "dateTimePicker-toggleDisabled",
                        )}
                        onPress={() => {
                            setIsDisabled(!isDisabled);
                        }}
                        spreadBehaviour="free"
                        isActive={isDisabled}
                    />
                </View>
            </View>
        </View>
    </PageContainer>;
};
export default DateTimePickerPage;
