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 {
    NIBGATCommunityIcon,
    CayCoreIcon,
    NIBGATIcon
} from "../../assets/svg";
import HighlightButton from "../highlightButton";
import Button from "../button";

const PaletteSwitcher: FC<IPaletteSwitcherProps> = ({
    variants: variantsProps = [],
    isWorkWithHighlightButton,
    isWorkWithNextShowSystem,
    customBorderColor,
    customColor,
    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: Array<Omit<ThemeSwitchVariants[0], "backgroundColor"> & {
        backgroundColor?: ThemeSwitchVariants[0]["backgroundColor"]
    }> = 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({
            backgroundColor: isWorkWithHighlightButton ? undefined : "subtle",
            borderColor: "transparent",
            icon: ({
                size
            }) => {
                return <NIBGATIcon
                    size={size + 5}
                />;
            },
            paletteKey: "nibgat",
            iconColor: "moon"
        });
    }

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

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

    const variants = [
        ...variantsPre
    ].sort((a, b) => {
        const indexA = paletteKeys.indexOf(a.paletteKey);
        const indexB = paletteKeys.indexOf(b.paletteKey);

        if (indexA === -1 && indexB !== -1) return 1;

        if (indexB === -1 && indexA !== -1) return -1;

        if (indexA === -1 && indexB === -1) {
            return a.paletteKey.localeCompare(b.paletteKey);
        }

        return indexA - indexB;
    });

    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];

    const allProps = {
        ...props,
        icon: ({
            color,
            size
        }: {
            color: keyof NCoreUIKit.IconContentColors;
            size: number;
        }) => {
            const Icon = currentVariant?.icon as NCoreUIKitIcon;

            return <Icon
                customColor={currentVariant && currentVariant.iconColor ? currentVariant.iconColor : undefined}
                color={color}
                size={size}
            />;
        },
        customBorderColor: customBorderColor || (currentVariant && currentVariant.borderColor ?
            currentVariant.borderColor
            :
            color),
        customColor: customColor || (currentVariant && currentVariant.backgroundColor ?
            currentVariant.backgroundColor
            :
            color),
        onPress: () => {
            NCoreUIKitTheme.switch({
                paletteKey: paletteKeys[viewVariantIndex]
            });
        },
        style: [
            style
        ]
    };

    if(isWorkWithHighlightButton) {
        return <HighlightButton
            {...allProps}
        />;
    }

    return <Button
        {...allProps}
    />;
};
export default PaletteSwitcher;
