import { ValidationChain, ValidationEventArgs } from "../chainOfResponsibility";
import { EnumValidateState } from "../enums";
import { ElementFactory } from "../Page/ElementsOfFormFactory/ElementFactory";
import { IMainStateFactory } from "../Types";
import { Cach } from "./Cach";

export class MultiSelectIdFactory extends ElementFactory {
    public forceUpdate = () => { };
    private _value: number[] | null;
    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) => {
        this.clearData();
        if (e === null) {
            //do nothing
        } else if (e === undefined) {
            //do nothing
        } else if (typeof e === 'object') {
            this._value = e;
            this._defaultValue = this._value;
        } else {
            this._value = e;
            this._defaultValue = this._value;
        }
        Cach.setValue(this, this._value);
    }

    public clearData = () => {
        Cach.clear(this);
        this._value = null;
        this._defaultValue = null;
        this.validation = true;
        this._hasChange = false;
    }

    private _defaultValue: number[] | null;
    public get defaultValue() {
        return this._defaultValue;
    }

    public toggle = (id: number) => {
        if (!this._value) {
            this._value = [];
        }

        if (this._value.some(i => i === id)) {
            this._value = this._value.filter(i => i !== id);
        } else {
            this._value.push(id);
        }
        this.forceUpdate();
    }

    isSelected: {
        (id: number): boolean;
        (object: { id: number }): boolean;
    } = (arg: number | { id: number }): boolean => {
        let id: number;
        if (typeof arg === 'number') {
            id = arg;
        } else {
            id = arg.id;
        }

        if (!this._value) {
            return false;
        }

        if (this._value.some(i => i === id)) {
            return true;
        }
        return false;
    }

    public focusToElement = () => {
        this.mainStateFactory.elementsOfForm.focuseToThisElement(this);
    }

    constructor(
        _mainStateFactory: IMainStateFactory,
        _fieldName: string,
        _dispose: () => void,
        public caption: string,
        public disabled: boolean,
        public readonly: boolean,
        public required: boolean,
        public showHasChangeFlag: boolean,
        public dirLeftToRight: boolean,
        public placeHolder?: string,
        public tabIndex?: number,
        initialValue?: number[],
        payLoadKey?: string,
        responseKey?: string,
    ) {
        super(_mainStateFactory, _fieldName, _dispose, payLoadKey, responseKey);

        if (initialValue && initialValue instanceof Array) {
            this._defaultValue = initialValue;
            this._value = initialValue;
        } else {
            this._defaultValue = null;
            this._value = null;
        }

        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);
        validationChain.validators.add(this.validateRequired);

        this.validation = validationChain.validate();
    }

    private validateNormal = (_eventArgs: ValidationEventArgs) => { };
    private validateRequired = (eventArgs: ValidationEventArgs) => {
        if (this.required) {
            if (!this.value) {
                eventArgs.error = 'اطلاعاتی وارد نشده است';
                eventArgs.state = EnumValidateState.empty;
                eventArgs.cancel = true;
            }
        }
    };

    isValueEmpty = () => {
        return typeof this._value === 'string' && this._value !== '';
    }
    isValueNotEmpty = () => {
        return !this.isValueEmpty();
    }
}