import {
    useEffect,
    useState,
    type FC
} from "react";
import type IPaletteSwitcherProps from "./type";
import type {
    ThemeSwitchVariants
} from "./type";
import {
    NCoreUIKitTheme
} from "../../core/hooks";
import type {
    NCoreUIKitIcon
} from "../../types";
import Button from "../button";
import {
    CayCoreIcon,
    NIBGATCommunityIcon,
    NIBGATIcon
} from "../../assets/svg";

const PaletteSwitcher: FC<IPaletteSwitcherProps> = ({
    variants: variantsProps = [],
    isWorkWithNextShowSystem,
    customTheme,
    color,
    style,
    ...props
}) => {
    const {
        activePalette,
        paletteKeys
    } = NCoreUIKitTheme.useContext(customTheme);

    const [
        currentIndex,
        setCurrentIndex
    ] = useState(paletteKeys.findIndex(t => t === activePalette));

    useEffect(() => {
        const cI = paletteKeys.findIndex(tI => tI === activePalette);

        const nextCI = isWorkWithNextShowSystem ? cI + 1 > paletteKeys.length - 1 ? 0 : cI + 1 : cI;

        if(currentIndex !== nextCI) {
            setCurrentIndex(nextCI);
        }
    }, [activePalette]);

    const variantsPre: ThemeSwitchVariants = variantsProps;

    const isHaveNC = variantsProps.findIndex(iV => iV.paletteKey === "nibgat-community") !== -1;
    const isHaveCC = variantsProps.findIndex(iV => iV.paletteKey === "caycore") !== -1;
    const isHaveN = variantsProps.findIndex(iV => iV.paletteKey === "nibgat") !== -1;

    if(!isHaveN) {
        variantsPre.unshift({
            borderColor: "transparent",
            backgroundColor: "subtle",
            paletteKey: "nibgat",
            iconColor: "moon",
            icon: ({
                size
            }) => {
                return <NIBGATIcon
                    size={size + 5}
                />;
            }
        });
    }

    if(!isHaveNC) {
        variantsPre.unshift({
            paletteKey: "nibgat-community",
            borderColor: "transparent",
            backgroundColor: "subtle",
            iconColor: "moon",
            icon: ({
                size
            }) => {
                return <NIBGATCommunityIcon
                    size={size + 5}
                />;
            }
        });
    }

    if(!isHaveCC) {
        variantsPre.unshift({
            borderColor: "transparent",
            backgroundColor: "subtle",
            paletteKey: "caycore",
            iconColor: "moon",
            icon: ({
                size
            }) => {
                return <CayCoreIcon
                    size={size + 5}
                />;
            }
        });
    }

    const variants = [...variantsPre].sort((a, b) => {
        return paletteKeys.indexOf(a.paletteKey) - paletteKeys.indexOf(b.paletteKey);
    });

    const currentVariantIndex = variants.findIndex(e => e.paletteKey === activePalette);

    if(currentVariantIndex === -1) {
        return null;
    }

    const nextVariantIndex = currentVariantIndex + 1;
    const viewVariantIndex = nextVariantIndex > variants.length - 1 ? 0 : nextVariantIndex;

    const currentVariant = variants[isWorkWithNextShowSystem ? viewVariantIndex : currentVariantIndex];

    return <Button
        {...props}
        customBorderColor={currentVariant && currentVariant.borderColor ?
            currentVariant.borderColor
            :
            color
        }
        customColor={currentVariant && currentVariant.backgroundColor ?
            currentVariant.backgroundColor
            :
            color
        }
        icon={({
            color,
            size
        }) => {
            const Icon = currentVariant?.icon as NCoreUIKitIcon;

            return <Icon
                customColor={currentVariant && currentVariant.iconColor ? currentVariant.iconColor : undefined}
                color={color}
                size={size}
            />;
        }}
        onPress={() => {
            NCoreUIKitTheme.switch({
                paletteKey: paletteKeys[viewVariantIndex]
            });
        }}
        style={[
            style
        ]}
    />;
};
export default PaletteSwitcher;
