import { EnumValidateState } from "../enums";
import { ElementFactory } from "../Page/ElementsOfFormFactory/ElementFactory";
import { IMainStateFactory } from "../Types";
import { Cach } from "./Cach";


export class ComboBoxFactory extends ElementFactory {
    public get any() {
        return this as any;
    }

    public forceUpdate: () => void = () => { };

    constructor(
        _mainStateFactory: IMainStateFactory,
        _fieldName: string,
        _dispose: () => void,
        public required: boolean,
        public caption: string,
        public showHasChangeFlag: boolean,
        public tabIndex?: number,
        public payLoadKey?: string,
        responseKey?: string,
        defaultValue?: number | string | undefined,
        private onChange?: (e: ComboBoxFactory) => void,
    ) {
        super(_mainStateFactory, _fieldName, _dispose, payLoadKey, responseKey);

        if (typeof defaultValue === 'number' || typeof defaultValue === 'string') {
            this.defaultValue = defaultValue;
            this._value = defaultValue;
        }

        if (Cach.isCached(this)) {
            const cach = Cach.getCached(this);
            this._value = cach.value;
            this.validation = cach.validation;
            this._hasChange = cach.hasChange;
        }
    }


    public focusToElement = () => {
        this.mainStateFactory.elementsOfForm.focuseToThisElement(this);
    };

    validate = () => {
        if (this.required && !this._value) {
            // 'namingNotImplement'
            this.validation = [`${this.caption} وارد نشده است`, EnumValidateState.empty];
            return this.validation;
        }
        this.validation = true;
    };

    private defaultValue: number | string | undefined = undefined;

    restartDefaultValue = () => {
        this.defaultValue = this._value;
        this.refreshHasChange();
    };
    refreshHasChange = () => {
        if (this.showHasChangeFlag) {
            this._hasChange = this.defaultValue !== this._value;
        }
    };

    _value: number | string | undefined = undefined;

    get value(): number | string | undefined {
        return this._value;
    }
    setValue(value: number | string | undefined) {
        this._value = value;
        Cach.setValue(this, value);
        this.forceUpdate();
        if (this.onChange) {
            setTimeout(() => {
                this.onChange && this.onChange(this);
            }, 10);
        }
    }
    public deseriallize = (e?: number | string | undefined) => {
        this.clearData();
        if (e === null) {
            //do nothing
        } else if (e === undefined) {
            //do nothing
        } else {
            this._value = e;
            this.defaultValue = this._value;
        }
    };

    public clearData = () => {
        Cach.clear(this);
        this._value = undefined;
        this.defaultValue = undefined;
        this.validation = true;
        this._hasChange = false;
    };
}
