import { currentDate } from 'core/tools/date';
import { ICalculationStore } from 'core/types/Calculation/Store/index';
import { get, isNull } from 'lodash';

const mapEngineType = {
  100000000: '0',
  100000001: '1',
  100000003: '2',
  100000004: '3',
};

const mapVehicleUsage = {
  100000000: 0,
  100000001: 1,
  100000002: 5,
  100000003: 5,
  100000004: 2,
  100000005: 6,
  100000006: 5,
  100000007: 4,
  100000008: 4,
  100000009: 0,
  100000010: 0,
  100000011: 3,
  100000012: 3,
  100000013: 9,
};

const mapCategory = {
  100000000: 'A',
  100000001: 'B',
  // 100000002: 'C2',
  100000003: 'D',
  100000004: 'E1',
};

const mapInfuranceToLesseSubjectType = {
  100000000: 1,
  100000001: 2,
};

const mapLeaseObjectUseForToIndustry = {
  100000014: 30,
  100000015: 15,
  100000016: 3,
  100000017: 26,
  100000018: 2,
  100000019: 6,
};

const getSpecified = value => !isNull(value);

export default function (this: ICalculationStore) {
  const region = this.getOption('selectLegalClientRegion');
  const city = this.getOption('selectLegalClientTown');
  const kladr = get(city, 'evo_kladr_id') || get(region, 'evo_kladr_id');
  const leaseObjectType = this.getOption('selectLeaseObjectType');
  const leaseObjectCategory = this.getValue('leaseObjectCategory');

  const brand = this.getOption('selectBrand');
  let brandId = brand?.evo_id;
  const model = this.getOption('selectModel');
  let modelId = model?.evo_id;

  const leaseObjectUsed = this.getValue('leaseObjectUsed');
  const product = this.getOption('selectProduct');
  const leaseObjectYear = this.getValue('leaseObjectYear');
  let isNew = true;
  if (
    leaseObjectUsed === true ||
    (leaseObjectUsed === false &&
      product?.evo_sale_without_nds === true &&
      leaseObjectYear !== currentDate.getFullYear())
  ) {
    isNew = false;
  }

  const vehicleYear = this.getValue('leaseObjectYear');
  let vehicleDate;
  if (
    leaseObjectUsed === true ||
    (leaseObjectUsed === false &&
      product?.evo_sale_without_nds === true &&
      leaseObjectYear !== currentDate.getFullYear())
  ) {
    vehicleDate = new Date(`${vehicleYear}-01-01`);
  }
  let vehicleDateSpecified = getSpecified(vehicleDate);

  const power = this.getValue('leaseObjectMotorPower');
  const powerSpecified = getSpecified(power);

  let country = 0,
    countrySpecified = false;
  if (
    leaseObjectCategory === 100000002 ||
    (leaseObjectType?.evo_id &&
      ['6', '9', '10', '8'].includes(leaseObjectType?.evo_id))
  )
    if (brand?.evo_brand_owner === 100000001) {
      country = 1;
      countrySpecified = true;
    }

  let engineType = '5';
  const engineTypeValue = this.getValue('engineType');
  if (engineTypeValue) {
    engineType = mapEngineType[engineTypeValue];
  }

  let duration = this.getValue('leasingPeriod');
  if (duration < 12) {
    duration = 12;
  }
  const cost =
    //@ts-ignore
    (this.plPriceRub() || 0) -
    (this.getValue('supplierDiscountRub') || 0) -
    //@ts-ignore
    (this.importProgramSum() || 0) +
    (this.getValue('addEquipmentPrice') || 0);

  let notConfirmedGlassesDamages,
    notConfirmedGlassesDamagesSpecified = false,
    notConfirmedDamages,
    notConfirmedDamagesSpecified = false,
    selfIgnition,
    selfIgnitionSpecified,
    outsideRoads,
    outsideRoadsSpecified = false;
  if (
    leaseObjectCategory === 100000002 ||
    (leaseObjectType?.evo_id &&
      ['6', '9', '10'].includes(leaseObjectType?.evo_id))
  ) {
    notConfirmedGlassesDamages = 3;
    notConfirmedGlassesDamagesSpecified = true;
    notConfirmedDamages = 2;
    notConfirmedDamagesSpecified = true;
    selfIgnition = true;
    selfIgnitionSpecified = getSpecified(selfIgnition);
    outsideRoads = true;
    outsideRoadsSpecified = true;
  }

  const franchise = parseInt(this.getValue('insFranchise') || 0);
  const franchiseSpecified = getSpecified(franchise);
  const puuMark = this.getOption('selectGPSBrand')?.evo_id;
  const puuModel = this.getOption('selectGPSModel')?.evo_id;
  const puuModelSpecified = getSpecified(puuModel);

  let age = this.getValue('insAgeDrivers'),
    experience = this.getValue('insExpDrivers'),
    sex = '0',
    driversCount = 1;

  const risk =
    leaseObjectType?.evo_id &&
    ['6', '9', '10'].includes(leaseObjectType?.evo_id)
      ? 3
      : 0;

  if (this.getValue('insUnlimitDrivers')) {
    age = 18;
    experience = 0;
    driversCount = 0;
  }
  const sexSpecified = getSpecified(sex);

  let maxAllowedMass = 0;
  if (leaseObjectCategory === 100000002) {
    maxAllowedMass = this.getValue('maxMass');
  }
  const maxAllowedMassSpecified = getSpecified(maxAllowedMass);

  let mileage = 0;
  if (leaseObjectUsed === true) {
    mileage = this.getValue('mileage');
  }

  if (
    leaseObjectUsed === false &&
    product?.evo_sale_without_nds === true &&
    leaseObjectYear !== currentDate.getFullYear()
  ) {
    mileage = 0;
  }

  let vin;

  if (leaseObjectUsed === true) {
    vin = this.getValue('vin');
  }

  const mileageSpecified = getSpecified(mileage);

  let vehicleUsage = 0;
  const leaseObjectUseForValue = this.getValue('leaseObjectUseFor');
  if (leaseObjectUseForValue) {
    vehicleUsage = mapVehicleUsage[leaseObjectUseForValue] || 0;
  }
  const vehicleUsageSpecified = getSpecified(vehicleUsage);

  let seatingCapacity = 0;
  if (leaseObjectCategory === 100000003) {
    seatingCapacity = this.getValue('countSeats');
  }
  const seatingCapacitySpecified = getSpecified(seatingCapacity);

  let category = mapCategory[leaseObjectCategory];
  if (leaseObjectCategory === 100000002)
    switch (leaseObjectType?.evo_id) {
      case '7': {
        category = 'C1';
        break;
      }
      case '3': {
        category = 'C3';
        break;
      }
      default: {
        category = 'C2';
        break;
      }
    }

  const classification = [100000002, 100000003, 100000004].includes(
    this.getValue('leaseObjectCategory'),
  )
    ? '11635'
    : '0';

  const INN = `${this.getValue('INNForCalc')}`;

  const infuranceOPF = this.getValue('infuranceOPF');
  const lesseSubjectType = get(mapInfuranceToLesseSubjectType, infuranceOPF, 0);

  let specialMachineryType = 0,
    specialMachineryIndustry = 0,
    specialMachineryMover = 0;
  if (
    leaseObjectType?.evo_id &&
    ['6', '9', '10'].includes(leaseObjectType?.evo_id)
  ) {
    specialMachineryType = parseInt(
      model?.evo_vehicle_body_typeidData?.evo_id_elt || '0',
    );
    specialMachineryIndustry =
      mapLeaseObjectUseForToIndustry[leaseObjectUseForValue];
    specialMachineryMover = model?.evo_running_gear === 100000001 ? 2 : 1;
  }

  return {
    preparams: {
      kladr,
      brandId,
      modelId,
      specialMachinery: {
        type: specialMachineryType,
        typeSpecified: getSpecified(specialMachineryType),
        industry: specialMachineryIndustry,
        industrySpecified: getSpecified(specialMachineryIndustry),
        mover: specialMachineryMover,
        moverSpecified: getSpecified(specialMachineryMover),
      },
    },
    ELTParams: {
      currency: 'RUR',
      isNew,
      usageStart: currentDate,
      vehicleYear,
      vehicleDate,
      vehicleDateSpecified,
      modification: {
        power,
        powerSpecified,
        engineType,
        engineVolume: 0,
        engineVolumeSpecified: true,
        KPPTypeId: 1,
        country,
        countrySpecified,
      },
      duration,
      bankId: '245',
      cost,
      franchise,
      franchiseSpecified,
      ssType: '1',
      STOA: '0',
      OfficialDealerSpecified: true,
      OfficialDealer: true,
      notConfirmedGlassesDamages,
      notConfirmedGlassesDamagesSpecified,
      notConfirmedDamages,
      notConfirmedDamagesSpecified,
      selfIgnition,
      selfIgnitionSpecified,
      outsideRoads,
      outsideRoadsSpecified,
      PUUs: puuMark && [
        {
          mark: puuMark,
          model: puuModel,
          modelSpecified: puuModelSpecified,
        },
      ],
      driversCount,
      approvedDriving: 2,
      approvedDrivingSpecified: true,
      risk,
      payType: '0',
      vehicle: {
        vin,
        maxAllowedMass,
        maxAllowedMassSpecified,
        mileage,
        mileageSpecified,
        vehicleUsage,
        vehicleUsageSpecified,
        seatingCapacity,
        seatingCapacitySpecified,
        category,
        classification,
      },
      drivers: [{ age, experience, sex, sexSpecified }],
      Insurer: {
        SubjectType: 1,
        SubjectTypeSpecified: true,
      },
      Owner: {
        SubjectType: 1,
        SubjectTypeSpecified: true,
      },
      Lessee: {
        SubjectType: lesseSubjectType,
        SubjectTypeSpecified: true,
        INN,
      },
      // FullDriversInfo: [
      //   {
      //     surname,
      //     name,
      //     patronymic,
      //     sex,
      //     sexSpecified,
      //     expertienceStart,
      //   },
      // ],
    },
  };
}
