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

setGlobalConfig({
  modelAutoTypeChecking: ModelAutoTypeCheckingMode.AlwaysOn,
});
@model("keystone/Remote/CounterList")
export default class CounterList extends Model({
  counters: tProp(types.array(types.model<Counter>(Counter)), () => []),
  counterGroups: tProp(
    types.array(
      types.object(() => ({
        id: types.string,
        name: types.string,
        ip: types.string,
        maxSize: types.number,
        resetMode: types.number,
        message: types.number,
        zeroBecomesXXX: types.boolean,
        counters: types.array(
          types.object(() => ({
            counterId: types.string,
            operator: types.string,
            value: types.number,
          }))
        ),
      }))
    ),
    () => []
  ),
  resetMode: tProp(types.number, 0),
  configured: tProp(types.boolean, false),
  schedulerAction: tProp(types.number, 0), // here we set the value to 1 when receiving a schedule
}) {
  @modelAction
  add(counter: Counter) {
    // Don't add if it already exists
    if (!this.counters.find((x) => x.id === counter.id))
      this.counters.push(counter);
  }

  @modelAction
  addCounterGroup(
    name: string,
    ip: string,
    maxSize: number,
    resetMode: number,
    message: number,
    zeroBecomesXXX: boolean,
    counterGroupArr: string[]
  ) {
    const parsedCounterGroup = [];

    for (let i = 0; i < counterGroupArr.length; i += 1) {
      console.log("loop", i);
      if (
        counterGroupArr[i] !== "+" &&
        counterGroupArr[i] !== "-" &&
        counterGroupArr[i] !== "End of counter group"
      ) {
        parsedCounterGroup.push({
          counterId: counterGroupArr[i],
          operator: counterGroupArr[i + 1],
          value: 0,
        });
      }
    }

    if (parsedCounterGroup !== []) {
      this.counterGroups.push({
        id: uuidv4(),
        name,
        ip,
        maxSize,
        resetMode,
        message,
        zeroBecomesXXX,
        counters: parsedCounterGroup,
      });
    }
  }

  @modelAction
  setValue(x: number, y: number, value: number) {
    this.counterGroups[x].counters[y].value = value;
  }

  @modelAction
  setConfigured(configured: boolean) {
    this.configured = configured;
  }

  @modelAction
  set(counterArr: Counter[]) {
    this.counters = counterArr;
  }

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

  @modelAction
  getCounterById(id: string) {
    return this.counters.find((x) => x.id === id);
  }

  @modelAction
  removeAllCounterGroups() {
    this.counterGroups.length = 0;
  }

  @modelAction
  remove(counter: Counter | undefined) {
    if (typeof counter === "undefined") return;
    const index = this.counters.indexOf(counter);
    if (index >= 0) {
      this.counters.splice(index, 1);
    }
  }

  @modelAction
  getCounterIndexById(id: string) {
    const elementPos = this.counters
      .map(function getId(a: Counter) {
        return a.id;
      })
      .indexOf(id);
    return elementPos;
  }

  @modelAction
  setResetMode(resetMode: number) {
    this.resetMode = resetMode;
  }

  @modelAction
  setSchedulerAction(schedulerAction: number) {
    this.schedulerAction = schedulerAction;
  }

  @modelAction
  removeByIds(arrayOfIds: Array<string>) {
    arrayOfIds.map((id: string) => {
      return this.remove(
        this.counters.find((x: { id: string }) => x.id === id)
      );
    });
  }

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

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

  get allKeystone() {
    return this.counters;
  }
}
