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

const SIZES: Array<ILoadingProps["size"]> = [
    16,
    24,
    32
];

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

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

    const {
        localize
    } = NCoreUIKitLocalize.useContext();

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

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

    useLayoutEffect(() => {
        navigation.setOptions({
            header: () => <Header
                title={localize("loading-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("loading-desc")}
            </Text>
            <View
                style={[
                    stylesheet.previewContainer,
                    {
                        borderColor: colors.content.border.subtle,
                        marginBottom: spaces.spacingMd,
                        borderRadius: radiuses.form,
                        borderWidth: borders.line,
                        padding: spaces.spacingMd
                    }
                ]}
            >
                <Loading
                    size={SIZES[sizeIndex]}
                />
            </View>
            <View
                style={{
                    marginBottom: spaces.spacingMd
                }}
            >
                <Text
                    style={{
                        marginBottom: spaces.spacingSm
                    }}
                    variant="headlineSmallSize"
                >
                    {localize("usage")}
                </Text>
                <CodeViewer
                    code={"import {\n    Loading\n} from \"ncore-ui-kit\";\n\n<Loading size={24} />"}
                    language="tsx"
                />
            </View>
            <View
                style={[
                    stylesheet.rowContainer,
                    {
                        gap: spaces.spacingMd
                    }
                ]}
            >
                <SelectBox
                    onChange={(selectedItems) => {
                        if (selectedItems.length > 0) {
                            const index = SIZES.indexOf(
                                    Number(
                                        selectedItems[0]!.__key,
                                    ) as ILoadingProps["size"],
                            );
                            if (index !== -1) setSizeIndex(index);
                        }
                    }}
                    titleExtractor={(item) => item.__title}
                    title={localize("loading-changeSize")}
                    keyExtractor={(item) => item.__key}
                    initialSelectedItems={

                        SIZES_DATA[sizeIndex]

                            ? [
                                SIZES_DATA[sizeIndex]!
                            ]

                            : []

                    }
                    spreadBehaviour="free"
                    data={SIZES_DATA}
                />
            </View>
        </View>
    </PageContainer>;
};
export default LoadingPage;
