import { BaseSelfControl } from './BaseSelfControl';
import { ValidationEventArgs, ValidationChain } from '../chainOfResponsibility';
import { EnumValidateState } from '../enums';

export interface IObject {
  id: number;
}
export interface IObjectJson {
  id: number;
}

export class SelfObject<
  V extends IObject,
  J extends IObjectJson
  > extends BaseSelfControl<
  V,
  J,
  number,
  HTMLInputElement
  > {

  private validateNormal = (eventArgs: ValidationEventArgs) => {
  };
  private validateRequired = (eventArgs: ValidationEventArgs) => {
    if (this.required) {
      if (!this.value.id) {
        eventArgs.error = 'اطلاعاتی وارد نشده است';
        eventArgs.state = EnumValidateState.empty;
        eventArgs.cancel = true;
      }
    }
  };

  isValueEmpty = () => {
    return typeof this.#value === 'object' && this.#value.id !== 0;
  }
  isValueNotEmpty = () => {
    return !this.isValueEmpty();
  }

  validate = () => {
    var validationChain = new ValidationChain();

    validationChain.validators.add(this.validateNormal);
    validationChain.validators.add(this.validateRequired);

    this.validation = validationChain.validate();
  };

  public cleaningClassInitializer = () => {
    this.timerOfIncorrectChar && clearTimeout(this.timerOfIncorrectChar);
    this.hasChange = false;
    this.defaultValue = undefined;
    this.initializeProperties = false;
    this.initializeListener = false;
    this.validation = true;
  };

  public refreshHasChange = () => {
    if (this.showHasChangeFlag) {
      this.hasChange = this.defaultValue !== this.#value.id;
    }
  };
  public restartDefaultValue = () => {
    this.defaultValue = this.value.id;
    this.refreshHasChange();
  };

  #value: V;
  public get value() {
    return this.#value;
  }
  public set value(value: V) { }

  public setValue = (value: V) => {
    this.#value = value;
    this.refreshHasChange();
  };

  public deserialize = (value: J) => {
    this.#value = this._deserialize(value);
    this.restartDefaultValue();
  };

  constructor(
    private _deserialize: (json?: J) => V,
    value: V
  ) {
    super();
    this.#value = value;
    this.restartDefaultValue();
  }

  static empty<V extends IObject, J extends IObjectJson>(deserialize: (json?: J) => V): SelfObject<V, J> {
    return new SelfObject(deserialize, deserialize());
  }
}
