import {
  type State,
  type StatusDetails,
} from 'src/pages/dashboard/serviceManagement/types';
import { type LokaliseTFunction } from 'src/utils/i18n';

export enum ServiceManagementTaskStateName {
  UNASSIGNED = 'UNASSIGNED',
  CLOSED = 'CLOSED',
  ASSIGNED = 'ASSIGNED',
  CANCELLED = 'CANCELLED',
  ON_HOLD = 'ON_HOLD',
  IN_PROGRESS = 'IN_PROGRESS',
}

export const ServiceManagementTaskStateDictionary: {
  [key: string]: ServiceManagementTaskStateName;
} = {
  'SIN ASIGNAR': ServiceManagementTaskStateName.UNASSIGNED,
  UNASSIGNED: ServiceManagementTaskStateName.UNASSIGNED,
  CERRADA: ServiceManagementTaskStateName.CLOSED,
  CLOSED: ServiceManagementTaskStateName.CLOSED,
  ASIGNADA: ServiceManagementTaskStateName.ASSIGNED,
  ASSIGNED: ServiceManagementTaskStateName.ASSIGNED,
  CANCELADA: ServiceManagementTaskStateName.CANCELLED,
  CANCELLED: ServiceManagementTaskStateName.CANCELLED,
  'EN ESPERA': ServiceManagementTaskStateName.ON_HOLD,
  'ON HOLD': ServiceManagementTaskStateName.ON_HOLD,
  'EN PROCESO': ServiceManagementTaskStateName.IN_PROGRESS,
  'IN PROCESS': ServiceManagementTaskStateName.IN_PROGRESS,
};

export const getStateName = (value: string) => {
  return ServiceManagementTaskStateDictionary[value] || undefined;
};

export const getStatusDetails = (status: string): StatusDetails => {
  switch (status) {
    case ServiceManagementTaskStateName.UNASSIGNED:
      return {
        title: 'UNASSIGNED',
        color: '#757575',
        backgroundColor: 'transparent',
      };
    case ServiceManagementTaskStateName.CANCELLED:
      return {
        title: 'CANCELLED',
        color: '#B42318',
        backgroundColor: 'rgba(240, 68, 56, 0.12)',
      };
    case ServiceManagementTaskStateName.ASSIGNED:
      return {
        title: 'ASSIGNED',
        color: '#6941C6',
        backgroundColor: 'rgba(151, 71, 255, 0.12)',
      };
    case ServiceManagementTaskStateName.IN_PROGRESS:
      return {
        title: 'IN_PROGRESS',
        color: '#0E7090',
        backgroundColor: 'rgba(6, 174, 212, 0.12)',
      };
    case ServiceManagementTaskStateName.ON_HOLD:
      return {
        title: 'ON_HOLD',
        color: '#B54708',
        backgroundColor: 'rgba(247, 144, 9, 0.12)',
      };
    case ServiceManagementTaskStateName.CLOSED:
      return {
        title: 'CLOSED',
        color: '#107569',
        backgroundColor: 'rgba(21, 183, 158, 0.12)',
      };
    default:
      return {
        title: '-',
        color: '#757575',
        backgroundColor: 'transparent',
      };
  }
};

export const getStatusPillProps = (state: State, t: LokaliseTFunction) => {
  const stateName = getStateName(state?.name);
  const { title } = getStatusDetails(stateName);

  let type: 'neutral' | 'info' | 'warning' | 'error' | 'success' | 'highlight';
  switch (stateName) {
    case ServiceManagementTaskStateName.UNASSIGNED:
      type = 'neutral';
      break;
    case ServiceManagementTaskStateName.IN_PROGRESS:
      type = 'info';
      break;
    case ServiceManagementTaskStateName.ON_HOLD:
      type = 'warning';
      break;
    case ServiceManagementTaskStateName.CANCELLED:
      type = 'error';
      break;
    case ServiceManagementTaskStateName.CLOSED:
      type = 'success';
      break;
    case ServiceManagementTaskStateName.ASSIGNED:
      type = 'highlight';
      break;
    default:
      type = 'neutral';
  }

  return {
    label: t(title) as string,
    type,
    hasIcon: false,
    size: 'medium' as const,
  };
};
