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

const AVATAR_GROUP_SIZES: Array<IAvatarGroupProps["size"]> = [
    "small",
    "medium",
    "large",
    "xLarge",
    "xSmall"
];

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

const MOCK_AVATARS = [
    {
        imageUrl: "https://ncore.nibgat.space/assets/images/ncorelogo.png",
        title: "Ncore System"
    },
    {
        title: "Jane Smith"
    },
    {
        title: "Mike Ross"
    },
    {
        title: "John Doe"
    }
];

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

    const {
        localize
    } = NCoreUIKitLocalize.useContext();

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

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

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

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

    useLayoutEffect(() => {
        navigation.setOptions({
            header: () => <Header
                title={localize("avatarGroup-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("avatarGroup-desc")}
            </Text>
            <View
                style={[
                    stylesheet.previewContainer,
                    {
                        borderColor: colors.content.border.subtle,
                        marginBottom: spaces.spacingMd,
                        borderRadius: radiuses.form,
                        borderWidth: borders.line,
                        padding: spaces.spacingMd
                    }
                ]}
            >
                <AvatarGroup
                    size={AVATAR_GROUP_SIZES[sizeIndex]}
                    isDisabled={isDisabled}
                    avatars={MOCK_AVATARS}
                    isLoading={isLoading}
                />
            </View>
            <View
                style={{
                    marginBottom: spaces.spacingMd
                }}
            >
                <Text
                    style={{
                        marginBottom: spaces.spacingSm
                    }}
                    variant="headlineSmallSize"
                >
                    {localize("usage")}
                </Text>
                <CodeViewer
                    code={"import {\n    AvatarGroup\n} from \"ncore-ui-kit\";\n\n<AvatarGroup\n    avatars={[\n        { title: \"John Doe\" },\n        { title: \"Jane Smith\" }\n    ]}\n    size=\"large\"\n/>"}
                    language="tsx"
                />
            </View>
            <View
                style={[
                    stylesheet.rowContainer,
                    {
                        gap: spaces.spacingMd
                    }
                ]}
            >
                <SelectBox
                    onChange={(selectedItems) => {
                        if (selectedItems.length > 0) {
                            const index = AVATAR_GROUP_SIZES.indexOf(
                                    selectedItems[0]!
                                        .__key as IAvatarGroupProps["size"],
                            );
                            if (index !== -1) setSizeIndex(index);
                        }
                    }}
                    initialSelectedItems={

                        AVATAR_GROUP_SIZES_DATA[sizeIndex]

                            ? [
                                AVATAR_GROUP_SIZES_DATA[sizeIndex]!
                            ]

                            : []

                    }
                    title={localize("avatarGroup-changeSize")}
                    titleExtractor={(item) => item.__title}
                    keyExtractor={(item) => item.__key}
                    data={AVATAR_GROUP_SIZES_DATA}
                    spreadBehaviour="free"
                />
                <View
                    style={stylesheet.switchesContainer}
                >
                    <Switch
                        title={localize("avatarGroup-toggleLoading")}
                        onPress={() => {
                            setIsLoading(!isLoading);
                        }}
                        spreadBehaviour="free"
                        isActive={isLoading}
                    />
                    <Switch
                        title={localize(
                            "avatarGroup-toggleDisabled",
                        )}
                        onPress={() => {
                            setIsDisabled(!isDisabled);
                        }}
                        spreadBehaviour="free"
                        isActive={isDisabled}
                    />
                </View>
            </View>
        </View>
    </PageContainer>;
};
export default AvatarGroupPage;
