import { formatNumber } from 'core/tools/format';
import { ICalculationStore } from 'core/types/Calculation/Store';
import { ComputedValuesNames } from 'core/types/Calculation/Store/values';
import { convertPrice } from './lib/tools';

const LEASE_OBJECT_RISK = {
  100000000: 'Низкий',
  100000001: 'Средний',
  100000002: 'Высокий',
};

type ComputedEffect = (this: ICalculationStore) => any;

export const insKaskoPriceLeasePeriod: ComputedEffect = function () {
  const { leasingPeriod } = this.values;
  const { rows } = this.tables.tableInsurance;
  const kaskoRow = rows[1];
  const dgoRow = rows[2];
  const nsRow = rows[3];

  let res = 0;
  if (
    leasingPeriod &&
    leasingPeriod > 15 &&
    kaskoRow?.insTerm?.value === 100000001
  ) {
    res =
      (leasingPeriod / 12) *
      (kaskoRow?.insCost?.value +
        dgoRow?.insCost?.value +
        nsRow?.insCost?.value);
  }

  return res;
};

const computedEffects: Record<ComputedValuesNames, ComputedEffect> = {
  leaseObjectRiskName: function () {
    const configuration = this.getOption('selectConfiguration');
    if (configuration) {
      if (configuration.evo_leasingobject_risk) {
        const res = LEASE_OBJECT_RISK[configuration.evo_leasingobject_risk];
        return res;
      }
    }

    const model = this.getOption('selectModel');
    const evo_leasingobject_risk = model?.evo_leasingobject_risk;
    if (evo_leasingobject_risk)
      return LEASE_OBJECT_RISK[evo_leasingobject_risk];
  },
  insKaskoPriceLeasePeriod,

  irr: function () {
    const product = this.getOption('selectProduct');
    const tarif = this.getOption('selectTarif');
    const { bonusCoefficient } = this.values;

    let min = 0;
    let max = 0;

    if (product?.evo_cut_irr_with_bonus && bonusCoefficient < 1) {
      min =
        (tarif?.evo_min_irr || 0) -
        (1 - bonusCoefficient || 0) *
          (tarif?.evo_cut_irr_with_bonus_coefficient || 0);
    } else {
      min = tarif?.evo_min_irr || 0;
    }

    max = tarif?.evo_max_irr || 0;

    return { min, max };
  },
  irrInfo: function () {
    //@ts-ignore
    return `Min: ${formatNumber(this.irr().min)}%  - Max: ${formatNumber(
      //@ts-ignore
      this.irr().max,
    )}%`;
  },
  registrationDescription: function () {
    const registration = this.getOption('selectRegistration');
    return registration?.evo_description;
  },

  plPriceRub: function () {
    const supplierCurrency = this.getOption('selectSupplierCurrency');
    const { leaseObjectPrice } = this.values;

    const evo_currencychanges = this.getStaticData('evo_currencychange');
    const evo_currencychange = evo_currencychanges.find(
      x =>
        x.evo_ref_transactioncurrency ===
        supplierCurrency?.transactioncurrencyid,
    );

    return convertPrice(
      supplierCurrency?.isocurrencycode,
      leaseObjectPrice,
      evo_currencychange?.evo_currencychange || 1,
    );
  },

  discountRub: function () {
    const supplierCurrency = this.getOption('selectSupplierCurrency');
    const { supplierDiscountRub } = this.values;

    const evo_currencychanges = this.getStaticData('evo_currencychange');
    const evo_currencychange = evo_currencychanges.find(
      x =>
        x.evo_ref_transactioncurrency ===
        supplierCurrency?.transactioncurrencyid,
    );

    return convertPrice(
      supplierCurrency?.isocurrencycode,
      supplierDiscountRub,
      evo_currencychange?.evo_currencychange || 1,
    );
  },

  //TODO для нового калькулятора: перенести в process/subsidy
  importProgramSum: function () {
    const importProgram = this.getOption('selectImportProgram');

    if (!importProgram) return 0;
    if (
      importProgram?.evo_subsidy_summ &&
      importProgram?.evo_subsidy_summ > 0
    ) {
      return importProgram?.evo_subsidy_summ;
    } else {
      const sum =
        //@ts-ignore
        ((this.plPriceRub() - (this.values.supplierDiscountRub || 0)) *
          (importProgram?.evo_percent_subsidy || 0)) /
        100;
      return Math.min(sum, importProgram?.evo_max_subsidy_summ || 0);
    }
  },
};

export default computedEffects;
