import { INaming } from "../../NamingCaption";
import { IParentFormManager } from "../../Page/ElementsOfFormFactory";
import { UserInfo } from "../Models/UserInfo/UserInfo";
import { IRoutes, ITypeOfFormForRoute, MenuFactory } from "./1-MenuFactory";
import { MenuSubItemFactory } from "./3-MenuSubItemFactory";


export class MenuItemFactory {

    public get currentSelected() {
        // return this.menuFactory.authFactory.systems;
        return true;
    }
    public visible: boolean = true;
    public keyOfItem: string;
    constructor(
        public menuFactory: MenuFactory,
        public caption: string,
        public accounting: boolean,
        public purchase: boolean,
        public sale: boolean,
        public treasury: boolean,
        public warehouse: boolean,
        public produce: boolean,
        public pms: boolean,
        public costOfGoods: boolean,
        public payment: boolean,
        public asset: boolean,
        public warranty: boolean,
        public automation: boolean,
        public dailyDistribution: boolean,
        public preventindMaintainance: boolean,
        public workflow: boolean,
        public users?: number[],
        public path?: string,
        public Page?: React.ComponentType<{ parentFormManager: IParentFormManager }>,
        public exact?: boolean,
        public onClick?: () => void,
    ) {
        this.keyOfItem = this.menuFactory.menuItems.length + 'menu';
    }

    public subMenuItems: MenuSubItemFactory[] = [];

    public addNewSubMenuItem(
        caption: INaming | string,
        path?: string,
        Page?: React.ComponentType<{ parentFormManager: IParentFormManager }>,
        exact?: boolean,
        onClick?: () => void,
        accounting?: boolean,
        purchase?: boolean,
        sale?: boolean,
        treasury?: boolean,
        warehouse?: boolean,
        produce?: boolean,
        pms?: boolean,
        costOfGoods?: boolean,
        payment?: boolean,
        asset?: boolean,
        warranty?: boolean,
        automation?: boolean,
        dailyDistribution?: boolean,
        preventindMaintainance?: boolean,
        workflow?: boolean,
        typeOfForm?: ITypeOfFormForRoute,
    ): MenuSubItemFactory {
        let captionFinal = '';
        if (caption) {
            if (typeof caption === 'string') {
                captionFinal = caption;
            } else if (caption) {
                captionFinal = this.menuFactory.mainStateManager.getCaptionNaming(caption);
            }
        }
        const menuSubItemFactory = new MenuSubItemFactory(
            this,
            captionFinal,
            accounting || false,
            purchase || false,
            sale || false,
            treasury || false,
            warehouse || false,
            produce || false,
            pms || false,
            costOfGoods || false,
            payment || false,
            asset || false,
            warranty || false,
            automation || false,
            dailyDistribution || false,
            preventindMaintainance || false,
            workflow || false,
            path,
            Page,
            exact,
            onClick,
            typeOfForm);
        this.subMenuItems.push(menuSubItemFactory);

        if (typeof path === 'string' && path !== "" && Page && typeof Page === 'function') {
            const route: IRoutes = { path, Page, exact, typeOfForm };
            this.menuFactory.routes.push(route);
        }

        return menuSubItemFactory;
    }

    public get isAllSystems() {
        return (
            this.accounting
            && this.asset
            && this.automation
            && this.costOfGoods
            && this.dailyDistribution
            && this.payment
            && this.pms
            && this.preventindMaintainance
            && this.pms
            && this.produce
            && this.purchase
            && this.sale
            && this.treasury
            && this.warehouse
            && this.warranty
            && this.workflow
        )
            || (
                !this.accounting
                && !this.asset
                && !this.automation
                && !this.costOfGoods
                && !this.dailyDistribution
                && !this.payment
                && !this.pms
                && !this.preventindMaintainance
                && !this.pms
                && !this.produce
                && !this.purchase
                && !this.sale
                && !this.treasury
                && !this.warehouse
                && !this.warranty
                && !this.workflow
            );
    }
    public get userInfo(): UserInfo {
        return this.menuFactory.mainStateManager.userInfo;
    }
    private get isNotAdminUser(): boolean {
        return !this.userInfo.isAdmin;
    }
    public get isInRole(): boolean {
        if (!this.userInfo.isAuthenticated()) {
            return false;
        }

        if (this.users instanceof Array && this.isNotAdminUser) {
            if (this.userInfo) {
                const userCode = this.userInfo.code;
                return this.users.includes(userCode);
            } else {
                return false;
            }
        }
        return true;
    }

    public get isSystemSelected(): boolean {
        return this.visible;
        // return this.visible &&
        // (
        //     this.isAllSystems ||
        //     this.currentSelected.accounting && this.accounting ||
        //     this.currentSelected.asset && this.asset ||
        //     this.currentSelected.automation && this.automation ||
        //     this.currentSelected.costOfGoods && this.costOfGoods ||
        //     this.currentSelected.dailyDistribution && this.dailyDistribution ||
        //     this.currentSelected.payment && this.payment ||
        //     this.currentSelected.pms && this.pms ||
        //     this.currentSelected.preventindMaintainance && this.preventindMaintainance ||
        //     this.currentSelected.produce && this.produce ||
        //     this.currentSelected.purchase && this.purchase ||
        //     this.currentSelected.sale && this.sale ||
        //     this.currentSelected.treasury && this.treasury ||
        //     this.currentSelected.warehouse && this.warehouse ||
        //     this.currentSelected.warranty && this.warranty ||
        //     this.currentSelected.workflow && this.workflow
        // ) &&
        // (this.subMenuItems && this.subMenuItems.length ? this.subMenuItems.some(i => i.isSystemSelected) : true)
    }
}