import {
    useLayoutEffect,
    useState
} from "react";
import {
    View
} from "react-native";
import stylesheet from "./stylesheet";
import {
    NCoreUIKitLocalize,
    NCoreUIKitTheme,
    PageContainer,
    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
            }}
            scrollViewStyle={[stylesheet.container]}
            isWorkWithHeaderSpace={false}
            isScrollable={true}
        >
            <View style={[stylesheet.contentContainer]}>
                <Text
                    variant="bodyMediumSize"
                    color="mid"
                    style={{
                        marginBottom: 20
                    }}
                >
                    {localize("switch-desc")}
                </Text>
                <View
                    style={{
                        borderColor: colors.content.border.subtle,
                        marginBottom: spaces.spacingMd,
                        borderRadius: radiuses.form,
                        borderWidth: borders.line,
                        padding: spaces.spacingMd,
                        justifyContent: "center",
                        alignItems: "center",
                        minHeight: 120
                    }}
                >
                    <Switch
                        title={title || localize("switch-exampleTitle")}
                        spreadBehaviour={SPREAD_BEHAVIOURS[spreadIndex]}
                        onPress={() => setIsActive(!isActive)}
                        isDisabled={isDisabled}
                        isLoading={isLoading}
                        subTitle={subTitle}
                        isActive={isActive}
                    />
                </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={{
                        justifyContent: "space-between",
                        gap: spaces.spacingMd,
                        flexDirection: "row",
                        flexWrap: "wrap",
                        width: "100%"
                    }}
                >
                    <Button
                        onPress={() => {
                            setSpreadIndex((prev) =>
                                prev + 1 > SPREAD_BEHAVIOURS.length - 1
                                    ? 0
                                    : prev + 1,
                            );
                        }}
                        title={
                            `${localize("switch-changeSpread")}: ${SPREAD_BEHAVIOURS[spreadIndex]}`
                        }
                        spreadBehaviour="free"
                    />
                    <View
                        style={{
                            justifyContent: "space-between",
                            flexDirection: "row",
                            alignItems: "center",
                            flexWrap: "wrap",
                            width: "100%"
                        }}
                    >
                        <Switch
                            title={localize("switch-toggleDisabled")}
                            onPress={() => setIsDisabled(!isDisabled)}
                            isActive={isDisabled}
                        />
                        <Switch
                            title={localize("switch-toggleLoading")}
                            onPress={() => setIsLoading(!isLoading)}
                            isActive={isLoading}
                        />
                        <Switch
                            title={localize("switch-toggleActive")}
                            onPress={() => setIsActive(!isActive)}
                            isActive={isActive}
                        />
                    </View>
                </View>
            </View>
        </PageContainer>
    );
};
export default SwitchPage;
