import { observable, action, computed, makeObservable } from 'mobx';
import { IServicepack, IRateplan, IUsagePack, IPrice, IAlert, IPackInfo } from 'typings';

import UsageItem from './UsageItem';

// hopefully this can be handled elsewhere soon
const PACK_SORT_RULES = {
  mobile: 0,
  internet: 10,
  unregistered: 11,
  fiber: 12,
  Special: 19,
  phonedata: 20,
  PhoneData: 21,
  Data: 22,
  SpecialData: 23,
  ConditionallyAvailable: 24,
  Phone: 29,
  foreign: 30,
  Foreign: 31,
  other: 40,
};

export default class UsagePack {
  constructor(state: IUsagePack) {
    this.servicepackId = state.servicepackId;
    this.title = state.title;
    this.price = state.price;
    this.alerts = state.alerts;
    this.offerUpsell = state.offerUpsell;
    this.items = state.items.map((i) => new UsageItem(i));
    this.info = state.info;
    this.excessCount = state.excessCount;
    this.isExcessUsage = state.isExcessUsage;
    this.minimumUpgradeInMb = state.minimumUpgradeInMb;
    this.validTo = state.validTo ? new Date(state.validTo) : undefined;
    this.connectionId = state.connectionId;

    makeObservable(this, {
      servicepackId: observable,
      connectionId: observable,
      title: observable,
      items: observable,
      price: observable,
      alerts: observable,
      offerUpsell: observable,
      info: observable,
      excessCount: observable,
      isExcessUsage: observable,
      minimumUpgradeInMb: observable,
      validTo: observable,
      primaryDisplayUsage: computed,
      secondaryUsage: computed,
      isUsageInfinite: computed,
      isUpgradeAllowed: action,
      isRateplanChangeAllowed: action,
      order: computed,
    });
  }

  servicepackId: string;

  connectionId: string;

  title: string;

  items: Array<UsageItem>;

  price: IPrice;

  alerts: Array<IAlert>;

  offerUpsell: boolean;

  info: IPackInfo;

  excessCount: number;

  isExcessUsage: boolean;

  minimumUpgradeInMb: number;

  validTo: Date | undefined;

  get primaryDisplayUsage() {
    const primaryItems = this.items.filter((i: UsageItem) => i.isPrimary);

    if (primaryItems.length > 1 && primaryItems.find((x) => x.infinite)) {
      const remainingUnit = primaryItems
        .map((item) => {
          return item.mainUnit;
        })
        .join('/');

      return `∞ ${remainingUnit}`;
    }

    if (primaryItems.length > 0) {
      return primaryItems[0].remaining;
    }

    return this.items.length > 0 ? this.items[0].remaining : '';
  }

  get secondaryUsage() {
    return this.items.filter((i: UsageItem) => !i.isPrimary);
  }

  get isUsageInfinite() {
    return this.items.find((i) => i.infinite && i.isPrimary);
  }

  isUpgradeAllowed(servicepack: IServicepack) {
    return (
      servicepack.typeId !== 'phonedata' || servicepack.dataAmountInMb >= this.minimumUpgradeInMb
    );
  }

  isRateplanChangeAllowed(rateplan: IRateplan) {
    return rateplan.includedDataAmountInMb >= this.minimumUpgradeInMb;
  }

  get order() {
    return this.info && this.info.typeId
      ? PACK_SORT_RULES[this.info.typeId]
      : PACK_SORT_RULES.other;
  }
}
