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

setGlobalConfig({
  modelAutoTypeChecking: ModelAutoTypeCheckingMode.AlwaysOn,
});
const dateValidityRow = new Date();
dateValidityRow.setHours(1);
dateValidityRow.setMinutes(0);
dateValidityRow.setSeconds(0);
@model('keystone/Application/TarifList')
export default class TarifList extends Model({
  tarifs: tProp(types.array(types.model<Tarif>(Tarif)), () => [
    new Tarif({
      id: uuidv4(),
      name: 'Default tarif, 1h free then €40',
      type: 0,
      startDate: 0,
      endDate: 0,
      startPrice: 0,
      maxPrice: 0,
      exitTimeConf: 0,
      exitTime: 0,
      tarifRows: [
        new TarifRow({
          sequence: 1,
          type: '',
          timeSlot: dateValidityRow.getTime(),
          repeat: 1,
          amount: '0',
        }),
        new TarifRow({
          sequence: 2,
          type: '',
          timeSlot: dateValidityRow.getTime(),
          repeat: 1,
          amount: '40',
        }),
      ],
    }),
  ]),
  useTarif: tProp(types.boolean, false),
  defaultTarif: tProp(types.maybe(types.model<Tarif>(Tarif))),
}) {
  get allKeystone() {
    return this;
  }

  @modelAction
  add(tarif: Tarif) {
    this.tarifs.push(tarif);
  }

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

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

  @modelAction
  getTarifNameById(id: string | undefined) {
    if (!id) {
      return '';
    }
    let name = '';
    const elementPos = this.tarifs
      .map(function getId(u: Tarif) {
        return u.id;
      })
      .indexOf(id);
    if (elementPos !== -1) {
      name = this.tarifs[elementPos].name;
    }
    return name;
  }

  @modelAction
  gettarifIndexById(id: string) {
    const elementPos = this.tarifs
      .map(function getId(u: Tarif) {
        return u.id;
      })
      .indexOf(id);
    return elementPos;
  }

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

  @modelAction
  setUseTarif(useTarif: boolean) {
    this.useTarif = useTarif;
  }

  @modelAction
  setDefaultTarif(defaultTarif: Tarif | number) {
    if (typeof defaultTarif === 'number') {
      this.defaultTarif = undefined;
    } else {
      this.defaultTarif = defaultTarif;
    }
  }

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

  get allAndHidden() {
    return this.tarifs;
  }

  get getUseTarif() {
    return this.useTarif;
  }
}
