import {
    type ReactNode,
    Fragment
} from "react";
import {
    type MenuContextType,
    type MenuDataType
} from "../types/menu";
import NCoreContext, {
    type ConfigType
} from "ncore-context";
import Menu from "../components/menu";
import {
    uuid
} from "../utils";

class NCoreUIKitMenu extends NCoreContext<MenuContextType, ConfigType<MenuContextType>> {
    constructor({
        data = []
    }: {
        data?: Array<MenuDataType>
    }) {
        super({
            get: () => undefined,
            unload: () => "",
            update: () => {},
            close: () => {},
            load: () => "",
            open: () => {},
            data: data
        }, {
            key: "NCoreUIKit-MenuContext"
        });
    };

    load = (menuData: MenuDataType) => {
        const currentData = this.state.data;

        const menuID = menuData.id ? menuData.id : uuid();

        currentData.push({
            ...menuData,
            id: menuID
        });

        this.setState({
            data: currentData
        });

        return menuID;
    };

    get = ({
        index,
        id
    }: {
        index?: number;
        id?: string;
    }) => {
        const currentData = this.state.data;

        if(id) {
            const currentIndex = currentData.findIndex(cI => cI.id === id);

            return currentData[currentIndex];
        }

        if(index) {
            return currentData[index];
        }

        return undefined;
    };

    update = ({
        menuData,
        index,
        id
    }: {
        menuData: MenuDataType;
        index?: number;
        id?: string;
    }) => {
        const currentData = this.state.data;

        if(id) {
            const currentIndex = currentData.findIndex(cI => cI.id === id);

            currentData[currentIndex] = menuData;
        }

        if(index) {
            currentData[index] = menuData;
        }

        this.setState({
            data: currentData
        });
    };

    open = (props?: {
        index?: number;
        id?: string;
    }) => {
        const currentData = this.state.data;

        if (props && props.id) {
            const keyIndex = currentData.findIndex((menu) => menu.id === props.id);

            if (keyIndex !== -1) {
                currentData[keyIndex]!.isCollapse = true;

                this.setState({
                    data: currentData
                });
            }

            return;
        }

        if (props && props.index !== undefined && currentData[props.index]) {
            currentData[props.index!]!.isCollapse = true;

            this.setState({
                data: currentData
            });

            return;
        }
    };

    close = (props?: {
        index?: number;
        id?: string;
    }) => {
        const currentData = this.state.data;

        if (props && props.id) {
            const keyIndex = currentData.findIndex((menu) => menu.id === props.id);

            if (keyIndex !== -1) {
                currentData[keyIndex]!.isCollapse = false;

                this.setState({
                    data: currentData
                });
            }

            return;
        }

        if (props && props.index !== undefined && currentData[props.index]) {
            currentData[props.index!]!.isCollapse = false;

            this.setState({
                data: currentData
            });

            return;
        }
    };

    unload = (props?: {
        index?: number;
        id?: string;
    }) => {
        const currentData = this.state.data;

        if (props && props.id) {
            const keyIndex = currentData.findIndex((menu) => menu.id === props.id);

            if (keyIndex !== -1) {
                currentData.splice(keyIndex, 1);

                this.setState({
                    data: currentData
                });
            }

            return;
        }

        if (props && props.index !== undefined) {
            currentData.splice(props.index, 1);

            this.setState({
                data: currentData
            });

            return;
        }

        currentData.pop();

        this.setState({
            data: currentData
        });
    };

    Render = ({
        children
    }: {
        children: ReactNode;
    }) => {
        const {
            data
        } = this.useContext();

        return <Fragment>
            {children}
            {data.map((item: MenuDataType, index: number) => {
                return <Menu
                    key={`NCoreUIKit-Menu-${item.id}`}
                    id={item.id as string}
                    close={() => {
                        if(item.isAutoClosed) {
                            this.close({
                                index: index,
                                id: item.id
                            });
                        }
                    }}
                    onClosed={() => {
                        if(item.onClosed) {
                            item.onClosed({
                                id: item.id as string
                            });
                        }

                        if(item.isAutoClosedOnClosed === undefined || item.isAutoClosedOnClosed === true) {
                            this.close({
                                index: index,
                                id: item.id
                            });
                        }
                    }}
                    {...item}
                />;
            })}
        </Fragment>;
    };
}
export default NCoreUIKitMenu;
