import { MainStateManager } from "../../MainStateManager";
import { ElementsOfFormFactory } from "../../Page/ElementsOfFormFactory";
import { IMainStateFactory } from "../../Types";
import { ToolStripItemFactory } from "./ToolStripItemFactory";


export class ToolStripFactory {
    public refreshDisabled = () => {
        if (this._toolStripItems && this._toolStripItems.length) {
            this._toolStripItems.forEach(item => {
                if (typeof item.refreshDisabled === 'function') {
                    item.disabled = item.refreshDisabled();
                }
            });
        }
    }
    public refreshVisible = () => {
        if (this._toolStripItems && this._toolStripItems.length) {
            this._toolStripItems.forEach(item => {
                if (typeof item.refreshVisible === 'function') {
                    item.visible = item.refreshVisible();
                }
            });
        }
    }
    public forceUpdateToolStripButtonItems = () => { };
    public forceUpdateToolStripItem = () => { };
    public forceUpdateToolStackMenu = () => { };
    public get any(): any {
        return this;
    }
    public mainStateManager: MainStateManager;
    private _toolStripItems: ToolStripItemFactory[];
    public get toolStripItems(): ToolStripItemFactory[] {
        return this._toolStripItems;
    }
    public addToolStripItem = (item: ToolStripItemFactory) => {
        this._toolStripItems.push(item);
        this._toolStripItems.sort((a, b) => a.tabIndex - b.tabIndex);

        if (typeof item.tabIndex === 'number') {
            this.elementsOfFormFactory.tabbing.toolboxTabbing.mainFocusAbleToolbox.push(item);
            this.elementsOfFormFactory.tabbing.toolboxTabbing.mainFocusAbleToolbox.sort((a, b) => a.tabIndex - b.tabIndex);
        }

    }
    public removeToolStripItem = (icon: ToolStripItemFactory) => {
        delete this.any[icon.factoryFieldName];
        this._toolStripItems = this._toolStripItems.filter(i => i !== icon);
        this.elementsOfFormFactory.tabbing.toolboxTabbing.mainFocusAbleToolbox = this.elementsOfFormFactory.tabbing.toolboxTabbing.mainFocusAbleToolbox.filter(i => i !== icon);
        this._toolStripItems.sort((a, b) => a.tabIndex - b.tabIndex);
    }

    public mainStateFactory: IMainStateFactory;
    constructor(
        public elementsOfFormFactory: ElementsOfFormFactory,
    ) {
        this.mainStateFactory = this.elementsOfFormFactory.mainStateFactory;
        this.mainStateManager = this.elementsOfFormFactory.mainStateManager;
        this._toolStripItems = [];
    }

    public focusToThisToolStripItem = (toolStripItem: ToolStripItemFactory) => {
        this.elementsOfFormFactory.tabbing.toolboxTabbing.focuseToThisToolStripItem(toolStripItem);
    }
}