import {
  model,
  Model,
  modelAction,
  ModelAutoTypeCheckingMode,
  setGlobalConfig,
  tProp,
  types,
} from "mobx-keystone";
import TicketModel from "./TicketModel";

setGlobalConfig({
  modelAutoTypeChecking: ModelAutoTypeCheckingMode.AlwaysOn,
});
@model('keystone/Application/TicketList')
export default class TicketList extends Model({
  ticketModels: tProp(
    types.array(types.model<TicketModel>(TicketModel)),
    () => []
  ),
  currentEditId: tProp(types.string, '0'),
  defaultStart: tProp(types.number, () => {
    const date = new Date();
    date.setHours(8);
    date.setMinutes(0);
    return date.getTime();
  }),
  defaultEnd: tProp(types.number, () => {
    const date = new Date();
    date.setHours(20);
    date.setMinutes(0);
    return date.getTime();
  }),
}) {
  @modelAction
  getTicketById(id: string) {
    return this.ticketModels.find((x) => x.id === id);
  }

  @modelAction
  add(ticket: TicketModel) {
    this.ticketModels.push(ticket);
  }

  @modelAction
  setCurrentEdit(id: string) {
    this.currentEditId = id;
  }

  get currentEdit() {
    return this.currentEditId;
  }

  @modelAction
  remove(ticket: TicketModel) {
    const index = this.ticketModels.indexOf(ticket);
    if (index >= 0) {
      this.ticketModels.splice(index, 1);
    }
  }

  get all() {
    return this.ticketModels;
  }

  @modelAction
  setDefaultStart(date: Date) {
    this.defaultStart = +date;
  }

  @modelAction
  setDefaultEnd(date: Date) {
    this.defaultEnd = +date;
  }
}
