import { BaseSelfControl } from './BaseSelfControl';
import { ValidationChain, ValidationEventArgs } from '../chainOfResponsibility';
import { EnumValidateState } from '../enums';

export class SelfNumber extends BaseSelfControl<
  number | null,
  number | null,
  number | null,
  HTMLInputElement
> {
  minValue: number | undefined;
  maxValue: number | undefined;

  private validateNormal = (eventArgs: ValidationEventArgs) => { };
  private validateRequired = (eventArgs: ValidationEventArgs) => {
    if (this.required) {
      if (typeof this.value !== 'number') {
        eventArgs.error = 'اطلاعاتی وارد نشده است';
        eventArgs.state = EnumValidateState.empty;
        eventArgs.cancel = true;
      }
    }
  };
  private validateMinValue = (eventArgs: ValidationEventArgs) => {
    if (this.minValue) {
      if (typeof this.value !== 'number') {
        eventArgs.error = `هیچ مقداری وارد نشده است. حداقل مقدار مجاز:  ${this.minValue}`;
        eventArgs.state = EnumValidateState.minValue;
        eventArgs.cancel = true;
      } else if (this.value < this.minValue) {
        eventArgs.error = `مقدار وارد شده کمتر از مقدار مجاز میباشد. حداقل مقدار مجاز: ${this.minValue}`;
        eventArgs.state = EnumValidateState.minValue;
        eventArgs.cancel = true;
      }
    }
  };
  private validateMaxValue = (eventArgs: ValidationEventArgs) => {
    if (this.maxValue) {
      if (typeof this.value === 'number' && this.value > this.maxValue) {
        eventArgs.error = `مقدار وارد شده بیشتر از مقدار مجاز میباشد. حداکثر مقدار مجاز: ${this.maxValue}`;
        eventArgs.state = EnumValidateState.maxValue;
        eventArgs.cancel = true;
      }
    }
  };


  isValueEmpty = () => {
    return typeof this.#value === 'number';
  }
  isValueNotEmpty = () => {
    return !this.isValueEmpty();
  }

  validate = () => {
    if (this.hidden) {
      return true;
    }

    var validationChain = new ValidationChain();

    validationChain.validators.add(this.validateNormal);
    validationChain.validators.add(this.validateRequired);
    validationChain.validators.add(this.validateMinValue);
    validationChain.validators.add(this.validateMaxValue);

    this.validation = validationChain.validate();
  };

  public cleaningClassInitializer = () => {
    this.timerOfIncorrectChar && clearTimeout(this.timerOfIncorrectChar);
    this.hasChange = false;
    this.defaultValue = undefined;
    this.initializeListener = false;
    this.initializeProperties = false;
    this.validation = true;
  };

  public refreshHasChange = () => {
    if (this.showHasChangeFlag) {
      this.hasChange = this.defaultValue !== this.#value;
    }
  };
  public restartDefaultValue = () => {
    this.defaultValue = this.value;
    this.refreshHasChange();
  };

  #value: number | null;
  public get value() {
    return this.#value;
  }
  public set value(value: any) { }

  public setValue = (value: number | null) => {
    this.#value = value;
    this.refreshHasChange();
  };

  public deserialize = (value: number | null) => {
    this.#value = value;
    this.restartDefaultValue();
  };

  constructor(value: number | null) {
    super();
    this.#value = value;
    this.restartDefaultValue();
  }

  static empty(): SelfNumber {
    return new SelfNumber(null);
  }
  static deserialize(value?: number | null): SelfNumber {
    return new SelfNumber(value || null);
  }
}
