import { Tabbing } from ".";
import { ElementsOfFormFactory } from "..";
import { DateBoxFactory } from "../../../ComponentFactory/DateBox/DateBoxFactory";
import { StructrulCodeFactory } from "../../../ComponentFactory/StructructrulCode/StructrulCodeFactory";
import { EnumValidateState } from "../../../enums";
import { ElementFactory } from "../ElementFactory";

export class validationTabbing {
    static gotoNextElement(tabbing: Tabbing, currentElementFactory: ElementFactory) {
        const othersTabbing = new validationTabbing(tabbing, currentElementFactory);
        othersTabbing.gotoNextElement()
    }
    public elementsOfForm: ElementsOfFormFactory;
    constructor(
        public tabbing: Tabbing,
        public currentElementFactory: ElementFactory
    ) {
        this.elementsOfForm = tabbing.elementsOfFormFactory;
    }

    public gotoNextElement = () => {
        if (this.currentElementFactory instanceof DateBoxFactory) {
            this.goToNextElementDataBox(this.currentElementFactory);
        } else if (this.currentElementFactory instanceof StructrulCodeFactory) {
            this.goToNextElementStructrulCode(this.currentElementFactory);
        } else {
            this.tabbing.elementTabbing.focusNextElement();
        }
    }

    goToNextElementDataBox = (currentDateBoxFactory: DateBoxFactory) => {
        currentDateBoxFactory.validate();
        if (typeof currentDateBoxFactory.validation !== 'boolean') {
            if (currentDateBoxFactory.validation[1] === EnumValidateState.empty) {
                currentDateBoxFactory.showDatePicker && currentDateBoxFactory.showDatePicker();
            } else {
                if (currentDateBoxFactory.showDatePicker) {
                    const showDatePicker = () => {
                        currentDateBoxFactory.any.showDatePicker();
                        delete this.elementsOfForm.onCloseModalOpenNexWindow;
                    }
                    this.elementsOfForm.onCloseModalOpenNexWindow = showDatePicker;
                }
                this.elementsOfForm.showInvalidArgumentMessageBox(currentDateBoxFactory.validation[0]);

            }
        } else {
            this.tabbing.elementTabbing.focusNextElement();
        }
    }




    goToNextElementStructrulCode = (currentStructrulCodeFactory: StructrulCodeFactory) => {
        currentStructrulCodeFactory.validate();
        if (typeof currentStructrulCodeFactory.validation !== 'boolean') {
            if (currentStructrulCodeFactory.validation[1] === EnumValidateState.empty) {
                currentStructrulCodeFactory.showTreeView && currentStructrulCodeFactory.showTreeView();
            } else {
                if (currentStructrulCodeFactory.showTreeView) {
                    const showTreeView = () => {
                        currentStructrulCodeFactory.any.showTreeView();
                        delete this.elementsOfForm.onCloseModalOpenNexWindow;
                    }
                    this.elementsOfForm.onCloseModalOpenNexWindow = showTreeView;
                }
                this.elementsOfForm.showInvalidArgumentMessageBox(currentStructrulCodeFactory.validation[0]);

            }
        } else {
            this.tabbing.elementTabbing.focusNextElement();
        }

    }
}

