import { Table } from 'client/Containers/Calculation/types/tables';
import valuesConstants from 'core/constants/values';
import {
  GetCalculationResponse,
  PreparedData,
} from 'core/services/CoreService/types/Calculation/request';
import { ICalculationStore } from 'core/types/Calculation/Store';

export default {
  showResultsTable: function (
    this: ICalculationStore,
    preparedData: PreparedData,
    res: GetCalculationResponse,
  ) {
    if (preparedData.preparedValues.nmper) {
      const { sumWithVatColumn, vatColumn, sumRepaymentColumn } = res.columns;
      const results: Table['rows'] = [];

      for (let i = 0; i < preparedData.preparedValues.nmper; i++) {
        results.push({
          paymentSum: { value: sumWithVatColumn?.values[i + 1] },
          ndsCompensation: { value: vatColumn?.values[i + 1] },
          redemptionAmount: {
            value: sumRepaymentColumn?.values[i + 1],
          },
        });
      }

      const subdidySum = this.getValue('subsidySum') || 0;
      if (results[0]?.paymentSum?.value)
        results[0].paymentSum.value -= subdidySum;
      if (results[0]?.ndsCompensation?.value) {
        results[0].ndsCompensation.value -= subdidySum - subdidySum / 1.2;
      }

      //TODO: speed up table render
      this.setTableRows('tableResults', 0)(results);
    }
  },
  showResults: function (
    this: ICalculationStore,
    preparedData: PreparedData,
    res: GetCalculationResponse,
  ) {
    const { sumWithVatColumn } = res.columns;
    this.setValue(
      'resultTotalGraphwithNDS',
      (sumWithVatColumn?.values[0] || 0) - (this.getValue('subsidySum') || 0),
    );
    //@ts-ignore
    const importProgramSum = this.importProgramSum();
    this.setValue(
      'resultPlPrice',
      (preparedData?.preparedValues?.plPriceWithVAT || 0) -
        this.values.supplierDiscountRub -
        importProgramSum,
    );
    this.setValue(
      'resultPriceUpPr',
      (res?.postValues?.priceUP_Year_PR || 0) * 100,
    );
    this.setValue(
      'resultIRRGraphPerc',
      (res?.columns?.sumColumn?.irr || 0) * 100,
    );
    this.setValue(
      'resultIRRNominalPerc',
      (res?.columns?.cashflowMsfoColumn?.nominal || 0) * 100,
    );
    this.setValue(
      'resultInsKasko',
      preparedData?.preparedValues?.insuranceKasko || 0,
    );
    this.setValue(
      'resultInsOsago',
      preparedData?.preparedValues?.insuranceOsago || 0,
    );
    this.setValue(
      'resultInsOsago',
      preparedData?.preparedValues?.insuranceOsago || 0,
    );
    this.setValue(
      'resultDopProdSum',
      (preparedData?.preparedValues?.rats || 0) +
        (preparedData?.preparedValues?.registration || 0) +
        (preparedData?.preparedValues?.trackerCost || 0) +
        (preparedData?.preparedValues?.tLMCost || 0) +
        (preparedData?.preparedValues?.nsibBrutto || 0) +
        (preparedData?.preparedValues?.insuranceFinGAPNmper || 0),
    );
    this.setValue(
      'resultFirstPayment',
      (preparedData?.preparedValues?.firstPaymentSum || 0) *
        (1 + valuesConstants.VAT) -
        this.getValue('subsidySum') || 0,
    );
    this.setValue(
      'resultLastPayment',
      (preparedData?.preparedValues?.lastPaymentSum || 0) *
        (1 + valuesConstants.VAT),
    );

    this.setValue('resultTerm', preparedData?.preparedValues?.nmper || '');
    this.setValue(
      'resultAB_FL',
      (((preparedData?.preparedValues?.agentsSum || 0) +
        (preparedData?.preparedValues?.doubleAgentsSum || 0)) /
        valuesConstants.ESN) *
        (1 - valuesConstants.NDFL),
    );
    this.setValue(
      'resultAB_UL',
      ((preparedData?.preparedValues?.deliverySum || 0) +
        (preparedData?.preparedValues?.brokerSum || 0) +
        (preparedData?.preparedValues?.brokerOfDeliverySum || 0) +
        (preparedData?.preparedValues?.financialDeptOfDeliverySum || 0)) *
        (1 + valuesConstants.VAT),
    );
    this.setValue(
      'resultBonusMPL',
      Math.abs(
        ((res?.columns?.npvBonusExpensesColumn?.values[2] || 0) /
          (1 + (preparedData?.preparedValues?.salaryRate || 0))) *
          (1 - valuesConstants.NDFL),
      ),
    );
    this.setValue(
      'resultDopMPLLeasing',
      Math.abs(
        ((res?.columns?.extraBonusSumColumn?.values[2] || 0) /
          (1 + (preparedData?.preparedValues?.salaryRate || 0))) *
          (1 - valuesConstants.NDFL),
      ),
    );
    this.setValue(
      'resultBonusDopProd',
      Math.abs(
        ((res?.columns?.npvBonusExpensesColumn?.values[1] || 0) /
          (1 + (preparedData?.preparedValues?.salaryRate || 0))) *
          (1 - valuesConstants.NDFL),
      ),
    );
    this.setValue(
      'resultBonusSafeFinance',
      (preparedData?.preparedValues?.bonusFinGAP || 0) *
        (1 - valuesConstants.NDFL),
    );
    this.setValue(
      'resultFirstPaymentRiskPolicy',
      (preparedData?.preparedValues?.firstPayment || 0) * 100,
    );
    const { preparedValues, columns } = res;
    this.setValue(
      'resultParticipationAmount',
      (preparedValues.niAtInception || 0) +
        ((preparedValues.ratBonus || 0) +
          (preparedValues.nsBonus || 0) +
          (preparedValues.nsibBonus || 0)) *
          (preparedValues.marketRate || 0) *
          (preparedValues.districtRate || 0) +
        Math.abs(columns.npvBonusExpensesColumn.values.at(0)) +
        Math.abs(columns.extraBonusSumColumn.values.at(0)) +
        (preparedValues.importerSum || 0) +
        (preparedValues.agentsSum || 0) +
        (preparedValues.extraBonusSumColumn || 0) +
        (preparedValues.deliverySum || 0) +
        (preparedValues.brokerSum || 0) +
        (preparedValues.brokerOfDeliverySum || 0) +
        (preparedValues.financialDeptOfDeliverySum || 0),
    );
  },
  setResValues: function (
    this: ICalculationStore,
    res: GetCalculationResponse,
  ) {
    this.setValue(
      'IRR_Perc',
      (res?.columns?.cashflowMsfoColumn?.nominal || 0) * 100,
    );
    this.setValue(
      'totalPayments',
      (res?.columns?.sumWithVatColumn?.values[0] || 0) -
        (this.getValue('subsidySum') || 0),
    );
    if (this.getValue('lastPaymentRule') === 100_000_002)
      this.setValue(
        'lastPaymentRub',
        res?.columns?.sumWithVatColumn?.values.pop(),
      );
  },
};
