import { BaseSelfControl } from './BaseSelfControl';
import { IControlSimulateKey } from './../Page/Events';

export type ISelfCheckTree = 0 | 1 | 2 | 3 | 4;

export interface INewProperties {
  propertyName: string;
  onChangePropertyName: string | undefined;
  controlRef: React.RefObject<HTMLInputElement>;
  caption: string;
  showHasChangeFlag: boolean;
  onErrorSimulateKey?: IControlSimulateKey;
}

export class SelfCheckTree extends BaseSelfControl<
  ISelfCheckTree,
  ISelfCheckTree,
  ISelfCheckTree,
  HTMLDivElement
> {

  isValueEmpty = () => {
    return typeof this.#value === 'number';
  }
  isValueNotEmpty = () => {
    return !this.isValueEmpty();
  }

  public validate = () => {
    this.validation = true;
  };

  public cleaningClassInitializer = () => {
    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: ISelfCheckTree;
  public get value() {
    return this.#value;
  }
  public set value(value: ISelfCheckTree) { }
  public setValue = (value: ISelfCheckTree) => {
    this.#value = value;
    this.refreshHasChange();
  };

  public deserialize = (value: ISelfCheckTree) => {
    this.#value = value;
    this.restartDefaultValue();
  };

  constructor(value: ISelfCheckTree) {
    super();
    this.#value = value;
    this.restartDefaultValue();
  }
  static empty(): SelfCheckTree {
    return new SelfCheckTree(0);
  }
  static deserialize(value: ISelfCheckTree | null): SelfCheckTree {
    if (!value) {
      return new SelfCheckTree(0);
    } else if ([0, 1, 2, 3, 4].includes(value)) {
      return new SelfCheckTree(value as ISelfCheckTree);
    } else {
      return new SelfCheckTree(0);
    }
  }
}
