import { ICalculationStore } from 'core/types/Calculation/Store';

/**
 * Добавить валидацию на кнопку Рассчитать:
    если tbxDealerRewardSumm > 0 и
    если selectDealerPerson = selectDealerBroker и tbxDealerBrokerRewardSumm > 0, то ругаться на selectDealerPerson
    если selectDealerPerson = selectIndAgent и tbxIndAgentRewardSumm > 0, то ругаться на selectDealerPerson
    если selectDealerPerson = selectCalcDoubleAgent и tbxCalcDoubleAgentRewardSumm > 0, то ругаться на selectDealerPerson
    если selectDealerPerson = selectCalcBroker tbxCalcBrokerRewardSum > 0, то ругаться на selectDealerPerson
    если selectDealerPerson = selectFinDepartment и tbxFinDepartmentRewardSumm > 0, то ругаться на selectDealerPerson
    2.если tbxDealerBrokerRewardSumm  > 0 и

    если selectDealerBroker = selectDealerPerson и tbxDealerRewardSumm > 0, то ругаться на selectDealerBroker
    если selectDealerBroker = selectIndAgent и tbxIndAgentRewardSumm > 0, то ругаться на selectDealerBroker
    если selectDealerBroker = selectCalcDoubleAgent и tbxCalcDoubleAgentRewardSumm > 0, то ругаться на selectDealerBroker
    если selectDealerBroker = selectCalcBroker tbxCalcBrokerRewardSum > 0, то ругаться на selectDealerBroker
    если selectDealerBroker = selectFinDepartment и tbxFinDepartmentRewardSumm > 0, то ругаться на selectDealerBroker
    3. если tbxIndAgentRewardSumm > 0 и

    если selectIndAgent = selectDealerPerson и tbxDealerRewardSumm > 0, то ругаться на selectIndAgent
    если selectIndAgent = selectDealerBroker и tbxDealerBrokerRewardSumm > 0, то ругаться на selectIndAgent
    если selectIndAgent = selectCalcDoubleAgent и tbxCalcDoubleAgentRewardSumm > 0, то ругаться на selectIndAgent
    если selectIndAgent = selectCalcBroker tbxCalcBrokerRewardSum > 0, то ругаться на selectIndAgent
    если selectIndAgent = selectFinDepartment и tbxFinDepartmentRewardSumm > 0, то ругаться на selectIndAgent
    4. если tbxCalcDoubleAgentRewardSumm > 0 и

    если selectCalcDoubleAgent = selectDealerPerson и tbxDealerRewardSumm > 0, то ругаться на selectCalcDoubleAgent
    если selectCalcDoubleAgent = selectDealerBroker и tbxDealerBrokerRewardSumm > 0, то ругаться на selectCalcDoubleAgent
    если selectCalcDoubleAgent = sselectIndAgent и tbxIndAgentRewardSumm > 0, то ругаться на selectCalcDoubleAgent
    если selectCalcDoubleAgent = selectCalcBroker tbxCalcBrokerRewardSum > 0, то ругаться на selectCalcDoubleAgent
    если selectCalcDoubleAgent = selectFinDepartment и tbxFinDepartmentRewardSumm > 0, то ругаться на selectCalcDoubleAgent
    5. если tbxCalcBrokerRewardSum > 0 и

    если selectCalcBroker = selectDealerPerson и tbxDealerRewardSumm > 0, то ругаться на selectCalcBroker
    если selectCalcBroker = selectDealerBroker и tbxDealerBrokerRewardSumm > 0, то ругаться на selectCalcBroker
    если selectCalcBroker = sselectIndAgent и tbxIndAgentRewardSumm > 0, то ругаться на selectCalcBroker
    если selectCalcBroker = selectCalcDoubleAgent и tbxCalcDoubleAgentRewardSumm > 0, то ругаться на selectCalcBroker
    если selectCalcBroker = selectFinDepartment и tbxFinDepartmentRewardSumm > 0, то ругаться на selectCalcBroker
    6. если tbxFinDepartmentRewardSumm > 0 и

    если selectFinDepartment = selectDealerPerson и tbxDealerRewardSumm > 0, то ругаться на selectFinDepartment
    если selectFinDepartment = selectDealerBroker и tbxDealerBrokerRewardSumm > 0, то ругаться на selectFinDepartment
    если selectFinDepartment = sselectIndAgent и tbxIndAgentRewardSumm > 0, то ругаться на selectFinDepartment
    если selectFinDepartment = selectCalcDoubleAgent и tbxCalcDoubleAgentRewardSumm > 0, то ругаться на selectFinDepartment
    если selectFinDepartment = selectCalcBroker tbxCalcBrokerRewardSum > 0, то ругаться на selectFinDepartment
 */
