import { useFormContext } from 'react-hook-form';

import HuAlert from '@material-hu/components/design-system/Alert';

import {
  AllowanceType,
  BalanceReport,
  OperationsBalance,
  PolicyTypes,
  UnitVacations,
} from 'src/types/vacations';
import { useLokaliseTranslation } from 'src/utils/i18n';
import { calculateRemainingAvailableTime } from 'src/utils/vacations';

type Props = {
  reportItem: BalanceReport;
};

const getTitleKeys = (
  operation: OperationsBalance,
  policyType: PolicyTypes,
) => {
  if (policyType.unit === UnitVacations.HOURS) {
    if (operation === OperationsBalance.ADDITION) {
      return 'adding_hour';
    }
    return 'subtracting_hour';
  }
  if (operation === OperationsBalance.ADDITION) {
    return 'adding_day';
  }
  return 'subtracting_day';
};

const getDescriptionKeys = (
  policyType: PolicyTypes,
  allowanceType?: AllowanceType,
) => {
  if (policyType.unit === UnitVacations.HOURS) {
    if (allowanceType === AllowanceType.BASIC_ANNUAL) {
      return 'final_balance_hour';
    }
    return 'final_time_used_hour';
  }
  if (allowanceType === AllowanceType.BASIC_ANNUAL) {
    return 'final_balance_day';
  }
  return 'final_time_used_day';
};

const AdjustBalanceFooter = ({ reportItem }: Props) => {
  const { watch } = useFormContext();
  const { t } = useLokaliseTranslation('time_off');

  const operation = watch('operation');
  const amount = watch('amount');

  return (
    <HuAlert
      severity="info"
      title={t(getTitleKeys(operation, reportItem.policy.policyType), {
        count: Number(amount),
      })}
      description={t(
        getDescriptionKeys(
          reportItem.policy.policyType,
          reportItem.policy.allowanceType,
        ),
        {
          count: calculateRemainingAvailableTime(
            operation,
            reportItem.amountRequested,
            reportItem.currentBalance!,
            Number(amount),
            reportItem.policy.allowanceType!,
          ),
        },
      )}
    />
  );
};

export default AdjustBalanceFooter;
