import { ipcRenderer } from "electron";
import {
  getRootStore,
  model,
  Model,
  modelAction,
  ModelAutoTypeCheckingMode,
  setGlobalConfig,
  tProp,
  types,
} from "mobx-keystone";
import type { RootStore } from "../../root-store";
import Reader from "./Reader";

setGlobalConfig({
  modelAutoTypeChecking: ModelAutoTypeCheckingMode.AlwaysOn,
});
@model("keystone/Remote/Acs01/ReaderList")
export default class ReaderList extends Model({
  readers: tProp(types.array(types.model<Reader>(Reader)), () => []),
  defaultConfigured: tProp(types.boolean, false),
  defaultReaderId: tProp(types.maybe(types.string)),
  defaultReader: tProp(types.maybe(types.model<Reader>(Reader))),
}) {
  @modelAction
  set(readers: any) {
    const all: Reader[] = [];
    for (let i = 0; i < readers.length; i += 1) {
      all.push(new Reader(readers[i]));
    }
    this.readers = all;
  }

  @modelAction
  setReadersFromDefaultPassage(readers: any) {
    const all: Reader[] = [];
    const rootStore = getRootStore<RootStore>(this);

    for (let i = 0; i < readers.length; i += 1) {
      if (
        readers[i].passage === rootStore?.passageList.defaultPassageId &&
        readers[i].type === 13 &&
        readers[i].connection === 8
      )
        all.push(new Reader(readers[i]));
    }
    this.readers = all;
  }

  @modelAction
  setDefault(reader: any) {
    if (typeof reader === "undefined") {
      this.defaultReader = undefined;
    } else {
      this.defaultReader = new Reader(reader);
    }
  }

  @modelAction
  add(reader: Reader) {
    this.readers.push(reader);
  }

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

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

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

  @modelAction
  getReaderIndexById(id: string) {
    const elementPos = this.readers
      .map(function getId(u: Reader) {
        return u.id;
      })
      .indexOf(id);
    return elementPos;
  }

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

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

  @modelAction
  setDefaultReaderId(defaultReaderId: string | undefined) {
    ipcRenderer.sendToHost("mainWindow->main-keystoneSync", [
      { key: "defaultReaderId", value: defaultReaderId },
    ]);
    this.defaultReaderId = defaultReaderId;
  }

  get allKeystone() {
    return this;
  }

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