import DownloadOutlined from '@ant-design/icons/lib/icons/DownloadOutlined';
import { MAX_FRANCHISE } from 'core/constants/stores/Calculation/limits';
import { currentYear } from 'core/tools/date';
import { formatMoney, formatNumber } from 'core/tools/format';
import { pipe } from 'core/tools/func';
import { round } from 'core/tools/num';
import { ElementProps } from 'types/elements';
import { AllElementsNames, ElementsNames } from '../../types/elements';

const elementsProps: Partial<Record<AllElementsNames, ElementProps>> = {
  tbxLeaseObjectPrice: {
    min: '0.00',
    max: '1000000000.00',
    step: '10000.00',
    precision: 2,
    formatter: formatNumber,
  },
  tbxLeaseObjectPriceWthtVAT: {
    min: '0.00',
    max: '1000000000.00',
    step: '10000.00',
    precision: 2,
    formatter: formatNumber,
  },
  tbxVATInLeaseObjectPrice: {
    min: '0.00',
    max: '1000000000.00',
    step: '10000.00',
    precision: 2,
    formatter: formatNumber,
  },
  tbxEngineHours: {
    min: '0.0',
    step: '10.0',
    precision: 2,
    formatter: formatNumber,
  },
  tbxSupplierDiscountRub: {
    min: '0',
    max: '1000000000',
    step: '10000.00',
    precision: 2,
    formatter: formatNumber,
  },
  tbxSupplierDiscountPerc: {
    min: '0',
    max: '100',
    precision: 2,
    formatter: formatNumber,
  },
  radioBalanceHolder: {
    style: 'button',
  },
  tbxSaleBonus: {
    min: '0.00',
    // max: '1.30',
    step: '0.1',
    precision: 2,
    formatter: formatNumber,
  },
  tbxFirstPaymentPerc: {
    min: '0',
    max: '50',
    precision: 4,
    formatter: formatNumber,
  },
  tbxFirstPaymentRub: {
    min: '0',
    max: '1000000000',
    step: '10000.00',
    precision: 2,
    formatter: formatNumber,
  },
  radioLastPaymentRule: {
    // style: 'button',
  },
  tbxLastPaymentPerc: {
    min: '0',
    max: '70',
    step: '1.000000',
    precision: 6,
    formatter: formatNumber,
  },
  tbxLastPaymentRub: {
    min: '0',
    max: '1000000000',
    step: '10000.00',
    precision: 2,
    formatter: formatNumber,
  },
  tbxRedemptionPaymentSum: {
    min: '1000',
    max: '2000',
    step: '1000.00',
    precision: 2,
    formatter: formatNumber,
  },
  tbxLeasingPeriod: {
    min: '13',
    max: '60',
  },
  tbxParmentsDecreasePercent: {
    min: '50',
    max: '99',
  },
  tbxComissionPerc: {
    min: '0',
    max: '100',
  },
  tbxComissionRub: {
    min: '0',
    max: '1000000000',
    step: '10000.00',
  },
  selectLeaseObjectType: {
    showSearch: true,
  },
  selectBrand: {
    showSearch: true,
  },
  selectModel: {
    showSearch: true,
  },
  selectConfiguration: {
    showSearch: true,
  },
  radioDeliveryTime: {
    showSearch: false,
    style: 'button',
  },
  tbxLeaseObjectCount: {
    min: '1',
    max: '1000',
  },
  selectLeaseObjectUseFor: {
    showSearch: true,
  },
  tbxLeaseObjectYear: {
    min: '1994',
    max: (currentYear + 1).toString(),
  },
  selectLeaseObjectCategory: {
    showSearch: false,
  },
  selectEngineType: {
    showSearch: true,
  },
  tbxLeaseObjectMotorPower: {
    min: '0.00',
    max: '20000.00',
    step: '10.00',
    precision: 2,
    formatter: formatNumber,
  },
  tbxEngineVolume: {
    min: '0.0000',
    max: '99.9999',
    step: '0.5',
    precision: 4,
    formatter: formatNumber,
  },
  tbxMaxMass: {
    min: '0',
    max: '999999',
    step: '100',
    formatter: formatNumber,
  },
  tbxCountSeats: {
    min: '0',
    max: '2000',
  },
  tbxMaxSpeed: {
    min: '0',
    max: '2000',
  },
  selectDealer: {
    showSearch: true,
  },
  tbxDealerRewardSumm: {
    min: '0.00',
    max: '999999.00',
    step: '0.10',
    precision: 2,
    formatter: formatNumber,
  },
  tbxDealerBrokerRewardSumm: {
    min: '0.00',
    max: '999999.00',
    step: '0.10',
    precision: 2,
    formatter: formatNumber,
  },
  tbxIndAgentRewardSumm: {
    min: '0.00',
    max: '999999.00',
    step: '0.10',
    precision: 2,
    formatter: formatNumber,
  },
  tbxCalcDoubleAgentRewardSumm: {
    min: '0.00',
    max: '999999.00',
    step: '0.10',
    precision: 2,
    formatter: formatNumber,
  },
  tbxCalcBrokerRewardSum: {
    min: '0.00',
    max: '999999.00',
    step: '0.10',
    precision: 2,
    formatter: formatNumber,
  },
  tbxFinDepartmentRewardSumm: {
    min: '0.00',
    max: '999999.00',
    step: '0.10',
    precision: 2,
    formatter: formatNumber,
  },
  radioInsKaskoType: {
    style: 'button',
  },
  tbxInsFranchise: {
    min: '0',
    max: MAX_FRANCHISE.toString(),
    step: '10000.00',
    precision: 2,
    formatter: formatNumber,
  },
  tbxInsAgeDrivers: {
    // min: '18',
    // max: '99',
  },
  tbxInsExpDrivers: {
    // min: '0',
    // max: '99',
  },
  selectRegionRegistration: {
    showSearch: true,
  },
  selectTownRegistration: {
    showSearch: true,
  },
  radioQuoteContactGender: {
    style: 'button',
  },
  btnCreateKP: {
    type: 'primary',
    text: 'Создать КП',
  },
  tbxCreditRate: {
    min: '0.00',
    max: '99.99',
    step: '0.1',
  },
  tbxMaxPriceChange: {
    min: '0',
    max: '34999990',
    step: '10000.00',
  },
  tbxMinPriceChange: {
    min: '0',
    max: '34999990',
    step: '10000.00',
  },
  tbxImporterRewardPerc: {
    min: '0.00',
    max: '99.99',
    step: '0.10',
    precision: 2,
  },
  tbxImporterRewardRub: {
    min: '0.00',
    max: '1000000000.00',
    step: '10000.00',
    precision: 2,
  },
  selectLead: {
    showSearch: true,
  },
  selectOpportunity: {
    showSearch: true,
  },
  selectQuote: {
    showSearch: true,
  },
  btnCalculate: {
    text: 'Рассчитать график',
    type: 'primary',
  },
  tbxIRR_Perc: {
    min: '0.0000',
    max: '500.0000',
    step: '0.0001',
    precision: 6,
    formatter: formatNumber,
  },
  linkDownloadKp: {
    type: 'primary',
    text: 'Скачать КП',
    icon: DownloadOutlined,
  },
  tbxMileage: {
    min: 0,
    step: 100.0,
    precision: 2,
    formatter: formatNumber,
  },
  cbxRecalcWithRevision: {
    text: 'Пересчет без пересмотра',
    containerStyle: {
      marginBottom: '8px',
    },
  },
  radioCalcType: {
    style: 'button',
  },
  tbxTotalPayments: {
    min: 0,
    step: 1000,
    precision: 2,
    formatter: formatNumber,
  },
  radioObjectRegistration: {
    // style: 'button',
  },
  radioTypePTS: {
    // style: 'button',
  },
  tbxVehicleTaxInYear: {
    min: '0',
    step: '100',
    max: '9999999',
    precision: 2,
  },
  tbxVehicleTaxInLeasingPeriod: {
    min: '0',
    step: '100',
    max: '9999999',
    precision: 2,
  },
  selectObjectRegionRegistration: {
    showSearch: true,
  },
  tbxInsKaskoPriceLeasePeriod: {
    min: 0,
    precision: 2,
    formatter: formatNumber,
    readOnly: true,
    controls: false,
  },
  selectLegalClientRegion: {
    showSearch: true,
  },
  selectLegalClientTown: {
    showSearch: true,
  },
  radioInfuranceOPF: {
    style: 'button',
  },
  tbxAddEquipmentPrice: {
    min: '0',
    max: '1000000000',
    step: '10000.00',
    precision: 2,
    formatter: formatNumber,
  },
  labelImportProgramSum: {
    middleware: value => formatMoney(value),
  },
};

