import { type FC } from 'react';

import HuPills from '@material-hu/components/design-system/Pills';
import { type PillsProps } from '@material-hu/components/design-system/Pills/types';

import {
  ApprovalStateName,
  type State,
} from 'src/pages/dashboard/serviceManagement/types';
import {
  getApprovalStateName,
  getStateName,
  ServiceManagementTaskStateName,
} from 'src/pages/dashboard/serviceManagement/utils';
import { useLokaliseTranslation } from 'src/utils/i18n';
import { capitalizeFirstLetter } from 'src/utils/text';

export const getPillProps = (
  state: State,
): { title: string; type: PillsProps['type'] } => {
  const stateName = getStateName(state.name);
  switch (stateName) {
    case ServiceManagementTaskStateName.ASSIGNED:
      return { title: stateName, type: 'highlight' };
    case ServiceManagementTaskStateName.CLOSED:
      return { title: stateName, type: 'success' };
    case ServiceManagementTaskStateName.CANCELLED:
      return { title: stateName, type: 'error' };
    case ServiceManagementTaskStateName.ON_HOLD:
      return { title: stateName, type: 'warning' };
    case ServiceManagementTaskStateName.IN_PROGRESS:
      return { title: stateName, type: 'info' };
    default:
      return { title: stateName, type: 'neutral' };
  }
};

export const getApprovalPillProps = (
  state: State,
): { title: string; type: PillsProps['type'] } => {
  const stateName = getApprovalStateName(state.name);
  switch (stateName) {
    case ApprovalStateName.APPROVED:
      return { title: stateName, type: 'success' };
    case ApprovalStateName.REJECTED:
      return { title: stateName, type: 'error' };
    case ApprovalStateName.PENDING:
      return { title: stateName, type: 'highlight' };
    case ApprovalStateName.CANCELLED:
      return { title: stateName, type: 'disabled' };
    default:
      return { title: stateName, type: 'neutral' };
  }
};

const StatusBadge: FC<{
  state: State | undefined;
  isApproval?: boolean;
}> = ({ state, isApproval }) => {
  const { t } = useLokaliseTranslation('service_management');

  if (!state) return null;

  const colorType = isApproval
    ? getApprovalPillProps(state)
    : getPillProps(state);
  const titleCapitalized = capitalizeFirstLetter(
    t(colorType.title).toLowerCase(),
  );

  return (
    <HuPills
      label={titleCapitalized}
      type={colorType.type}
      size="medium"
      hasIcon={false}
    />
  );
};

export default StatusBadge;
