import { IAccount } from 'core/services/CrmService/types/entities';
import { currentDate } from 'core/tools/date';
import { ICalculationStore } from 'core/types/Calculation/Store';
import { get, isNull } from 'lodash';

const mapCategory = {
  100000000: 'A',
  100000001: 'B',
  100000002: 'C',
  100000003: 'D',
  100000004: 'ПРОЧИЕ ТС',
};

const mapSubCategoryBuilder = (leaseObjectUseFor, maxMass, countSeats) => ({
  100000000: () => '0',
  100000001: () => {
    if (leaseObjectUseFor === 100000001) {
      return '11';
    }
    return '10';
  },
  100000002: () => {
    if (maxMass <= 16000) {
      return '20';
    }
    return '21';
  },
  100000003: () => {
    if (leaseObjectUseFor === 100000001) {
      return '32';
    }
    if (countSeats <= 20) {
      return 30;
    }
    return '31';
  },
  100000004: () => '22',
});

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

export default function (this: ICalculationStore, company: IAccount) {
  const city = this.getOption('selectTownRegistration');
  let kladr = '7700000000000';
  if (this.getValue('objectRegistration') === 100000001) {
    kladr = city?.evo_kladr_id || kladr;
  } else {
    if (company.evo_legal_region_calc === true) {
      const legalClientRegion = this.getOption('selectLegalClientRegion');
      const legalClientTown = this.getOption('selectLegalClientTown');
      kladr =
        legalClientTown?.evo_kladr_id ||
        legalClientRegion?.evo_kladr_id ||
        kladr;
    }
  }
  const brandId = this.getOption('selectBrand')?.evo_id;
  const modelId = this.getOption('selectModel')?.evo_id;

  const mark = this.getOption('selectGPSBrand')?.evo_id;
  const model = this.getOption('selectGPSModel')?.evo_id;
  const vehicleYear = this.getValue('leaseObjectYear') + '';
  const leaseObjectCategory = this.getValue('leaseObjectCategory');
  const vehiclePower = parseFloat(
    this.getValue('leaseObjectMotorPower') || '0',
  );

  let category = '0';
  if (Object.keys(mapCategory).includes(`${leaseObjectCategory}`)) {
    category = mapCategory[`${leaseObjectCategory}`];
  }

  const leaseObjectUseFor = this.getValue('leaseObjectUseFor');
  const maxMass = this.getValue('maxMass');
  const countSeats = this.getValue('countSeats');

  const mapSubCategory = mapSubCategoryBuilder(
    leaseObjectUseFor,
    maxMass,
    countSeats,
  );

  const subCategory = get(mapSubCategory, leaseObjectCategory, () => '0')();

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

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

  const useWithTrailer = this.getValue('withTrailer');
  const useWithTrailerSpecified = true;

  const address = {
    resident: 1,
    country: 'Россия',
    region: 'Москва',
    // district: '0',
    city: 'Москва',
    cityKladr: '7700000000000',
    street: 'ул. Котляковская',
    house: '8',
    flat: '337',
  };

  const owner = {
    JuridicalName: 'ООО "ЛК "ЭВОЛЮЦИЯ"',
    inn: '9724016636',
    kpp: '772401001',
    ogrn: '1207700245037',
    registrationAddress: address,
    factAddress: address,
    phone: '8 (800) 333-75-75',
    email: 'client@evoleasing.ru',
    subjectType: 1,
    subjectTypeSpecified: true,
    opf: 1,
    opfSpecified: true,
  };

  const inn = this.getValue('INNForCalc').toString();

  const osagoRow = this.getTableRowValues(
    'tableInsurance',
    rows => rows.findIndex(x => x?.key === 'osago'),
    'value',
  );

  return {
    preparams: {
      kladr,
      brandId,
      modelId,
    },
    ELTParams: {
      driversCount: 0,
      tsToRegistrationPlace: 0,
      contractBeginDate: currentDate,
      duration: 12,
      insurerType: 1,
      ownerType: 1,
      contractOptionId: 1,
      contractStatusId: 13,
      carInfo: {
        mark,
        model,
        vehicleYear,
        vehiclePower,
        tsType: { category, subCategory },
        vehicle: {
          seatingCapacity,
          seatingCapacitySpecified,
          maxAllowedMass,
          maxAllowedMassSpecified,
        },
        useWithTrailer,
        useWithTrailerSpecified,
      },
      owner,
      FullDriversInfo: [
        {
          kbm: '3',
        },
      ],
      lessee: {
        inn,
        SubjectType: 1,
        SubjectTypeSpecified: true,
      },
      insurer: {
        SubjectType: 1,
        SubjectTypeSpecified: true,
        INN: osagoRow?.insured === 100_000_000 ? inn : '9724016636',
      },
    },
  };
}
