import { ISOToDatetime } from "../../../../../../../../Bundle/ChillMainBundle/Resources/public/chill/js/date";
import {
  trans,
  CHILL_TICKET_TICKET_BANNER_DAYS,
  CHILL_TICKET_TICKET_BANNER_HOURS,
  CHILL_TICKET_TICKET_BANNER_MINUTES,
  CHILL_TICKET_TICKET_BANNER_SECONDS,
  CHILL_TICKET_TICKET_BANNER_AND,
  CHILL_TICKET_TICKET_BANNER_NO_MOTIVE,
} from "translator";
import { MotiveWithParent, Ticket, TicketSimple } from "../../../types";
import { localizeString } from "ChillMainAssets/lib/localizationHelper/localizationHelper";

/**
 * Calcule et formate le temps écoulé depuis une date de création
 * @param createdAt La date de création au format ISO
 * @param currentTime La date actuelle
 * @returns Une chaîne formatée représentant le temps écoulé
 */
export function getSinceCreated(createdAt: string, currentTime: Date): string {
  if (!createdAt) {
    return "";
  }
  const date = ISOToDatetime(createdAt);
  if (!date) {
    return "";
  }

  const timeDiff = Math.abs(currentTime.getTime() - date.getTime());
  const daysDiff = Math.floor(timeDiff / (1000 * 3600 * 24));
  const hoursDiff = Math.floor((timeDiff % (1000 * 3600 * 24)) / (1000 * 3600));
  const minutesDiff = Math.floor((timeDiff % (1000 * 3600)) / (1000 * 60));
  const secondsDiff = Math.floor((timeDiff % (1000 * 60)) / 1000);

  const parts: string[] = [];
  if (daysDiff > 0) {
    parts.push(trans(CHILL_TICKET_TICKET_BANNER_DAYS, { count: daysDiff }));
  }
  if (hoursDiff > 0 || daysDiff > 0) {
    parts.push(
      trans(CHILL_TICKET_TICKET_BANNER_HOURS, {
        count: hoursDiff,
      }),
    );
  }
  if (minutesDiff > 0 || hoursDiff > 0 || daysDiff > 0) {
    parts.push(
      trans(CHILL_TICKET_TICKET_BANNER_MINUTES, {
        count: minutesDiff,
      }),
    );
  }
  if (parts.length === 0) {
    return trans(CHILL_TICKET_TICKET_BANNER_SECONDS, {
      count: secondsDiff,
    });
  }
  if (parts.length > 1) {
    const last = parts.pop();
    return (
      parts.join(", ") +
      " " +
      trans(CHILL_TICKET_TICKET_BANNER_AND) +
      " " +
      last
    );
  }
  return parts[0];
}

export function formatDateTime(
  dateTime: string,
  dateStyle: string,
  timeStyle: string,
): string {
  return new Date(dateTime).toLocaleString("fr-FR", {
    dateStyle: dateStyle as "short" | "medium" | "long" | "full",
    timeStyle: timeStyle as "short" | "medium" | "long" | "full",
  });
}
export function getTicketTitle(ticket: Ticket | TicketSimple): string {
  if (ticket.currentMotive) {
    return `#${ticket.id} ${localizeString(ticket.currentMotive.label)}`;
  }
  return `#${ticket.id} ${trans(CHILL_TICKET_TICKET_BANNER_NO_MOTIVE)}`;
}

export function motiveHierarchyLabel(motive: MotiveWithParent | null): string {
  if (null === motive) {
    return "Aucun motif";
  }
  const str = [];
  let m: MotiveWithParent | null = motive;
  do {
    str.push(localizeString(m.label));
    m = m.parent;
  } while (m !== null);
  str.reverse();
  if (str.length > 1) {
    str.pop();
  }
  return str.join(" > ");
}
