
import { EnumValidateState } from "../../enums";
import { ElementFactory } from "../../Page/ElementsOfFormFactory/ElementFactory";
import { IMainStateFactory } from "../../Types";
import { Cach } from "../Cach";
import { ButtonGroupFactory } from "./ButtonGroupFactory";


export class ButtonGroupsFactory extends ElementFactory {
    public items: ButtonGroupFactory[] = [];

    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,
        private onChange?: (e: ButtonGroupsFactory) => void,
    ) {
        super(_mainStateFactory, _fieldName, _dispose, payLoadKey, responseKey);

        if (typeof defaultValue === 'number') {
            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;
        return this.validation;
    };

    private defaultValue: string | number | undefined | null = null;

    restartDefaultValue = () => {
        this.defaultValue = this._value;
        this.refreshHasChange();
    };
    refreshHasChange = () => {
        if (this.showHasChangeFlag) {
            this._hasChange = this.defaultValue !== this._value;
        }
    };

    _value: string | number | undefined | null = null;

    get value(): string | number | undefined | null {
        return this._value;
    }
    setValue(value: string | number | undefined | null) {
        this._value = value;
        Cach.setValue(this, value);
        this.forceUpdate();
        if (this.onChange) {
            setTimeout(() => {
                this.onChange && this.onChange(this);
            }, 10);
        }
    }
    public deseriallize = (e?: string | number | undefined | null) => {
        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 = null;
        this.defaultValue = null;
        this.validation = true;
        this._hasChange = false;
    };
}
