import { ValidationChain, ValidationEventArgs } from "../chainOfResponsibility";
import { ElementFactory } from "../Page/ElementsOfFormFactory/ElementFactory";
import { IMainStateFactory } from "../Types";
import { Cach } from "./Cach";

export class CheckBoxTreeFactory extends ElementFactory {
    public forceUpdate = () => { };
    #value: number;
    public get value() {
        return this.#value;
    }

    public setValue = (value: number) => {
        this.#value = value;
        this.refreshHasChange();
        Cach.setValue(this, this.#value);
        this.forceUpdate();
    };

    public deseriallize = (e: any) => {
        const deseriallizeNumber = (numberValue: number) => {
            if (numberValue === 1) {
                this.#defaultValue = 1;
                this.#value = 1;
            } else if (numberValue === 2) {
                this.#defaultValue = 2;
                this.#value = 2;
            } else if (numberValue === 3) {
                this.#defaultValue = 3;
                this.#value = 3;
            } else if (numberValue === 4) {
                this.#defaultValue = 4;
                this.#value = 4;
            } else {
                this.#defaultValue = 0;
                this.#value = 0;
            }
        }

        this.clearData();
        if (e === null) {
            //do nothing
        } else if (e === undefined) {
            //do nothing
        } else if (typeof e === 'number') {
            deseriallizeNumber(e);
        } else if (typeof e === 'string') {
            const numberValue = +e;
            if (!isNaN(numberValue)) {
                deseriallizeNumber(numberValue);
            }
        }

        Cach.setValue(this, this.#value);
    }

    public clearData = () => {
        Cach.clear(this);
        this.#value = 0;
        this.#defaultValue = 0;
        this.validation = true;
        this._hasChange = false;
    }

    #defaultValue: number;
    public get defaultValue() {
        return this.#defaultValue;
    }

    public focusToElement = () => {
        this.mainStateFactory.elementsOfForm.focuseToThisElement(this);
    }

    constructor(
        _mainStateFactory: IMainStateFactory,
        _fieldName: string,
        _dispose: () => void,
        public caption: string,
        public disabled: boolean,
        public readonly: boolean,
        public showHasChangeFlag: boolean,
        public dirLeftToRight: boolean,
        public tabIndex?: number,
        payLoadKey?: string,
        responseKey?: string,
    ) {
        super(_mainStateFactory, _fieldName, _dispose, payLoadKey, responseKey);
        this.#value = 0;
        this.#defaultValue = 0;

        if (Cach.isCached(this)) {
            const cach = Cach.getCached(this);
            this.#value = cach.value;
            this.validation = cach.validation;
            this._hasChange = cach.hasChange;
        }
    }

    public restartDefaultValue = () => {
        this.#defaultValue = this.value;
        this.refreshHasChange();
    };

    public refreshHasChange = () => {
        if (this.showHasChangeFlag) {
            this._hasChange = this.defaultValue !== this.#value;
        }
    };

    public validate = () => {
        var validationChain = new ValidationChain();

        validationChain.validators.add(this.validateNormal);

        this.validation = validationChain.validate();
    }

    private validateNormal = (_eventArgs: ValidationEventArgs) => { };

    isValueEmpty = () => {
        return typeof this.#value === 'string' && this.#value !== '';
    }
    isValueNotEmpty = () => {
        return !this.isValueEmpty();
    }
}