import { message } from 'antd';
import { resetFields as kaskoResetFields } from 'client/Containers/Calculation/Components/ELT/Kasko/lib/validation';
import { cancelRequests } from 'client/Containers/Calculation/Components/ELT/lib/requests';
import {
  initIns,
  resetIns
} from 'client/Containers/Calculation/Components/ELT/lib/resetIns';
import { resetFields as osagoResetFields } from 'client/Containers/Calculation/Components/ELT/Osago/lib/validation';
import { getValueName } from 'client/Containers/Calculation/Elements/tools';
import { ElementsNames } from 'client/Containers/Calculation/types/elements';
import { TCalculationProcess } from 'client/stores/CalculationStore/subStores/calculationProcess';
import { IReactionEffect } from 'core/types/Calculation/Store/effect';
import { ICalculationStore } from 'core/types/Calculation/Store/index';
import { Process } from 'core/types/Calculation/Store/process';

const resetConf = [
  {
    insType: 'osago',
    resetFields: osagoResetFields,
    tableRowNumber: 0,
  },
  {
    insType: 'kasko',
    resetFields: kaskoResetFields,
    tableRowNumber: 1,
  },
];

const RESET_MESSAGES = {
  kasko: 'Расчеты ЭЛТ по КАСКО сброшены',
  osago: 'Расчеты ЭЛТ по ОСАГО сброшены',
};

const eltReactions: IReactionEffect[] = [
  calculationStore => ({
    expression: () => {
      return calculationStore.getOption('selectLeaseObjectType');
    },
    effect: (prevLeaseObjectType, nextLeaseObjectType) => {
      if (
        prevLeaseObjectType?.evo_id === '9' ||
        nextLeaseObjectType?.evo_id === '9'
      )
        initIns.call(calculationStore, 'kasko');
    },
  }),
  ...resetConf.map(
    ({ insType, resetFields }) =>
      (
        calculationStore: ICalculationStore,
        calculationProcess: TCalculationProcess,
      ) => ({
        expression: () => {
          return calculationStore.getValues(
            (resetFields as ElementsNames[]).map(getValueName),
          );
        },
        effect: () => {
          cancelRequests(insType);

          if (
            calculationProcess.hasProcess(Process.Init) ||
            calculationProcess.hasProcess(Process.ELT) ||
            calculationProcess.hasProcess(Process.LoadKp)
          ) {
            return;
          }

          const { ELTStore } = calculationStore.stores;
          if (ELTStore[insType]?.isReseted()) {
            return;
          }
          message.warn({ content: RESET_MESSAGES[insType] });
          resetIns.call(calculationStore, insType);
        },
      }),
  ),

  ...resetConf.map(
    ({ insType, tableRowNumber }) =>
      (
        calculationStore: ICalculationStore,
        calculationProcess: TCalculationProcess,
      ) => ({
        expression: () => {
          return [
            ...['insuranceCompany', 'insCost'].map(
              fieldName =>
                calculationStore.tables.tableInsurance.rows[tableRowNumber][
                  fieldName
                ].value,
            ),
          ];
        },
        effect: () => {
          cancelRequests(insType);

          if (
            calculationProcess.hasProcess(Process.Init) ||
            calculationProcess.hasProcess(Process.ELT) ||
            calculationProcess.hasProcess(Process.LoadKp)
          ) {
            return;
          }

          const { ELTStore } = calculationStore.stores;
          if (ELTStore[insType]?.isReseted()) {
            return;
          }
          message.warn({ content: RESET_MESSAGES[insType] });
          resetIns.call(calculationStore, insType);
        },
      }),
  ),
];

export default eltReactions;
