import {
  Model,
  model,
  modelAction,
  ModelAutoTypeCheckingMode,
  setGlobalConfig,
  tProp,
  types,
} from "mobx-keystone";
import { v4 as uuidv4 } from "uuid";
import { toJS } from "mobx";

setGlobalConfig({
  modelAutoTypeChecking: ModelAutoTypeCheckingMode.AlwaysOn,
});
@model("keystone/Application/ScannedTicket")
export default class ScannedTicket extends Model({
  id: tProp(types.string, () => uuidv4()),
  idTicket: tProp(types.number),
  version: tProp(types.number),
  startType: tProp(types.number),
  startTime: tProp(types.number),
  endType: tProp(types.number),
  endTime: tProp(types.number),
  credits: tProp(types.number).withSetter(),
  check: tProp(types.string, ""),
  qrData: tProp(types.string),
}) {
  onInit = () => {};

  get all() {
    return this;
  }

  returnAllJSON() {
    const object = {
      startTime: this.startTime,
      endTime: this.endTime,
      id: this.id,
      credits: this.credits,
      qrData: this.qrData,
    };
    return object;
  }

  returnStringArray() {
    const stringArrayFromObject = `[${this.idTicket.toString()},0,${
      this.startType
    },${this.startTime},${this.endType},${this.endTime},${this.credits.toFixed(
      2
    )}]`;

    return stringArrayFromObject;
  }

  @modelAction
  returnSignedString() {
    const stringArrayFromObject = `[${this.idTicket.toString()},0,${
      this.startType
    },${this.startTime},${this.endType},${this.endTime},${this.credits.toFixed(
      2
    )},"${this.check}"]`;

    return stringArrayFromObject;
  }

  get allPlain() {
    return toJS(this);
  }

  @modelAction
  setStartTime(startTime: number) {
    this.startTime = startTime;
  }

  @modelAction
  setCheck(check: string) {
    this.check = check;
  }

  @modelAction
  setEndTime(endTime: number) {
    this.endTime = endTime;
  }

  @modelAction
  changeCheck(data: string) {
    this.check = data;
  }
}
