import { ValidationChain, ValidationEventArgs } from "../chainOfResponsibility";
import { EnumValidateState } from "../enums";
import { ElementFactory } from "../Page/ElementsOfFormFactory/ElementFactory";
import { IMainStateFactory } from "../Types";
import { persianCharToEnglishNumber } from "nums2persian";
import { Cach } from "./Cach";
export type ITypeOfUserBox = 'code' | 'englishName' | 'farsiName' | 'email';

export class UserBoxFactory extends ElementFactory {
    public forceUpdate = () => { };

    public value: string | null = null;
    public setValue = (value: string) => {
        if (typeof value !== 'string') {
            this.value = null;
            this.forceUpdate();
        } else if (this.type === 'code') {
            this.setValueCodeUserName(value);
        } else if (this.type === 'englishName') {
            this.setValueEnglishUserName(value);
        } else if (this.type === 'farsiName') {
            this.setValueFarsiUserName(value);
        } else if (this.type === 'email') {
            this.setValueEmailUserName(value);
        }

        Cach.setValue(this, value);

    }
    private setValueCodeUserName = (value: string) => {
        let newValue = value.split('').map(i => '۱۲۳۴۵۶۷۸۹۰'.includes(i) ? persianCharToEnglishNumber(i).toString() : i).join('');

        const match = newValue.match(/[0-9]/g) || [];
        newValue = match.join('');

        this.value = newValue;
        this.forceUpdate();
    }
    private setValueEnglishUserName = (value: string) => {
        let newValue = value.split('').map(i => '۱۲۳۴۵۶۷۸۹۰'.includes(i) ? persianCharToEnglishNumber(i).toString() : i).join('');

        const match = newValue.match(/[a-zA-Z0-9_]/g) || [];
        newValue = match.join('');

        this.value = newValue;
        this.forceUpdate();
    }
    private setValueEmailUserName = (value: string) => {
        let newValue = value.split('').map(i => '۱۲۳۴۵۶۷۸۹۰'.includes(i) ? persianCharToEnglishNumber(i).toString() : i).join('');

        const match = newValue.match(/[a-zA-Z0-9@._]/g) || [];
        newValue = match.join('');

        this.value = newValue;
        this.forceUpdate();
    }
    private setValueFarsiUserName = (value: string) => {
        this.value = value;
        this.forceUpdate();
    }

    public clearData: () => void = () => {
        Cach.clear(this);

    }
    public deseriallize: () => void = () => {

    }
    public refreshHasChange: () => void = () => {

    }
    public restartDefaultValue: () => void = () => {

    }

    constructor(
        mainStateFactory: IMainStateFactory,
        fieldName: string,
        _dispose: () => void,
        public caption: string,
        public placeHolder?: string,
        public tabIndex?: number,
        public type: ITypeOfUserBox = 'englishName',
        payLoadKey?: string,
        responseKey?: string,
    ) {
        super(mainStateFactory, fieldName, _dispose, payLoadKey, responseKey);


        if (Cach.isCached(this)) {
            const cach = Cach.getCached(this);
            this.value = cach.value;
            this.validation = cach.validation;
            this._hasChange = cach.hasChange;
        }
    }

    public validate = () => {
        var validationChain = new ValidationChain();

        validationChain.validators.add(this.validateRequired);

        this.validation = validationChain.validate();
    }
    private validateRequired = (eventArgs: ValidationEventArgs) => {
        if (!this.value) {
            eventArgs.error = 'اطلاعاتی وارد نشده است';
            eventArgs.state = EnumValidateState.empty;
            eventArgs.cancel = true;
        }
    };
}