import InputNumber from 'client/Elements/InputNumber';
import Label from 'client/Elements/Label';
import { openNotification } from 'client/Elements/Notification';
import Select from 'client/Elements/Select';
import * as filters from 'core/constants/stores/Calculation/filters';
import { MAX_INSURANCE } from 'core/constants/stores/Calculation/limits';
import { formatNumber } from 'core/tools/format';
import { ElementStatus } from 'types/elements';
import { Column, Table } from '../../types/tables';

const columns: Column[] = [
  {
    name: 'policyType',
    title: 'Тип полиса',
    Component: Label,
  },
  {
    name: 'insuranceCompany',
    title: 'Страховая компания',
    Component: Select,
    props: {
      nameMiddleware: name => {
        const targetName = name.match(/"(.+)"/);
        if (targetName) {
          return targetName[1].replaceAll('"', String.fromCharCode(160)).trim();
        }
        return name;
      },
    },
  },
  {
    name: 'insured',
    title: 'Плательщик',
    Component: Select,
  },
  {
    name: 'insCost',
    title: 'Стоимость за первый период',
    Component: InputNumber,
    props: {
      min: '0.00',
      max: MAX_INSURANCE,
      step: '1000.00',
      precision: 2,
      formatter: formatNumber,
    },
  },
  {
    name: 'insTerm',
    title: 'Срок страхования',
    Component: Select,
  },
];

const rows: Table['rows'] = [
  {
    key: 'osago',
    policyType: {
      value: 'ОСАГО',
    },
    insuranceCompany: {
      value: null,
      filter: filters.insuranceOsagoDefaultFilter,
    },
    insured: {
      value: 100000000,
    },
    insCost: {
      value: 0,
    },
    insTerm: {
      value: 100000000,
      status: ElementStatus.Disabled,
    },
  },
  {
    key: 'kasko',
    policyType: {
      value: 'КАСКО',
    },
    insuranceCompany: {
      value: null,
      filter: filters.insuranceKaskoDefaultFilter,
    },
    insured: {
      value: 100000000,
    },
    insCost: {
      value: 0,
    },
    insTerm: {
      value: null,
      status: ElementStatus.Disabled,
    },
  },
  {
    key: 'dgo',
    policyType: {
      value: 'ДГО',
    },
    insuranceCompany: {
      value: null,
      status: ElementStatus.Disabled,
      filter: filters.insuranceKaskoDefaultFilter,
    },
    insured: {
      value: null,
      status: ElementStatus.Disabled,
    },
    insCost: {
      value: 0,
    },
    insTerm: {
      value: null,
      status: ElementStatus.Disabled,
    },
  },
  {
    key: 'ns',
    policyType: {
      value: 'НС',
    },
    insuranceCompany: {
      value: null,
      status: ElementStatus.Disabled,
      filter: filters.insuranceKaskoDefaultFilter,
    },
    insured: {
      value: null,
      status: ElementStatus.Disabled,
    },
    insCost: {
      value: 0,
    },
    insTerm: {
      value: null,
      status: ElementStatus.Disabled,
    },
  },
  {
    key: 'finGAP',
    policyType: {
      value: 'Safe Finance',
    },
    insuranceCompany: {
      value: null,
      filter: filters.insurancefinGAPDefaultFilter,
    },
    insured: {
      value: 100000000,
    },
    insCost: {
      value: 0,
      status: ElementStatus.Disabled,
    },
    insTerm: {
      value: null,
    },
  },
];

