import { ITypeOfFormForRoute } from "../../AmisaAuth/Menus/1-MenuFactory";
import { MainStateManager } from "../../MainStateManager";
import { IMainStateFactory } from "../../Types";
import { Argument } from "./Argument";
import { SubPageItem } from "./SubPageItem";

export class SubPagesFactory {

    public title: string = '';
    public activeSubPage?: SubPageItem;
    public forceUpdate = () => { };
    public subPages: SubPageItem[] = [];
    public subPagesOrder: SubPageItem[] = [];
    constructor(
        public mainStateFactory: IMainStateFactory
    ) { }

    public gotoHomePage = () => {
        this.activeSubPage = undefined;
        this.forceUpdate();
    }


    public addNewSubPageComponetSelf = (pageKey: string,
        args: object | undefined,
        customPayload: object | undefined,
        finedInRows: boolean,
        ExtraStackMenuComponent: React.ComponentType<{ mainState: any }> | undefined,
        typeOfForm: ITypeOfFormForRoute | undefined
    ) => {
        const Component = this.mainStateFactory.mainStateManager.pageLoadKeyFactory.get(pageKey);
        const result = new SubPageItem(this, pageKey, undefined, Component.Page);

        if (typeof args === 'number') {
            result.argument = Argument.Id(args, finedInRows);
        } else if (typeof args === 'string') {
            result.argument = Argument.Code(args, finedInRows);
        } else {
            result.argument = Argument.Object(args, finedInRows);
        }

        result.argument.typeOfForm = typeOfForm;
        result.argument.ExtraStackMenuComponent = ExtraStackMenuComponent || Component.ExtraPageToolStripItems;
        result.argument.customPayload = customPayload;

        this.subPages.push(result);
        this.subPagesOrder.push(result);
        this.activeSubPage = result;

        this.forceUpdate();
    }

    /**
 * 
 * @param pageKey 
 * @param args add itemsPerPage to object of argument to pass to page parameter of next page :)
 */
    public addNewSubPageComponet: {
        (pageKey: string,): void;
        (pageKey: string, id: number): void;
        (pageKey: string, id: number, customPayload: any): void;
        (pageKey: string, id: number, customPayload: any, finedInRows: boolean): void;
        (pageKey: string, code: string): void;
        (pageKey: string, code: string, customPayload: any): void;
        (pageKey: string, code: string, customPayload: any, finedInRows: boolean): void;
        (pageKey: string, object: any): void;
        (pageKey: string, object: any, customPayload: any): void;
        (pageKey: string, object: any, customPayload: any, finedInRows: boolean): void;
        (pageKey: string, object: any, customPayload: any, finedInRows: boolean, ExtraStackMenuComponent?: React.ComponentType<{ mainState: any }>, typeOfForm?: ITypeOfFormForRoute | undefined): void;
    } = (pageKey: string, args?: number | string | object, customPayload?: any, finedInRows?: boolean, ExtraStackMenuComponent?: React.ComponentType<{ mainState: any }>, typeOfForm?: ITypeOfFormForRoute | undefined) => {
        const Component = this.mainStateFactory.mainStateManager.pageLoadKeyFactory.get(pageKey);
        const result = new SubPageItem(this, pageKey, undefined, Component.Page);


        if (typeof args === 'number') {
            result.argument = Argument.Id(args, finedInRows);
        } else if (typeof args === 'string') {
            result.argument = Argument.Code(args, finedInRows);
        } else {
            result.argument = Argument.Object(args, finedInRows);
        }

        result.argument.typeOfForm = typeOfForm;
        result.argument.ExtraStackMenuComponent = ExtraStackMenuComponent || Component.ExtraPageToolStripItems;
        result.argument.customPayload = customPayload;

        this.subPages.push(result);
        this.subPagesOrder.push(result);
        this.activeSubPage = result;

        this.forceUpdate();
    }

    /**
     * 
     * @param pageKey 
     * @param argument add itemsPerPage to object of argument to pass to page parameter of next page :)
     */
    public addNewSubPage: {
        (pageKey: string): void;
        (pageKey: string, id: number): void;
        (pageKey: string, id: number, finedInRows: boolean): void;
        (pageKey: string, code: string): void;
        (pageKey: string, code: string, finedInRows: boolean): void;
        (pageKey: string, object: any): void;
        (pageKey: string, object: any, finedInRows: boolean): void;
    } = (pageKey: string, argument?: number | string | object, finedInRows?: boolean) => {

        const Component = this.mainStateFactory.mainStateManager.lazyLoadFactory.get(pageKey);

        const result = new SubPageItem(this, pageKey, Component, undefined);
        if (typeof argument === 'number') {
            result.argument = Argument.Id(argument, finedInRows);
        } else if (typeof argument === 'string') {
            result.argument = Argument.Code(argument, finedInRows);
        } else if (typeof argument === 'object') {
            result.argument = Argument.Object(argument, finedInRows);
        }

        this.subPages.push(result);
        this.subPagesOrder.push(result);
        this.activeSubPage = result;
        this.forceUpdate();
    }
    selectThisSubPageItem = (subPageItem: SubPageItem) => {
        const newSubPage = this.subPagesOrder.filter((t) => t !== subPageItem);
        newSubPage.push(subPageItem);
        this.subPagesOrder = newSubPage;

        this.activeSubPage = subPageItem;

        this.forceUpdate();

    }
    closeThisSubPageItem = (subPageItem: SubPageItem) => {
        this.subPages = this.subPages.filter((t) => t !== subPageItem);
        this.subPagesOrder = this.subPagesOrder.filter((t) => t !== subPageItem);

        if (this.subPagesOrder.length > 0) {
            this.activeSubPage = this.subPagesOrder[this.subPagesOrder.length - 1];
        } else {
            this.activeSubPage = undefined;
        }

        this.forceUpdate();
    }
}


