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

setGlobalConfig({
  modelAutoTypeChecking: ModelAutoTypeCheckingMode.AlwaysOn,
});
@model("keystone/Remote/Acs01/PassageList")
export default class PassageList extends Model({
  passages: tProp(types.array(types.model<Passage>(Passage)), () => []),
  defaultConfigured: tProp(types.boolean, false),
  defaultPassageId: tProp(types.maybe(types.string)),
  defaultPassage: tProp(types.maybe(types.model<Passage>(Passage))),
  zoneTo: tProp(types.number, 1),
  actionState: tProp(types.number, 0),
}) {
  @modelAction
  setActionState(actionState: number) {
    // If its continously open
    if (actionState === 4) {
      this.actionState = 4;
    }
    // If the continously open gets put off we disable the actionstate
    if (actionState === 5) {
      this.actionState = 5;
      setTimeout(() => {
        this.setActionState(0);
      }, 3000);
    }
    if (actionState === 1 || actionState === 3) {
      this.actionState = actionState;
      setTimeout(() => {
        this.setActionState(0);
      }, 6000);
    } else if (actionState === 0) {
      this.actionState = 0;
    }
  }

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

  @modelAction
  setDefault(passage: any) {
    if (typeof passage === "undefined") {
      this.defaultPassage = undefined;
    } else {
      this.defaultPassage = new Passage(passage);
    }
  }

  @modelAction
  add(passage: Passage) {
    this.passages.push(passage);
  }

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

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

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

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

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

  @modelAction
  setDefaultConfigured(defaultConfigured: boolean) {
    this.defaultConfigured = defaultConfigured;
  }

  @modelAction
  setDefaultPassageId(defaultPassageId: string | undefined) {
    this.defaultPassageId = defaultPassageId;
  }

  @modelAction
  setZoneTo(zoneTo: number) {
    this.zoneTo = zoneTo;
  }

  get allKeystone() {
    return this;
  }

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