export const numberElementsProps: Partial<Record<ElementsNames, ElementProps>> =
  Object.keys(elementsProps).reduce((acc, a) => {
    const min = elementsProps[a]?.min,
      max = elementsProps[a]?.max;
    if (min || max)
      return {
        ...acc,
        [a]: { min, max },
      };
    return acc;
  }, {});

const moneyResultElementsProps = (
  [
    'labelResultTotalGraphwithNDS',
    'labelResultPlPrice',
    'labelResultInsKasko',
    'labelResultInsOsago',
    'labelResultDopProdSum',
    'labelResultFirstPayment',
    'labelResultLastPayment',
    'labelResultAB_FL',
    'labelResultAB_UL',
    'labelResultBonusMPL',
    'labelResultDopMPLLeasing',
    'labelResultBonusDopProd',
    'labelSubsidySum',
    'labelResultBonusSafeFinance',
    'labelResultParticipationAmount',
  ] as ElementsNames[]
).reduce(
  (ac, a) => ({
    ...ac,
    //@ts-ignore
    [a]: { middleware: value => pipe(round, formatMoney)(value) },
  }),
  {},
);

const numberResultElementsProps = (
  [
    'labelResultPriceUpPr',
    'labelResultIRRGraphPerc',
    'labelResultIRRNominalPerc',
    'labelResultTerm',
    'labelResultFirstPaymentRiskPolicy',
  ] as ElementsNames[]
).reduce(
  (ac, a) => ({
    ...ac,
    [a]: { middleware: value => round(value) },
  }),
  {},
);

export default Object.assign(
  elementsProps,
  moneyResultElementsProps,
  numberResultElementsProps,
);
