import { BaseSelfControl } from './BaseSelfControl';
import { ValidationEventArgs, ValidationChain } from '../chainOfResponsibility';
import { EnumValidateState } from '../enums';

export class SelfProfileSectionInfo<TYPE> extends BaseSelfControl<
  Array<TYPE>,
  Array<TYPE>,
  string,
  HTMLDivElement
> {

  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();
  }

  validate = () => {
    var validationChain = new ValidationChain();

    validationChain.validators.add(this.validateNormal);
    validationChain.validators.add(this.validateRequired);

    this.validation = validationChain.validate();
  };

  public cleaningClassInitializer = () => {
    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.join('');
    }
  };
  public restartDefaultValue = () => {
    this.defaultValue = this.#value.join('');
    this.refreshHasChange();
  };

  #value: Array<TYPE>;
  public get value() {
    return this.#value;
  }
  public set value(value: Array<TYPE>) { }

  public setValue = (value: Array<TYPE>) => {
    this.#value = value;
    this.refreshHasChange();
  };

  public deserialize = (value: Array<TYPE>) => {
    this.#value = value;
    this.restartDefaultValue();
  };

  constructor(value: Array<TYPE>) {
    super();
    this.#value = value;
    this.restartDefaultValue();
  }

  static empty<TYPE>(): SelfProfileSectionInfo<TYPE> {
    return new SelfProfileSectionInfo<TYPE>([]);
  }
  static deserialize<TYPE>(value?: Array<TYPE>): SelfProfileSectionInfo<TYPE> {
    return new SelfProfileSectionInfo<TYPE>(value || []);
  }
}
