import { isEmpty } from 'lodash';
import { action, computed, extendObservable, makeObservable, observable } from 'mobx';
import {
  ERefillType,
  IAutoRefill,
  IProfile,
  IRateplan,
  IRateplanCategory,
  IRefill,
  IRefillCategory,
  IServicepack,
  IServicepackCategory,
  ISlot,
  IUsagePack,
  IVisitSlotInfo,
} from 'typings';

import AutoRefill from './models/AutoRefill';
import Connection from './models/Connection';

export default class Change {
  constructor({ change = {} }: any) {
    makeObservable(this, {
      servicepackType: observable,
      selectedRefillCategory: observable,
      selectedRateplanCategory: observable,
      selectedServicepack: observable,
      selectedRefill: observable,
      selectedProfile: observable,
      selectedRateplan: observable,
      packToChange: observable,
      autoRefillToChange: observable,
      autoRefillType: observable,
      activateNextMonth: observable,
      checkEffectTime: observable,
      connectionToChange: observable,
      connection: observable,
      reason: observable,
      reasonDescription: observable,
      workDate: observable,
      slot: observable,
      visitToChange: observable,
      changeConnection: action,
      initChangeVisit: action,
      initChangePack: action,
      initChangeAutoRefill: action,
      initChangeConnection: action,
      addServicepackType: action,
      selectProfile: action,
      isUpgradeAllowed: action,
      resetChangeFlow: action,
      isRateplanChangeAllowed: action,
      allowChangeNow: computed,
      allowChangeLater: computed,
      terminationDate: observable,
    });

    extendObservable(this, change);
  }

  servicepackType: IServicepackCategory | undefined = undefined;

  selectedRefillCategory: IRefillCategory | undefined = undefined;

  selectedRateplanCategory: IRateplanCategory | undefined = undefined;

  selectedServicepack: IServicepack | undefined = undefined;

  selectedRefill: IRefill | undefined = undefined;

  selectedProfile: IProfile | undefined = undefined;

  selectedRateplan: IRateplan | undefined = undefined;

  packToChange: IUsagePack | undefined = undefined;

  autoRefillToChange: AutoRefill | undefined = undefined;

  autoRefillType: 'payment' | 'date' = 'payment';

  activateNextMonth = false;

  checkEffectTime = true;

  connectionToChange: Connection | undefined = undefined;

  connection: string | undefined = undefined;

  reason: string | undefined = undefined;

  reasonDescription: string | undefined = undefined;

  workDate: string | undefined = undefined;

  terminationDate: string | undefined = undefined;

  slot: ISlot | undefined = undefined;

  visitToChange: IVisitSlotInfo | undefined = undefined;

  changeConnection = (connection: string) => {
    this.connection = connection;
  };

  initChangeVisit = (visitSlotInfo: IVisitSlotInfo) => {
    this.resetChangeFlow();
    this.visitToChange = visitSlotInfo;
  };

  initChangePack = (
    profile: IProfile,
    pack: IUsagePack,
    availeblaAutoRefillsCategory?: Array<IRefillCategory>,
    autoRefill?: IAutoRefill,
  ) => {
    this.resetChangeFlow();
    this.selectProfile(profile);

    if (pack && this.selectedProfile) {
      this.packToChange = pack;
      switch (pack.info.contentType) {
        case 'servicepack':
          this.servicepackType = this.selectedProfile.allowedServicePacks.find(
            (cat) => cat.servicepackType === this.packToChange!.info.typeId,
          );
          break;
        case 'refill':
          this.selectedRefillCategory = availeblaAutoRefillsCategory!.find(
            (cat) => ERefillType[cat.refillType] === ERefillType[this.packToChange!.info.typeId],
          );
          this.autoRefillToChange = autoRefill;
          break;
        case 'rateplan':
          this.selectedRateplanCategory = this.selectedProfile.rateplan.availableRateplans.find(
            (cat) => cat.rateplanType === this.packToChange!.info.typeId,
          );
          break;
        default:
      }
    }
  };

  initChangeAutoRefillPack = (
    profile: IProfile,
    autoRefill: IAutoRefill,
    availeblaAutoRefillsCategory: Array<IRefillCategory>,
  ) => {
    this.resetChangeFlow();
    this.selectProfile(profile);

    if (autoRefill && this.selectedProfile && availeblaAutoRefillsCategory) {
      this.autoRefillToChange = autoRefill;
      this.selectedRefillCategory = availeblaAutoRefillsCategory.find(
        (cat) => ERefillType[cat.refillType] === ERefillType[autoRefill.info.typeId],
      );
    }
  };

  initChangeAutoRefill = (profile: IProfile, autoRefill: AutoRefill, type: 'payment' | 'date') => {
    this.resetChangeFlow();
    this.selectProfile(profile);

    if (autoRefill && this.selectedProfile) {
      this.autoRefillToChange = autoRefill;
      this.autoRefillType = type;
    }
  };

  initChangeConnection = (profile: IProfile, connection: Connection) => {
    this.resetChangeFlow();
    this.selectProfile(profile);

    if (connection && this.selectedProfile) {
      this.connectionToChange = connection;
    }
  };

  addServicepackType(servicepackType: IServicepackCategory) {
    this.resetChangeFlow(true);

    this.servicepackType = servicepackType;

    if (servicepackType && this.selectedProfile && !isEmpty(this.selectedProfile.packs)) {
      const packByCategory = this.selectedProfile.packs.find((p: IUsagePack) => {
        return p.info && p.info.typeId === servicepackType.servicepackType && p.info.canModify;
      });

      if (!isEmpty(packByCategory)) {
        this.packToChange = packByCategory;
      }
    } else this.packToChange = undefined;
  }

  selectProfile = (profile: IProfile) => {
    this.selectedProfile = profile;
  };

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

  resetChangeFlow = (skipProfile = false) => {
    this.packToChange = undefined;
    this.autoRefillToChange = undefined;
    this.connectionToChange = undefined;
    this.selectedRefill = undefined;
    this.selectedServicepack = undefined;
    this.servicepackType = undefined;
    this.selectedRateplan = undefined;
    if (!skipProfile) {
      this.selectedProfile = undefined;
    }
    this.packToChange = undefined;
    this.activateNextMonth = false;
    this.checkEffectTime = true;
    this.autoRefillType = 'payment';
    this.connection = undefined;
    this.visitToChange = undefined;
    this.reason = '';
    this.reasonDescription = '';
  };

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

  get allowChangeNow() {
    return this.packToChange
      ? this.selectedRefill ||
          (this.selectedServicepack && this.isUpgradeAllowed(this.selectedServicepack)) ||
          (this.selectedRateplan && this.isRateplanChangeAllowed(this.selectedRateplan))
      : true;
  }

  get allowChangeLater() {
    return (
      this.selectedRefill ||
      (this.selectedServicepack && this.selectedServicepack.typeId === 'phonedata') ||
      this.selectedRateplan
    );
  }
}