export default function customValidation($calculation: ICalculationStore) {
  const dealerRewardSumm = $calculation.getValue('dealerRewardSumm');
  const dealerBrokerRewardSumm = $calculation.getValue(
    'dealerBrokerRewardSumm',
  );
  const indAgentRewardSumm = $calculation.getValue('indAgentRewardSumm');
  const calcDoubleAgentRewardSumm = $calculation.getValue(
    'calcDoubleAgentRewardSumm',
  );
  const calcBrokerRewardSum = $calculation.getValue('calcBrokerRewardSum');
  const finDepartmentRewardSumm = $calculation.getValue(
    'finDepartmentRewardSumm',
  );

  const dealerPerson = $calculation.getValue('dealerPerson');
  const dealerBroker = $calculation.getValue('dealerBroker');
  const indAgent = $calculation.getValue('indAgent');
  const calcDoubleAgent = $calculation.getValue('calcDoubleAgent');
  const calcBroker = $calculation.getValue('calcBroker');
  const calcFinDepartment = $calculation.getValue('calcFinDepartment');

  /**
   * В валидацию на кнопку Рассчитать внести изменение:
    1) поле selectDealerPerson убрать из списка обязательных для расчета полей
    2) добавить валидацию на поле selectDealerPerson :
    Если в поле selectDealer указан account, у которого evo_return_leasing_dealer = False (или null) и поле selectDealerPerson = null, то выводить ошибку и поле selectDealerPerson обводить красной рамкой, иначе все ок
   */

  const dealer = $calculation.getOption('selectDealer');
  const isReturnLeasing = dealer?.evo_return_leasing_dealer;

  if (
    (dealerRewardSumm > 0 &&
      dealerPerson &&
      ((dealerPerson === dealerBroker && dealerBrokerRewardSumm > 0) ||
        (dealerPerson === indAgent && indAgentRewardSumm > 0) ||
        (dealerPerson === calcDoubleAgent && calcDoubleAgentRewardSumm > 0) ||
        (dealerPerson === calcBroker && calcBrokerRewardSum > 0) ||
        (dealerPerson === calcFinDepartment && finDepartmentRewardSumm > 0))) ||
    // TODO: последнее условие сделать отдельно
    (!isReturnLeasing && !dealerPerson)
  ) {
    $calculation.setValidation('selectDealerPerson', false);
  } else {
    $calculation.setValidation('selectDealerPerson', undefined);
  }

  if (
    dealerBrokerRewardSumm > 0 &&
    dealerBroker &&
    ((dealerBroker === dealerPerson && dealerRewardSumm > 0) ||
      (dealerBroker === indAgent && indAgentRewardSumm > 0) ||
      (dealerBroker === calcDoubleAgent && calcDoubleAgentRewardSumm > 0) ||
      (dealerBroker === calcBroker && calcBrokerRewardSum > 0) ||
      (dealerBroker === calcFinDepartment && finDepartmentRewardSumm > 0))
  ) {
    $calculation.setValidation('selectDealerBroker', false);
  } else {
    $calculation.setValidation('selectDealerBroker', undefined);
  }

  if (
    indAgentRewardSumm > 0 &&
    indAgent &&
    ((indAgent === dealerPerson && dealerRewardSumm > 0) ||
      (indAgent === dealerBroker && dealerBrokerRewardSumm > 0) ||
      (indAgent === calcDoubleAgent && calcDoubleAgentRewardSumm > 0) ||
      (indAgent === calcBroker && calcBrokerRewardSum > 0) ||
      (indAgent === calcFinDepartment && finDepartmentRewardSumm > 0))
  ) {
    $calculation.setValidation('selectIndAgent', false);
  } else {
    $calculation.setValidation('selectIndAgent', undefined);
  }

  if (
    calcDoubleAgentRewardSumm > 0 &&
    calcDoubleAgent &&
    ((calcDoubleAgent === dealerPerson && dealerRewardSumm > 0) ||
      (calcDoubleAgent === dealerBroker && dealerBrokerRewardSumm > 0) ||
      (calcDoubleAgent === indAgent && indAgentRewardSumm > 0) ||
      (calcDoubleAgent === calcBroker && calcBrokerRewardSum > 0) ||
      (calcDoubleAgent === calcFinDepartment && finDepartmentRewardSumm > 0))
  ) {
    $calculation.setValidation('selectCalcDoubleAgent', false);
  } else {
    $calculation.setValidation('selectCalcDoubleAgent', undefined);
  }

  if (
    calcBrokerRewardSum > 0 &&
    calcBroker &&
    ((calcBroker === dealerPerson && dealerRewardSumm > 0) ||
      (calcBroker === dealerBroker && dealerBrokerRewardSumm > 0) ||
      (calcBroker === indAgent && indAgentRewardSumm > 0) ||
      (calcBroker === calcDoubleAgent && calcDoubleAgentRewardSumm > 0) ||
      (calcBroker === calcFinDepartment && finDepartmentRewardSumm > 0))
  ) {
    $calculation.setValidation('selectCalcBroker', false);
  } else {
    $calculation.setValidation('selectCalcBroker', undefined);
  }

  if (
    finDepartmentRewardSumm > 0 &&
    calcFinDepartment &&
    ((calcFinDepartment === dealerPerson && dealerRewardSumm > 0) ||
      (calcFinDepartment === dealerBroker && dealerBrokerRewardSumm > 0) ||
      (calcFinDepartment === indAgent && indAgentRewardSumm > 0) ||
      (calcFinDepartment === calcDoubleAgent &&
        calcDoubleAgentRewardSumm > 0) ||
      (calcFinDepartment === calcBroker && calcBrokerRewardSum > 0))
  ) {
    $calculation.setValidation('selectCalcFinDepartment', false);
  } else {
    $calculation.setValidation('selectCalcFinDepartment', undefined);
  }
}
