import { ipcRenderer } from 'electron';
import {
  getRootStore,
  model,
  Model,
  modelAction,
  ModelAutoTypeCheckingMode,
  setGlobalConfig,
  tProp,
  types,
} from 'mobx-keystone';
import { v4 as uuidv4 } from 'uuid';

import type { RootStore } from '../root-store';

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

@model('keystone/Settings/Settings')
export default class Settings extends Model({
  id: tProp(types.string, () => uuidv4()), 
  printWithoutBasket: tProp(types.boolean, true),
  darkMode: tProp(types.boolean, true),
  backgroundColor: tProp(types.string, '#424242'),
  backgroundColorPaper: tProp(types.string, '#424242'),
  defaultLanguage: tProp(types.string, ''), 
  ticketLevelToShow: tProp(types.number, 1), 
  autoUpdate: tProp(types.boolean, false),
  autoStart: tProp(types.boolean, false),
  fullscreen: tProp(types.boolean, false),
  silentPrint: tProp(types.boolean, true),
  applicationMode: tProp(types.number, 1),
  gridHeight: tProp(types.number, 450),
  hideCursor: tProp(types.boolean, false),
  serialPortList: tProp(
    types.array(
      types.object(() => ({
        manufacturer: types.string,
        serialNumber: types.maybe(types.string),
        pnpId: types.maybe(types.string),
        locationId: types.maybe(types.string),
        vendorId: types.maybe(types.string),
        productId: types.maybe(types.string),
        path: types.string,
      }))
    ),
    () => [
      {
        manufacturer: 'None',
        serialNumber: '1111',
        pnpId: '',
        locationId: undefined,
        vendorId: '',
        productId: '',
        path: 'No ports detected',
      },
    ]
  ),
  serialPortOnline: tProp(types.boolean, false),
  serialPortNeeded: tProp(types.boolean, false),
  serialPortPath: tProp(types.string, 'No ports detected'),
  serialMessage: tProp(types.string, ''),
  lostTicketId: tProp(types.string, ''),
  entryTicketId: tProp(types.string, ''),
  ipEMV: tProp(types.string, 'localhost'),
  emvDatacap: tProp(types.boolean, false).withSetter(), 
  merchantId: tProp(types.string, 'CAMEAMIAM0GP').withSetter(),
  operatorId: tProp(types.string, '4444').withSetter(),
  tranDeviceId: tProp(types.string, '1640014245').withSetter(),
  // Will always be 45s busy unless canceled by user on the EMV
  stateEMV: tProp(types.boolean, false),
  screenSaverEnabled: tProp(types.boolean, true),
  screenSaverState: tProp(types.boolean, true),
  useKioskMode: tProp(types.boolean, true), 
  kioskMode: tProp(types.boolean, false), 
  touchScreen: tProp(types.boolean, false),
  firstLanguage: tProp(types.number, 1),
  secondLanguage: tProp(types.number, 0),
  thirdLanguage: tProp(types.number, 0),
  workerWindowId: tProp(types.number, -1).withSetter(),

}) {
  onAttachedToRootStore = () => {
    this.setKioskMode(false);

    const rootStore = getRootStore<RootStore>(this);
    if (
      rootStore?.settings.applicationMode === 1 ||
      rootStore?.settings.applicationMode === 2 ||
      rootStore?.settings.applicationMode === 3 ||
      rootStore?.settings.applicationMode === 4 ||
      rootStore?.settings.applicationMode === 5
    ) {
      this.useKioskMode = true;
    } else if (rootStore?.settings.applicationMode === 0) {
      this.useKioskMode = false;
    }

    if (this.useKioskMode) {
      rootStore?.userList.setLoggedInUser(0);
      rootStore?.userList.setLoggedIn(false);
      setTimeout(() => {
        this.setKioskMode(true);
      }, 25000);
    }
    if (
      this.applicationMode === 5 &&
      rootStore?.terminal.scannerConnectedToDisplay
    ) {
      
    }
  };

  @modelAction
  setStateEmv(stateEMV: boolean) {
    this.stateEMV = stateEMV;
  }

  @modelAction
  setFirstLanguage(firstLanguage: number) {
    this.firstLanguage = firstLanguage;
  }

  @modelAction
  setSecondLanguage(secondLanguage: number) {
    this.secondLanguage = secondLanguage;
  }

  @modelAction
  setThirdLanguage(thirdLanguage: number) {
    this.thirdLanguage = thirdLanguage;
  }

  @modelAction
  setTouchScreen(touchScreen: boolean) {
    this.touchScreen = touchScreen;
  }

  @modelAction
  setKioskMode(kioskMode: boolean) {
    this.kioskMode = kioskMode;
  }

  @modelAction
  setScreenSaverEnabled(screenSaverEnabled: boolean) {
    this.screenSaverEnabled = screenSaverEnabled;
  }

  @modelAction
  setScreenSaverState(screenSaverState: boolean) {
    this.screenSaverState = screenSaverState;
  }

  @modelAction
  getSerialByPath(path: string) {
    return this.serialPortList.find((x) => x.path === path);
  }

  @modelAction
  setSerialPortList(serialPortList: any) {
    this.serialPortList = serialPortList;
  }

  @modelAction
  setIpEMV(ipEMV: string) {
    this.ipEMV = ipEMV;
  }

  @modelAction
  setLostTicket(lostTicketId: string) {
    this.lostTicketId = lostTicketId;
  }

  @modelAction
  setEntryTicket(entryTicketId: string) {
    this.entryTicketId = entryTicketId;
  }

  @modelAction
  setSerialMessage(message: string) {
    this.serialMessage = message;
  }

  @modelAction
  setSerialPortPath(path: string) {
    this.serialPortPath = path;
  }

  @modelAction
  setHideCursor(cursor: boolean) {
    this.hideCursor = cursor;
  }

  @modelAction
  setGridHeight(height: number) {
    this.gridHeight = height;
  }

  @modelAction
  setPrintWithoutBasket(printWithoutBasket: boolean) {
    this.printWithoutBasket = printWithoutBasket;
  }

  @modelAction
  setTicketLevelToShow(ticketLevelToShow: number) {
    this.ticketLevelToShow = ticketLevelToShow;
  }

  @modelAction
  setDarkMode(darkMode: boolean) {
    this.darkMode = darkMode;
    if (darkMode) {
      this.backgroundColor = '#424242';
    } else {
      this.backgroundColor = '#f2f2f2';
    }
  }

  @modelAction
  setBackgroundColor(backgroundColor: string) {
    this.backgroundColor = backgroundColor;
  }

  @modelAction
  setApplicationMode(applicationMode: number) {
    if (applicationMode === 1 || applicationMode === 5) {
      this.setTouchScreen(true);
    }
    this.applicationMode = applicationMode;
  }

  @modelAction
  setSilentPrint(silentPrint: boolean) {
    this.silentPrint = silentPrint;
  }

  @modelAction
  setAutoStart(autoStart: boolean) {
    this.autoStart = autoStart;
  }

  @modelAction
  setFullscreen(fullscreen: boolean) {
    this.fullscreen = fullscreen;
  }

  @modelAction
  setAutoUpdate(autoUpdate: boolean) {
    this.autoUpdate = autoUpdate;
  }

  @modelAction
  setDefaultLanguage(language: string) {
    this.defaultLanguage = language;
  }

  get printWithoutBasketValue() {
    return this.printWithoutBasket;
  }

  get darkModeValue() {
    return this.darkMode;
  }

  get defaultLanguageValue() {
    return this.defaultLanguage;
  }
}
