import AccessTimeIcon from '@material-hu/icons/material/AccessTime';
import ErrorOutlineOutlinedIcon from '@material-hu/icons/material/ErrorOutlineOutlined';
import PauseCircleOutlineOutlinedIcon from '@material-hu/icons/material/PauseCircleOutlineOutlined';
import {
  IconClock,
  IconExclamationCircle,
  IconPlayerPause,
} from '@material-hu/icons/tabler';

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

import { MILISECONDS } from 'src/constants/date';
import {
  ServiceItemSla,
  type SLA,
  type State,
} from 'src/pages/dashboard/serviceManagement/types';

import { getStateName, ServiceManagementTaskStateName } from './state';

export const getSla = (slas: SLA[], state: State) => {
  if (!slas || !state) {
    return null;
  }
  const stateName = getStateName(state.name);
  if (
    stateName === ServiceManagementTaskStateName.UNASSIGNED ||
    stateName === ServiceManagementTaskStateName.ASSIGNED
  ) {
    return slas.find(sla => sla.definitionName === ServiceItemSla.RESPONSE);
  } else {
    return slas.find(sla => sla.definitionName === ServiceItemSla.RESOLUTION);
  }
};

export const getSlaTime = (sla: SLA, state: State) => {
  const stateName = getStateName(state.name);
  const isClosed =
    stateName === ServiceManagementTaskStateName.CANCELLED ||
    stateName === ServiceManagementTaskStateName.CLOSED;

  const remainingMillis = isClosed
    ? sla?.elapsedMillis
    : sla?.slaTotalMillis - sla?.elapsedMillis;

  const remainingDays = Math.floor(remainingMillis / MILISECONDS.DAY);
  const remainingHours = Math.floor(
    (remainingMillis % MILISECONDS.DAY) / MILISECONDS.HOUR,
  );
  const remainingMinutes = Math.floor(
    (remainingMillis % MILISECONDS.HOUR) / MILISECONDS.MINUTE,
  );

  if (remainingDays > 0) {
    return `${remainingDays}d ${remainingHours}h`;
  } else if (remainingHours > 0) {
    return `${remainingHours}h ${remainingMinutes}m`;
  } else {
    return `${remainingMinutes}m`;
  }
};

export const getSlaPercentageColorType = (sla: SLA) => {
  const remainingMillis = sla?.slaTotalMillis - sla?.elapsedMillis;
  const threshold = sla?.slaTotalMillis * 0.25;
  if (remainingMillis > threshold) {
    return 'success';
  }
  return 'warning';
};

export const getSlaPercentageColor = (sla: SLA) => {
  const remainingMillis = sla?.slaTotalMillis - sla?.elapsedMillis;
  const threshold = sla?.slaTotalMillis * 0.25;
  if (remainingMillis > threshold) {
    return '#15B79E';
  } else {
    return '#F79009';
  }
};

export const getSlaColor = (sla: SLA, state: State) => {
  const stateName = getStateName(state.name);
  const isClosed =
    stateName === ServiceManagementTaskStateName.CANCELLED ||
    stateName === ServiceManagementTaskStateName.CLOSED;
  const isRunning =
    stateName === ServiceManagementTaskStateName.UNASSIGNED ||
    stateName === ServiceManagementTaskStateName.ASSIGNED ||
    stateName === ServiceManagementTaskStateName.IN_PROGRESS;

  if (isClosed) {
    return 'rgba(17, 25, 39, 0.38)';
  } else if (sla.expired && isRunning) {
    return 'rgba(240, 68, 56, 1)';
  }
  return getSlaPercentageColor(sla);
};

export const hasExpired = (sla: SLA, state: State) => {
  const stateName = getStateName(state.name);
  return (
    (stateName === ServiceManagementTaskStateName.UNASSIGNED ||
      stateName === ServiceManagementTaskStateName.ASSIGNED ||
      stateName === ServiceManagementTaskStateName.IN_PROGRESS) &&
    sla.expired
  );
};

export const getSlaIcon = (
  sla: SLA,
  state: State,
  withHugo: boolean = false,
) => {
  const stateName = getStateName(state.name);
  const isRunning =
    stateName === ServiceManagementTaskStateName.UNASSIGNED ||
    stateName === ServiceManagementTaskStateName.ASSIGNED ||
    stateName === ServiceManagementTaskStateName.IN_PROGRESS;
  const isClosed =
    stateName === ServiceManagementTaskStateName.CANCELLED ||
    stateName === ServiceManagementTaskStateName.CLOSED;

  const color = getSlaColor(sla, state);

  if ((!sla.expired && isRunning) || isClosed) {
    return withHugo ? (
      <IconClock color={color} />
    ) : (
      <AccessTimeIcon sx={{ color: color }} />
    );
  } else if (sla.expired && isRunning) {
    return withHugo ? (
      <IconExclamationCircle color={color} />
    ) : (
      <ErrorOutlineOutlinedIcon sx={{ color: color }} />
    );
  } else if (stateName === ServiceManagementTaskStateName.ON_HOLD) {
    return withHugo ? (
      <IconPlayerPause color={color} />
    ) : (
      <PauseCircleOutlineOutlinedIcon sx={{ color: color }} />
    );
  }
  return null;
};

// Use only this once migration to Hugo is complete
export const getSlaPillProps = (
  sla: SLA,
  state: State,
): {
  customIcon: React.ElementType;
  type: PillsProps['type'];
} => {
  const stateName = getStateName(state.name);
  const isRunning =
    stateName === ServiceManagementTaskStateName.UNASSIGNED ||
    stateName === ServiceManagementTaskStateName.ASSIGNED ||
    stateName === ServiceManagementTaskStateName.IN_PROGRESS;
  const isClosed =
    stateName === ServiceManagementTaskStateName.CANCELLED ||
    stateName === ServiceManagementTaskStateName.CLOSED;

  let type: 'disabled' | 'error' | 'warning' | 'success' = 'disabled';
  if (isClosed) {
    type = 'disabled';
  } else if (sla.expired && isRunning) {
    type = 'error';
  } else {
    type = getSlaPercentageColorType(sla);
  }

  if ((!sla.expired && isRunning) || isClosed) {
    return {
      customIcon: IconClock,
      type,
    };
  } else if (sla.expired && isRunning) {
    return {
      customIcon: IconExclamationCircle,
      type: 'error',
    };
  } else if (stateName === ServiceManagementTaskStateName.ON_HOLD) {
    return {
      customIcon: IconPlayerPause,
      type,
    };
  }
  return {
    customIcon: IconClock,
    type,
  };
};

export const getSlaTooltipMessage = (sla: SLA, state: State) => {
  const stateName = getStateName(state.name);
  const isClosed =
    stateName === ServiceManagementTaskStateName.CANCELLED ||
    stateName === ServiceManagementTaskStateName.CLOSED;
  if (hasExpired(sla, state)) {
    return 'EXPIRED_SLA_TOOLTIP';
  } else if (isClosed) {
    return 'CLOSED_SLA_TOOLTIP';
  }
  return 'PROGRESS_SLA_TOOLTIP';
};
