import { openNotification } from 'client/Elements/Notification';
import { IAutorunEffect } from 'core/types/Calculation/Store/effect';
import { ElementStatus } from 'types/elements';
import { convertPrice } from './lib/tools';

const autorunEffects: IAutorunEffect[] = [
  // calculationStore => () => {
  //   const {
  //     leaseObjectCategory,
  //     maxMass,
  //     withTrailer,
  //   } = calculationStore.values;
  //   if (leaseObjectCategory === 100000002) {
  //     if (!maxMass) {
  //       calculationStore.setValidation('tbxMaxMass', false);
  //     } else {
  //       calculationStore.setValidation('tbxMaxMass', true);
  //     }
  //   } else {
  //     calculationStore.setValidation('tbxMaxMass', true);
  //     calculationStore.setValidation('cbxWithTrailer', true);
  //   }
  // },
  // calculationStore => () => {
  //   const { leaseObjectCategory, countSeats } = calculationStore.values;
  //   if (leaseObjectCategory === 100000003) {
  //     if (!countSeats) {
  //       calculationStore.setValidation('tbxCountSeats', false);
  //     } else {
  //       calculationStore.setValidation('tbxCountSeats', true);
  //     }
  //   } else {
  //     calculationStore.setValidation('tbxCountSeats', true);
  //   }
  // // },

  calculationStore => () => {
    const { leaseObjectType: leaseObjectTypeId } = calculationStore.values;
    if (!leaseObjectTypeId) {
      return;
    }

    const leaseObjectType = calculationStore.getOption(
      'selectLeaseObjectType',
      {
        evo_leasingobject_typeid: leaseObjectTypeId,
      },
    );

    if (leaseObjectType) {
      if (leaseObjectType.evo_id !== '8') {
        calculationStore.setStatus('selectEngineType', ElementStatus.Default);
        calculationStore.setStatus('tbxEngineVolume', ElementStatus.Default);
        calculationStore.setStatus(
          'tbxLeaseObjectMotorPower',
          ElementStatus.Default,
        );

        const { engineVolume, engineType, leaseObjectMotorPower } =
          calculationStore.values;

        if (engineVolume <= 0) {
          calculationStore.setValidation('tbxEngineVolume', false);
          openNotification({
            type: 'error',
            message: 'Ошибка',
            description: 'Объем двигателя должен быть больше 0',
          });
        } else {
          calculationStore.setValidation('tbxEngineVolume', undefined);
        }

        if (!engineType) {
          calculationStore.setValidation('selectEngineType', false);
          openNotification({
            type: 'error',
            message: 'Ошибка',
            description: 'Тип двигателя должен содержать данные',
          });
        } else {
          calculationStore.setValidation('selectEngineType', undefined);
        }

        if (leaseObjectMotorPower <= 0) {
          calculationStore.setValidation('tbxLeaseObjectMotorPower', false);
          openNotification({
            type: 'error',
            message: 'Ошибка',
            description: 'Мощность двигателя должна быть больше 0',
          });
        } else {
          calculationStore.setValidation('tbxLeaseObjectMotorPower', undefined);
        }
      } else {
        calculationStore.setValue('leaseObjectMotorPower', 0);
        calculationStore.setStatus(
          'tbxLeaseObjectMotorPower',
          ElementStatus.Disabled,
        );
        calculationStore.setValidation('tbxLeaseObjectMotorPower', undefined);

        calculationStore.setValue('engineType', null);
        calculationStore.setStatus('selectEngineType', ElementStatus.Disabled);
        calculationStore.setValidation('selectEngineType', undefined);

        calculationStore.setValue('engineVolume', 0);
        calculationStore.setStatus('tbxEngineVolume', ElementStatus.Disabled);
        calculationStore.setValidation('tbxEngineVolume', undefined);
      }
    }
  },

  // TODO: move to computed
  // TODO: для нового калькулятора: перенести в process/subsidy

  calculationStore => () => {
    const subsidy = calculationStore.getOption('selectSubsidy');
    if (subsidy?.evo_subsidy_summ && subsidy?.evo_subsidy_summ > 0) {
      calculationStore.setValue('subsidySum', subsidy?.evo_subsidy_summ);
    } else {
      if (subsidy?.evo_max_subsidy_summ) {
        const {
          leaseObjectPrice,
          supplierDiscountRub,
          addEquipmentPrice = 0,
        } = calculationStore.values;
        const supplierCurrency = calculationStore.getOption(
          'selectSupplierCurrency',
        );
        const evo_currencychanges =
          calculationStore.getStaticData('evo_currencychange');
        const evo_currencychange = evo_currencychanges.find(
          x =>
            x.evo_ref_transactioncurrency ===
            supplierCurrency?.transactioncurrencyid,
        );
        const plPriceRub = convertPrice(
          supplierCurrency?.isocurrencycode,
          leaseObjectPrice -
            supplierDiscountRub -
            // @ts-ignore
            calculationStore.importProgramSum() +
            addEquipmentPrice,
          evo_currencychange?.evo_currencychange,
        );

        const subsidySum =
          (plPriceRub * (subsidy?.evo_percent_subsidy || 0)) / 100;

        const subsidySumValue =
          subsidySum > subsidy?.evo_max_subsidy_summ
            ? subsidy?.evo_max_subsidy_summ
            : subsidySum;

        calculationStore.setValue('subsidySum', subsidySumValue);
      } else {
        calculationStore.setValue('subsidySum', 0);
      }
    }
  },
];

export default autorunEffects;
