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

const DIALOG_VARIANTS: Array<IDialogProps["variant"]> = [
    "yes-no",
    "ok",
    "info"
];

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

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

    const {
        localize
    } = NCoreUIKitLocalize.useContext();

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

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

    const [
        isVisible,
        setIsVisible
    ] = useState<boolean>(false);

    const [
        content,
        setContent
    ] = useState<string>("This is a dialog content.");

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

    useLayoutEffect(() => {
        navigation.setOptions({
            header: () => <Header
                title={localize("dialog-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("dialog-desc")}
            </Text>
            <View
                style={[
                    stylesheet.previewContainer,
                    {
                        borderColor: colors.content.border.subtle,
                        marginBottom: spaces.spacingMd,
                        borderRadius: radiuses.form,
                        borderWidth: borders.line,
                        padding: spaces.spacingMd
                    }
                ]}
            >
                <Button
                    title={localize("dialog-openDialog")}
                    onPress={() => setIsVisible(true)}
                />
            </View>
            <View
                style={{
                    marginBottom: spaces.spacingMd
                }}
            >
                <Text
                    style={{
                        marginBottom: spaces.spacingSm
                    }}
                    variant="headlineSmallSize"
                >
                    {localize("usage")}
                </Text>
                <CodeViewer
                    code={"import {\n    Dialog\n} from \"ncore-ui-kit\";\n\n<Dialog\n    title=\"Dialog Title\"\n    content=\"This is a dialog content.\"\n    isVisible={isVisible}\n    variant=\"yes-no\"\n/>"}
                    language="tsx"
                />
            </View>
            <TextInput
                placeholder={localize("dialog-enterText")}
                title={localize("dialog-titleLabel")}
                style={{
                    marginBottom: spaces.spacingMd
                }}
                spreadBehaviour="stretch"
                onChangeText={setTitle}
                value={title}
            />
            <TextInput
                placeholder={localize("dialog-enterContentText")}
                title={localize("dialog-contentLabel")}
                style={{
                    marginBottom: spaces.spacingMd
                }}
                spreadBehaviour="stretch"
                onChangeText={setContent}
                value={content}
            />
            <View
                style={[
                    stylesheet.rowContainer,
                    {
                        gap: spaces.spacingMd
                    }
                ]}
            >
                <SelectBox
                    onChange={(selectedItems) => {
                        if (selectedItems.length > 0) {
                            const index = DIALOG_VARIANTS.indexOf(
                                    selectedItems[0]!
                                        .__key as IDialogProps["variant"],
                            );
                            if (index !== -1) setVariantIndex(index);
                        }
                    }}
                    initialSelectedItems={

                        DIALOG_VARIANTS_DATA[variantIndex]

                            ? [
                                DIALOG_VARIANTS_DATA[variantIndex]!
                            ]

                            : []

                    }
                    title={localize("dialog-changeVariant")}
                    titleExtractor={(item) => item.__title}
                    keyExtractor={(item) => item.__key}
                    data={DIALOG_VARIANTS_DATA}
                    spreadBehaviour="free"
                />
            </View>
        </View>
        <Dialog
            variant={DIALOG_VARIANTS[variantIndex]}
            secondaryButtonProps={{
                onPress: ({
                    closeAnimation
                }) => {
                    closeAnimation();
                }
            }}
            primaryButtonProps={{
                onPress: ({
                    closeAnimation
                }) => {
                    closeAnimation();
                }
            }}
            onClosed={() => {
                setIsVisible(false);
            }}
            onOverlayPress={({
                closeAnimation
            }) => {
                closeAnimation();
            }}
            isVisible={isVisible}
            content={content}
            title={title}
        />
    </PageContainer>;
};
export default DialogPage;
