import { AxiosInstance } from "axios";
import { MouseLocationOfPopup } from "../../ComponentFactory/PopupFactory";
import { MainStateManager } from "../../MainStateManager";
import { IMainStateFactory } from "../../Types";
import { IResultOfValidate } from "../ValidatingControl";


export abstract class ElementFactory {
    public get any(): any {
        return this;
    }
    public mainStateManager: MainStateManager;
    public element?: HTMLElement;
    public isCodeCopied?: boolean;


    protected _hasChange: boolean = false;
    public get hasChange() {
        return this._hasChange;
    }

    public disabled: boolean = false;
    public hidden: boolean = false;

    public abstract restartDefaultValue: () => void;
    public abstract refreshHasChange: () => void;
    public abstract validate: () => void;
    public abstract forceUpdate: () => void;
    public abstract tabIndex?: number;
    public abstract deseriallize: (jsonValue: any) => void;
    public abstract clearData: () => void;
    public abstract get value(): any;

    public validation: IResultOfValidate = true;

    constructor(
        public mainStateFactory: IMainStateFactory,
        public factoryFieldName: string,
        public dispose: () => void,
        public payLoadKey?: string,
        public responseKey?: string,
        public history?: {
            key: string,
            axios: (controllerPath: string) => AxiosInstance,
            controllerPath: string,
            actionPath?: string,
        }
    ) {
        if (this.mainStateFactory.any.cach === undefined) {
            this.mainStateFactory.any.cach = {};
        }
        this.mainStateManager = this.mainStateFactory.mainStateManager;

        if (typeof responseKey === 'string') {
            this.mainStateFactory.elementsOfForm.addResponseElement(this);
        }
        if (typeof payLoadKey === 'string') {
            this.mainStateFactory.elementsOfForm.addPayloadElement(this);
        }
    }



    public showHistoryOfColumn = (JSX: JSX.Element) => {
        if (!this.history) {
            return;
        }
        let mouseLocationOfPopup;
        if (this.element) {
            const rect = this.element.getBoundingClientRect();
            mouseLocationOfPopup = new MouseLocationOfPopup(rect.left + window.scrollX, rect.top + window.scrollY);
        }
        this.mainStateFactory.elementsOfForm.showPopup(JSX, mouseLocationOfPopup);
    }
}