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

setGlobalConfig({
  modelAutoTypeChecking: ModelAutoTypeCheckingMode.AlwaysOn,
});

@model("keystone/Remote/Acs01/PrinterList")
export default class PrinterList extends Model({
  printers: tProp(types.array(types.model<Printer>(Printer)), () => []),
  defaultConfigured: tProp(types.boolean, false),
  defaultPrinterId: tProp(types.maybe(types.string)),
  defaultPrinterBackupId: tProp(types.maybe(types.string)),
  defaultPrinterButton: tProp(types.maybe(types.model<Aux>(Aux))).withSetter(),
  defaultPrinterButtonId: tProp(types.maybe(types.string)),
  defaultPrinterButtonState: tProp(types.maybe(types.boolean)).withSetter(),
  defaultPrinter: tProp(types.maybe(types.model<Printer>(Printer))),
  printerState: tProp(types.number, 0),
  newPrintCommandSend: tProp(types.boolean, false), // To display it's being printed
  newPrintSequence: tProp(types.boolean, false),
}) {
  onInit = () => {
    this.printerState = 0;
    this.newPrintSequence = false;
  };

  @modelAction
  setNewPrintCommandSend(newPrintCommandSend: boolean) {
    this.newPrintCommandSend = newPrintCommandSend;
  }

  @modelAction
  setPrinterState(printerState: number) {
    this.printerState = printerState;
  }

  @modelAction
  setNewPrintSequence(newPrintSequence: boolean) {
    this.newPrintSequence = newPrintSequence;
  }

  @modelAction
  set(printers: any) {
    const all: Printer[] = [];
    console.log(printers);
    for (let i = 0; i < printers.length; i += 1) {
      all.push(
        new Printer({
          address: printers[i]["address"],
          condition_input: printers[i]["condition_input"],
          connection_type: printers[i]["connection_type"],
          delay_between_prints: printers[i]["delay_between_prints"],
          driver: printers[i]["driver"],
          fallback_device: printers[i]["fallback_device"],
          fault: printers[i]["fault"],
          id: printers[i]["id"],
          moduleid: printers[i]["moduleid"],
          name: printers[i]["name"],
          one_ticket_per_condition: printers[i]["one_ticket_per_condition"],
          paper_low: printers[i]["paper_low"],
          passage: printers[i]["passage"],
          print_input: printers[i]["print_input"],
          retract_time: printers[i]["retract_time"],
          template: printers[i]["template"],
          zone: printers[i]["zone"],
        })
      );
    }
    this.printers = all;
  }

  @modelAction
  setDefaultPrinterId(defaultPrinterId: string | undefined) {
    this.defaultPrinterId = defaultPrinterId;
  }

  @modelAction
  setDefaultPrinterBackupId(defaultPrinterBackupId: string | undefined) {
    this.defaultPrinterBackupId = defaultPrinterBackupId;
  }

  @modelAction
  setDefaultPrinterButtonId(defaultPrinterButtonId: string | undefined) {
    this.defaultPrinterButtonId = defaultPrinterButtonId;
  }

  // New action for setting defaultPrinterButton with conversion
  @modelAction
  setDefaultPrinterButtonData(auxData: Aux | any) {
    if (!auxData) {
      this.defaultPrinterButton = undefined;
    } else if (auxData instanceof Aux) {
      this.defaultPrinterButton = auxData;
    } else {
      this.defaultPrinterButton = new Aux(auxData);
    }
  }

  @modelAction
  add(printer: Printer) {
    this.printers.push(printer);
  }

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

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

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

  @modelAction
  getPrinterIndexById(id: string) {
    const elementPos = this.printers
      .map(function getId(u: Printer) {
        return u.id;
      })
      .indexOf(id);
    return elementPos;
  }

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

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

  // Modified setDefaultPrinter action: It converts raw printer data to a Printer instance if necessary.
  @modelAction
  setDefaultPrinter(defaultTarif: Printer | any) {
    if (typeof defaultTarif === "undefined" || defaultTarif === null) {
      this.defaultPrinter = undefined;
    } else if (defaultTarif instanceof Printer) {
      this.defaultPrinter = defaultTarif;
    } else {
      // Convert the raw object to a Printer instance
      this.defaultPrinter = new Printer({
        id: defaultTarif.id,
        fallback_device: defaultTarif.fallback_device,
        name: defaultTarif.name,
        moduleid: defaultTarif.moduleid,
        connection_type: defaultTarif.connection_type,
        driver: defaultTarif.driver,
        address: defaultTarif.address,
        template: defaultTarif.template,
        print_input: defaultTarif.print_input,
        condition_input: defaultTarif.condition_input,
        passage: defaultTarif.passage,
        zone: defaultTarif.zone,
        retract_time: defaultTarif.retract_time,
        one_ticket_per_condition: defaultTarif.one_ticket_per_condition,
        delay_between_prints: defaultTarif.delay_between_prints,
        paper_low: defaultTarif.paper_low,
        fault: defaultTarif.fault,
      });
    }
  }

  get allKeystone() {
    return this;
  }

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