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

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

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

const AVATAR_STATUS_INDICATOR_TYPES = Object.keys(STATUS_CHEAT_SHEET) as Array<ActivityStatusType>;

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

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

    const {
        localize
    } = NCoreUIKitLocalize.useContext();

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

    const [
        statusIndicatorIndex,
        setStatusIndicatorIndex
    ] = useState<number>(1);

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

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

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

    const [
        imageUrl,
        setImageUrl
    ] = useState<string>(
        "https://ncore.nibgat.space/assets/images/ncorelogo.png",
    );

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

    useLayoutEffect(() => {
        navigation.setOptions({
            header: () => <Header
                title={localize("avatar-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("avatar-desc")}
            </Text>
            <View
                style={[
                    stylesheet.previewContainer,
                    {
                        borderColor: colors.content.border.subtle,
                        marginBottom: spaces.spacingMd,
                        borderRadius: radiuses.form,
                        borderWidth: borders.line,
                        padding: spaces.spacingMd
                    }
                ]}
            >
                <Avatar
                    statusIndicatorType={
                        AVATAR_STATUS_INDICATOR_TYPES[statusIndicatorIndex]
                    }
                    size={AVATAR_SIZES[sizeIndex]}
                    isStatusIndicator={true}
                    isDisabled={isDisabled}
                    isLoading={isLoading}
                    imageUrl={imageUrl}
                    title={title}
                />
            </View>
            <View
                style={{
                    marginBottom: spaces.spacingMd
                }}
            >
                <Text
                    style={{
                        marginBottom: spaces.spacingSm
                    }}
                    variant="headlineSmallSize"
                >
                    {localize("usage")}
                </Text>
                <CodeViewer
                    code={"import {\n    Avatar\n} from \"ncore-ui-kit\";\n\n<Avatar\n    title=\"John Doe\"\n    imageUrl=\"https://ncore.nibgat.space/assets/images/ncorelogo.png\"\n    statusIndicatorType=\"online\"\n    isStatusIndicator={true}\n    size=\"large\"\n/>"}
                    language="tsx"
                />
            </View>
            <TextInput
                placeholder={localize("avatar-enterImageUrl")}
                title={localize("avatar-imageUrl")}
                style={{
                    marginBottom: spaces.spacingMd
                }}
                onChangeText={setImageUrl}
                spreadBehaviour="stretch"
                value={imageUrl}
            />
            <TextInput
                placeholder={localize("avatar-enterText")}
                title={localize("avatar-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 = AVATAR_SIZES.indexOf(
                                    selectedItems[0]!
                                        .__key as IAvatarProps["size"],
                            );
                            if (index !== -1) setSizeIndex(index);
                        }
                    }}
                    initialSelectedItems={

                        AVATAR_SIZES_DATA[sizeIndex]

                            ? [
                                AVATAR_SIZES_DATA[sizeIndex]!
                            ]

                            : []

                    }
                    titleExtractor={(item) => item.__title}
                    keyExtractor={(item) => item.__key}
                    title={localize("change-size")}
                    data={AVATAR_SIZES_DATA}
                    spreadBehaviour="free"
                />
                <SelectBox
                    onChange={(selectedItems) => {
                        if (selectedItems.length > 0) {
                            const index =
                                    AVATAR_STATUS_INDICATOR_TYPES.indexOf(
                                        selectedItems[0]!
                                            .__key as ActivityStatusType,
                                    );
                            if (index !== -1)
                                setStatusIndicatorIndex(index);
                        }
                    }}
                    initialSelectedItems={
                        AVATAR_STATUS_INDICATOR_TYPES_DATA[
                            statusIndicatorIndex
                        ]
                            ? [
                                    AVATAR_STATUS_INDICATOR_TYPES_DATA[
                                        statusIndicatorIndex
                                    ]!
                            ]
                            : []
                    }
                    data={AVATAR_STATUS_INDICATOR_TYPES_DATA}
                    title={localize("avatar-changeStatus")}
                    titleExtractor={(item) => item.__title}
                    keyExtractor={(item) => item.__key}
                    spreadBehaviour="free"
                />
                <View
                    style={stylesheet.switchesContainer}
                >
                    <Switch
                        title={localize("avatar-toggleLoading")}
                        onPress={() => {
                            setIsLoading(!isLoading);
                        }}
                        spreadBehaviour="free"
                        isActive={isLoading}
                    />
                    <Switch
                        title={localize("avatar-toggleDisabled")}
                        onPress={() => {
                            setIsDisabled(!isDisabled);
                        }}
                        spreadBehaviour="free"
                        isActive={isDisabled}
                    />
                </View>
            </View>
        </View>
    </PageContainer>;
};
export default AvatarPage;