const callbacks: Table['callbacks'] = {
  insCost: ({ calculationStore, tableName, rowIndex }) => {
    if (
      calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value &&
      calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value >
        0 &&
      !calculationStore.tables[tableName]?.rows[rowIndex]['insuranceCompany']
        ?.value
    ) {
      calculationStore.setTableRow(
        'tableInsurance',
        rowIndex,
      )({
        insuranceCompany: {
          validation: false,
        },
      });
      openNotification({
        type: 'error',
        message: 'Ошибка',
        description: 'Не указана страховая компания по полису',
      });
    } else {
      calculationStore.setTableRow(
        'tableInsurance',
        rowIndex,
      )({
        insuranceCompany: {
          validation: true,
        },
      });
    }

    if (
      calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value &&
      calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value >
        0 &&
      !calculationStore.tables[tableName]?.rows[rowIndex]['insTerm']?.value
    ) {
      calculationStore.setTableRow(
        'tableInsurance',
        rowIndex,
      )({
        insTerm: {
          validation: false,
        },
      });
      openNotification({
        type: 'error',
        message: 'Ошибка',
        description: 'Не указан срок страхования',
      });
    } else {
      calculationStore.setTableRow(
        'tableInsurance',
        rowIndex,
      )({
        insTerm: {
          validation: true,
        },
      });
    }

    if (
      calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value &&
      calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value >
        0 &&
      !calculationStore.tables[tableName]?.rows[rowIndex]['insured']?.value
    ) {
      calculationStore.setTableRow(
        'tableInsurance',
        rowIndex,
      )({
        insured: {
          validation: false,
        },
      });
      openNotification({
        type: 'error',
        message: 'Ошибка',
        description: 'Не указан плательщик',
      });
    } else {
      calculationStore.setTableRow(
        'tableInsurance',
        rowIndex,
      )({
        insured: {
          validation: true,
        },
      });
    }
  },

  insured: ({ calculationStore, tableName, rowIndex }) => {
    if (
      calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value &&
      calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value >
        0 &&
      !calculationStore.tables[tableName]?.rows[rowIndex]['insured']?.value
    ) {
      calculationStore.setTableRow(
        'tableInsurance',
        rowIndex,
      )({
        insured: {
          validation: false,
        },
      });
      openNotification({
        type: 'error',
        message: 'Ошибка',
        description: 'Не указан плательщик',
      });
    } else {
      calculationStore.setTableRow(
        'tableInsurance',
        rowIndex,
      )({
        insured: {
          validation: true,
        },
      });
    }
  },

  insTerm: ({ calculationStore, tableName, rowIndex }) => {
    if (
      calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value &&
      calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value >
        0 &&
      !calculationStore.tables[tableName]?.rows[rowIndex]['insTerm']?.value
    ) {
      calculationStore.setTableRow(
        'tableInsurance',
        rowIndex,
      )({
        insTerm: {
          validation: false,
        },
      });
      openNotification({
        type: 'error',
        message: 'Ошибка',
        description: 'Не указан срок страхования',
      });
    } else {
      calculationStore.setTableRow(
        'tableInsurance',
        rowIndex,
      )({
        insTerm: {
          validation: true,
        },
      });
    }
  },
  insuranceCompany: ({ calculationStore, tableName, rowIndex }) => {
    if (
      calculationStore.tables[tableName]?.rows[rowIndex] &&
      calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value &&
      calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value >
        0 &&
      !calculationStore.tables[tableName]?.rows[rowIndex]['insuranceCompany']
        ?.value
    ) {
      calculationStore.setTableRow(
        'tableInsurance',
        rowIndex,
      )({
        insuranceCompany: {
          validation: false,
        },
      });
      openNotification({
        type: 'error',
        message: 'Ошибка',
        description: 'Не указана страховая компания по полису',
      });
    } else {
      calculationStore.setTableRow(
        'tableInsurance',
        rowIndex,
      )({
        insuranceCompany: {
          validation: true,
        },
      });
    }
  },
};

const options: Table['options'] = {
  insured: [
    {
      name: 'ЛП',
      value: 100000000,
    },
    {
      name: 'ЛД',
      value: 100000001,
    },
  ],
  insTerm: [
    {
      name: '12 месяцев',
      value: 100000000,
    },
    {
      name: 'Срок ДЛ',
      value: 100000001,
    },
  ],
};

export default {
  columns,
  rows,
  options,
  callbacks,
  params: {},
} as Table;
