import {
    useLayoutEffect,
    useState
} from "react";
import {
    View
} from "react-native";
import stylesheet from "./stylesheet";
import {
    NCoreUIKitLocalize,
    NCoreUIKitTheme,
    HighlightButton,
    PageContainer,
    CodeViewer,
    SelectBox,
    TextInput,
    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 {
    IHighlightButtonProps
} from "ncore-ui-kit";
import {
    Home as HomeIcon
} from "lucide-react-native";

const TYPES: Array<IHighlightButtonProps["type"]> = [
    "primary",
    "danger",
    "warning",
    "neutral",
    "success",
    "info",
    "glass"
];
const SIZES: Array<IHighlightButtonProps["size"]> = [
    "small",
    "medium",
    "large"
];
const SPREAD_BEHAVIOURS: Array<IHighlightButtonProps["spreadBehaviour"]> = [
    "baseline",
    "stretch",
    "free"
];

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

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

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

    const {
        localize
    } = NCoreUIKitLocalize.useContext();

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

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

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

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

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

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

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

    const [
        isCustomPadding,
        setIsCustomPadding
    ] = useState<boolean>(false);

    const [
        showIcon,
        setShowIcon
    ] = useState<boolean>(false);

    const [
        iconDirection,
        setIconDirection
    ] = useState<"left" | "right">("left");

    useLayoutEffect(() => {
        navigation.setOptions({
            header: () => <Header
                title={localize("highlightButton-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("highlightButton-desc")}
            </Text>
            <View
                style={[
                    stylesheet.previewContainer,
                    {
                        borderColor: colors.content.border.subtle,
                        marginBottom: spaces.spacingMd,
                        borderRadius: radiuses.form,
                        borderWidth: borders.line,
                        padding: spaces.spacingMd
                    }
                ]}
            >
                <HighlightButton
                    title={
                        title || localize("highlightButton-example")
                    }
                    spreadBehaviour={SPREAD_BEHAVIOURS[spreadIndex]}
                    icon={
                        showIcon
                            ? ({
                                color,
                                size
                            }) => <HomeIcon
                                color={colors.content.icon[color]}
                                size={size}
                            />

                            : undefined
                    }
                    isCustomPadding={isCustomPadding}
                    iconDirection={iconDirection}
                    type={TYPES[typeIndex]}
                    size={SIZES[sizeIndex]}
                    isDisabled={isDisabled}
                    isLoading={isLoading}
                    onPress={() => {}}
                />
            </View>
            <View
                style={{
                    marginBottom: spaces.spacingMd
                }}
            >
                <Text
                    style={{
                        marginBottom: spaces.spacingSm
                    }}
                    variant="headlineSmallSize"
                >
                    {localize("usage")}
                </Text>
                <CodeViewer
                    code={"import {\n    HighlightButton\n} from \"ncore-ui-kit\";\n\n<HighlightButton\n    title=\"Highlight Button\"\n    onPress={() => {}}\n    type=\"primary\"\n/>"}
                    language="tsx"
                />
            </View>
            <TextInput
                placeholder={localize("highlightButton-enterText")}
                title={localize("highlightButton-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 = TYPES.indexOf(
                                    selectedItems[0]!
                                        .__key as unknown as IHighlightButtonProps["type"],
                            );
                            if (index !== -1) setTypeIndex(index);
                        }
                    }}
                    title={localize("highlightButton-changeType")}
                    titleExtractor={(item) => item.__title}
                    keyExtractor={(item) => item.__key}
                    initialSelectedItems={

                        TYPES_DATA[typeIndex]

                            ? [
                                TYPES_DATA[typeIndex]!
                            ]

                            : []

                    }
                    spreadBehaviour="free"
                    data={TYPES_DATA}
                />
                <HighlightButton
                    title={
                        `${localize("highlightButton-changeSpread")}: ${SPREAD_BEHAVIOURS[spreadIndex]}`
                    }
                    onPress={() => {
                        setSpreadIndex((prev) =>
                            prev + 1 > SPREAD_BEHAVIOURS.length - 1
                                ? 0
                                : prev + 1,
                        );
                    }}
                    spreadBehaviour="free"
                />
                <SelectBox
                    onChange={(selectedItems) => {
                        if (selectedItems.length > 0) {
                            const index = SIZES.indexOf(
                                    selectedItems[0]!
                                        .__key as unknown as IHighlightButtonProps["size"],
                            );
                            if (index !== -1) setSizeIndex(index);
                        }
                    }}
                    title={localize("highlightButton-changeSize")}
                    titleExtractor={(item) => item.__title}
                    keyExtractor={(item) => item.__key}
                    initialSelectedItems={

                        SIZES_DATA[sizeIndex]

                            ? [
                                SIZES_DATA[sizeIndex]!
                            ]

                            : []

                    }
                    spreadBehaviour="free"
                    data={SIZES_DATA}
                />
                <View
                    style={stylesheet.switchesContainer}
                >
                    <Switch
                        onPress={() =>
                            setIconDirection(
                                iconDirection === "left" ? "right" : "left",
                            )
                        }
                        title={localize(
                            "highlightButton-toggleIconDir",
                        )}
                        isActive={iconDirection === "right"}
                        spreadBehaviour="free"
                    />
                    <Switch
                        onPress={() => setShowIcon(!showIcon)}
                        title={localize(
                            "highlightButton-toggleIcon",
                        )}
                        spreadBehaviour="free"
                        isActive={showIcon}
                    />
                    <Switch
                        onPress={() => setIsLoading(!isLoading)}
                        title={localize(
                            "highlightButton-toggleLoading",
                        )}
                        spreadBehaviour="free"
                        isActive={isLoading}
                    />
                    <Switch
                        onPress={() => setIsDisabled(!isDisabled)}
                        title={localize(
                            "highlightButton-toggleDisabled",
                        )}
                        spreadBehaviour="free"
                        isActive={isDisabled}
                    />
                    <Switch
                        onPress={() => setIsCustomPadding(!isCustomPadding)}
                        title={localize(
                            "highlightButton-toggleCustomPadding",
                        )}
                        isActive={isCustomPadding}
                        spreadBehaviour="free"
                    />
                </View>
            </View>
        </View>
    </PageContainer>;
};
export default HighlightButtonPage;
