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

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

@model("keystone/Remote/Acs01")
export default class Acs01 extends Model({
  id: tProp(types.string, () => uuidv4()),
  ip: tProp(types.string, ""),
  role: tProp(types.number, -1),
  username: tProp(types.string, ""),
  password: tProp(types.string, ""),
  siteId: tProp(types.string, ""),
  remoteUrl: tProp(types.string, ""),
  localUrl: tProp(types.string, ""),
  localName: tProp(types.string, ""),
  moduleId: tProp(types.string, ""),
  connectionPreference: tProp(types.number, 0),
  failoverConfig: tProp(types.number, 0),
  acs01List: tProp(
    types.array(
      types.object(() => ({
        addresses: types.array(types.string),
        ip: types.string,
        name: types.string,
        hostname: types.string,
        port: types.number,
        type: types.string,
        protocol: types.string,
        domain: types.string,
      }))
    ),
    () => []
  ),
  sitesList: tProp(
    types.array(
      types.object(() => ({
        id: types.string,
        name: types.string,
        code: types.string,
        state: types.number,
        image: types.string,
        counterList: types.maybe(
          types.array(
            types.object(() => ({
              id: types.string,
              name: types.string,
              value: types.number,
            }))
          )
        ),
      }))
    ),
    () => []
  ),
  localStatusSingleAccount: tProp(types.number, -1),
  remoteStatusSingleAccount: tProp(types.number, -1),
  failoverStatus: tProp(types.number, -1),
}) {
  onInit = () => {};

  @modelAction
  getAcs01ByName(localName: string) {
    return this.acs01List.find((x) => x.name === localName);
  }

  @modelAction
  setModuleId(moduleId: string) {
    this.moduleId = moduleId;
  }

  @modelAction
  setUsername(username: string) {
    this.username = username;
  }

  @modelAction
  setPassword(password: string) {
    this.password = password;
  }

  @modelAction
  setSiteId(siteId: string) {
    this.siteId = siteId;
  }

  @modelAction
  setRemoteUrl(remoteUrl: string) {
    this.remoteUrl = remoteUrl;
  }

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

  @modelAction
  setLocalName(localName: string) {
    this.localName = localName;
  }

  @modelAction
  setConnectionPreference(connectionPreference: number) {
    this.connectionPreference = connectionPreference;
  }

  @modelAction
  setFailoverConfig(failoverConfig: number) {
    this.failoverConfig = failoverConfig;
  }

  @modelAction
  removeAcs01List(acs01autodiscover: any) {
    const index = this.acs01List.indexOf(acs01autodiscover);
    if (index >= 0) {
      this.acs01List.splice(index, 1);
    }
  }

  @modelAction
  addAcs01List(acs01autodiscover: any) {
    this.removeAcs01List(
      this.acs01List.find((x) => x.name === acs01autodiscover.name)
    );
    this.acs01List.push(acs01autodiscover);
  }

  @modelAction
  setAcs01List(acs01List: any) {
    this.acs01List = acs01List;
  }

  @modelAction
  setSitesList(sitesList: any) {
    if (sitesList !== undefined) this.sitesList = sitesList;
  }

  @modelAction
  getSiteIndexBySiteId(id: string) {
    const elementPos = this.sitesList
      .map(function getId(s: any) {
        return s.id;
      })
      .indexOf(id);
    return elementPos;
  }

  @modelAction
  setCountersList(siteId: string, counterList: any) {
    if (counterList !== undefined)
      this.sitesList[this.getSiteIndexBySiteId(siteId)].counterList =
        counterList;
  }

  @modelAction
  getSiteBySiteId(siteId: string) {
    return this.sitesList.find((x) => x.id === siteId);
  }

  @modelAction
  setLocalStatusSingleAccount(localStatusSingleAccount: number) {
    this.localStatusSingleAccount = localStatusSingleAccount;
  }

  @modelAction
  setRemoteStatusSingleAccount(remoteStatusSingleAccount: number) {
    this.remoteStatusSingleAccount = remoteStatusSingleAccount;
  }

  @modelAction
  setFailoverStatus(failoverStatus: number) {
    this.failoverStatus = failoverStatus;
  }

  @modelAction
  setIp(ip: string) {
    this.ip = ip;
  }

  @modelAction
  setRole(role: number) {
    this.role = role;
  }

  
}
