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

setGlobalConfig({
  modelAutoTypeChecking: ModelAutoTypeCheckingMode.AlwaysOn,
});
@model("keystone/Remote/Acs01/AuxList")
export default class AuxList extends Model({
  auxs: tProp(types.array(types.model<Aux>(Aux)), () => []),
  defaultLoopConfigured: tProp(types.boolean, false).withSetter(),
  defaultLoopId: tProp(types.maybe(types.string)),
  defaultLoop: tProp(types.maybe(types.model<Aux>(Aux))),
  defaultLoopState: tProp(types.maybe(types.boolean)).withSetter(),
  defaultLostTicketOutputConfigured: tProp(types.boolean, false).withSetter(),
  defaultLostTicketOutputId: tProp(types.maybe(types.string)),
  defaultLostTicketOutput: tProp(types.maybe(types.model<Aux>(Aux))),
  auxLostTicketActionState: tProp(types.number, 0),
  defaultUnknownTicketOutputConfigured: tProp(
    types.boolean,
    false
  ).withSetter(),
  defaultUnknownTicketOutputId: tProp(types.maybe(types.string)),
  defaultUnknownTicketOutput: tProp(types.maybe(types.model<Aux>(Aux))),
  defaultUnknownTicketOutputState: tProp(types.number, 0),
}) {
  @modelAction
  setDefaultUnknownTicketOutputState(defaultUnknownTicketOutputState: number) {
    if (
      defaultUnknownTicketOutputState === 1 ||
      defaultUnknownTicketOutputState === 3
    ) {
      this.defaultUnknownTicketOutputState = defaultUnknownTicketOutputState;
      setTimeout(() => {
        this.setDefaultUnknownTicketOutputState(0);
      }, 6000);
    } else if (defaultUnknownTicketOutputState === 0) {
      this.defaultUnknownTicketOutputState = 0;
    }
  }

  @modelAction
  setAuxLostTicketActionState(auxLostTicketActionState: number) {
    if (auxLostTicketActionState === 1 || auxLostTicketActionState === 3) {
      this.auxLostTicketActionState = auxLostTicketActionState;
      setTimeout(() => {
        this.setAuxLostTicketActionState(0);
      }, 6000);
    } else if (auxLostTicketActionState === 0) {
      this.auxLostTicketActionState = 0;
    }
  }

  @modelAction
  set(auxs: any) {
    const all: Aux[] = [];
    for (let i = 0; i < auxs.length; i += 1) {
      all.push(new Aux(auxs[i]));
    }
    this.auxs = all;
  }

  @modelAction
  setDefaultUnknownTicketOutputId(
    defaultUnknownTicketOutputId: string | undefined
  ) {
    this.defaultUnknownTicketOutputId = defaultUnknownTicketOutputId;
  }

  @modelAction
  setDefaultLostTicketOutputId(defaultLostTicketOutputId: string | undefined) {
    this.defaultLostTicketOutputId = defaultLostTicketOutputId;
  }

  @modelAction
  setDefaultLostTicketOutput(defaultLostTicketOutput: any) {
    if (typeof defaultLostTicketOutput === "undefined") {
      this.defaultLostTicketOutput = undefined;
    } else {
      this.defaultLostTicketOutput = new Aux(defaultLostTicketOutput);
    }
  }

  @modelAction
  setDefaultUnknownTicketOutput(defaultUnknownTicketOutput: any) {
    if (typeof defaultUnknownTicketOutput === "undefined") {
      this.defaultUnknownTicketOutput = undefined;
    } else {
      this.defaultUnknownTicketOutput = new Aux(defaultUnknownTicketOutput);
    }
  }

  @modelAction
  setDefault(defaultLoop: any) {
    if (typeof defaultLoop === "undefined") {
      this.defaultLoop = undefined;
    } else {
      this.defaultLoop = new Aux(defaultLoop);
    }
  }

  @modelAction
  setDefaultLoopId(defaultLoopId: string | undefined) {
    this.defaultLoopId = defaultLoopId;
  }

  get allKeystone() {
    return this;
  }

  @modelAction
  add(aux: Aux) {
    this.auxs.push(aux);
  }

  @modelAction
  getAuxById(id: string | undefined) {
    return this.auxs.find((x) => x.id === id);
  }

  @modelAction
  remove(aux: any) {
    const index = this.auxs.indexOf(aux);
    if (index >= 0) {
      this.auxs.splice(index, 1);
    }
  }

  @modelAction
  getAuxNameById(id: string | undefined) {
    if (!id) {
      return "";
    }
    let name = "";
    const elementPos = this.auxs
      .map(function getId(u: Aux) {
        return u.id;
      })
      .indexOf(id);
    if (elementPos !== -1) {
      name = this.auxs[elementPos].name;
    }
    return name;
  }

  @modelAction
  getPassageIndexById(id: string) {
    const elementPos = this.auxs
      .map(function getId(u: Aux) {
        return u.id;
      })
      .indexOf(id);
    return elementPos;
  }

  @modelAction
  removeByIds(arrayOfIds: any) {
    if (typeof arrayOfIds !== undefined) {
      arrayOfIds.map((id: string) => {
        return this.remove(this.auxs.find((x: { id: string }) => x.id === id));
      });
    }
  }

  @modelAction
  setDefaultLoop(defaultLoop: Aux | undefined) {
    if (typeof defaultLoop === "undefined") {
      this.defaultLoop = undefined;
    } else {
      this.defaultLoop = defaultLoop;
    }
  }

  get all() {
    const all = [];
    for (let i = 0; i < this.auxs.length; i += 1) {
      all.push(this.auxs[i]);
    }
    return all;
  }

  get allInputs() {
    const all = [];
    for (let i = 0; i < this.auxs.length; i += 1) {
      if (this.auxs[i].type === 2) all.push(this.auxs[i]);
    }
    return all;
  }

  get allOutputs() {
    const all = [];
    for (let i = 0; i < this.auxs.length; i += 1) {
      if (this.auxs[i].type === 1) all.push(this.auxs[i]);
    }
    return all;
  }
}
