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

const SPREAD_BEHAVIOURS: Array<ISwitchProps["spreadBehaviour"]> = [
    "baseline",
    "stretch",
    "free"
];

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

    const {
        localize
    } = NCoreUIKitLocalize.useContext();

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

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

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

    const [
        subTitle,
        setSubTitle
    ] = useState<string>("");

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

    const [
        isActive,
        setIsActive
    ] = useState<boolean>(false);

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

    useLayoutEffect(() => {
        navigation.setOptions({
            header: () => <Header
                title={localize("switch-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("switch-desc")}
            </Text>
            <View
                style={[
                    stylesheet.previewContainer,
                    {
                        borderColor: colors.content.border.subtle,
                        marginBottom: spaces.spacingMd,
                        borderRadius: radiuses.form,
                        borderWidth: borders.line,
                        padding: spaces.spacingMd
                    }
                ]}
            >
                <Switch
                    title={title || localize("switch-exampleTitle")}
                    spreadBehaviour={SPREAD_BEHAVIOURS[spreadIndex]}
                    onPress={() => setIsActive(!isActive)}
                    isDisabled={isDisabled}
                    isLoading={isLoading}
                    subTitle={subTitle}
                    isActive={isActive}
                />
            </View>
            <View
                style={{
                    marginBottom: spaces.spacingMd
                }}
            >
                <Text
                    style={{
                        marginBottom: spaces.spacingSm
                    }}
                    variant="headlineSmallSize"
                >
                    {localize("usage")}
                </Text>
                <CodeViewer
                    code={"import {\n    Switch\n} from \"ncore-ui-kit\";\n\n<Switch\n    title=\"Title\"\n    isActive={isActive}\n    onPress={() => setIsActive(!isActive)}\n/>"}
                    language="tsx"
                />
            </View>
            <TextInput
                placeholder={localize("switch-enterTitle")}
                title={localize("switch-titleLabel")}
                style={{
                    marginBottom: spaces.spacingMd
                }}
                spreadBehaviour="stretch"
                onChangeText={setTitle}
                value={title}
            />
            <TextInput
                placeholder={localize("switch-enterSubTitle")}
                title={localize("switch-subTitleLabel")}
                style={{
                    marginBottom: spaces.spacingMd
                }}
                onChangeText={setSubTitle}
                spreadBehaviour="stretch"
                value={subTitle}
            />
            <View
                style={[
                    stylesheet.rowContainer,
                    {
                        gap: spaces.spacingMd
                    }
                ]}
            >
                <Button
                    title={
                        `${localize("switch-changeSpread")}: ${SPREAD_BEHAVIOURS[spreadIndex]}`
                    }
                    onPress={() => {
                        setSpreadIndex((prev) =>
                            prev + 1 > SPREAD_BEHAVIOURS.length - 1
                                ? 0
                                : prev + 1,
                        );
                    }}
                    spreadBehaviour="free"
                />
                <View
                    style={stylesheet.switchesContainer}
                >
                    <Switch
                        onPress={() => setIsDisabled(!isDisabled)}
                        title={localize("switch-toggleDisabled")}
                        spreadBehaviour="free"
                        isActive={isDisabled}
                    />
                    <Switch
                        title={localize("switch-toggleLoading")}
                        onPress={() => setIsLoading(!isLoading)}
                        spreadBehaviour="free"
                        isActive={isLoading}
                    />
                    <Switch
                        title={localize("switch-toggleActive")}
                        onPress={() => setIsActive(!isActive)}
                        spreadBehaviour="free"
                        isActive={isActive}
                    />
                </View>
            </View>
        </View>
    </PageContainer>;
};
export default SwitchPage;
