import { truncateText } from '@star-kitten/util/text.js';

export function cleanText(input: string, maxLength: number = 1000): string {
  return truncateText(replaceBoldTextMarkup(replaceItalicTextMarkup(removeColorTags(removeLinks(input.trim())))), maxLength);
}

function replaceBoldTextMarkup(input: string): string {
  // replace all <b>name</b>, <b>name, and name</b> with **name** using regex
  const regex = /<b>([^<]*)<\/b>|<b>([^<]*)|([^<]*)<\/b>/g;
  return input.replace(regex, (match, p1, p2, p3) => {
    if (p1) return `**${p1}**`;
    if (p2) return `**${p2}**`;
    if (p3) return `**${p3}**`;
    return match;
  });
}

function replaceItalicTextMarkup(input: string): string {
  // replace all <i>name</i>, <i>name, and name</i> with *name* using regex
  const regex = /<i>([^<]*)<\/i>|<i>([^<]*)|([^<]*)<\/i>/g;
  return input.replace(regex, (match, p1, p2, p3) => {
    if (p1) return `*${p1}*`;
    if (p2) return `*${p2}*`;
    if (p3) return `*${p3}*`;
    return match;
  });
}

function removeColorTags(input: string): string {
  const regex = /<color=(?:0x)?([0-9a-fA-F]{6,8}|[a-zA-Z]+)>(.*?)<\/color>/g;
  return input.replace(regex, '$2');
}

function convertToDiscordLinks(input: string): string {
  const regex = /<a href=showinfo:(\d+)>(.*?)<\/a>/g;
  return input.replace(regex, (match, number, text) => {
    const eveRefLink = `https://everef.net/types/${number}`;
    return `[${text}](${eveRefLink})`;
  });
}

function removeLinks(input: string): string {
  const regex = /<a href=showinfo:(\d+)>(.*?)<\/a>/g;
  return input.replace(regex, (match, number, text) => {
    return text;
  });
}

export function convertMillisecondsToTimeString(milliseconds: number): string {
  const totalSeconds = milliseconds / 1000;
  const hours = Math.floor(totalSeconds / 3600);
  const minutes = Math.floor((totalSeconds % 3600) / 60);
  const secs = totalSeconds % 60;

  const parts: string[] = [];

  if (hours > 0) {
    parts.push(`${hours}h`);
  }
  if (minutes > 0) {
    parts.push(`${minutes}m`);
  }
  if (secs > 0 || parts.length === 0) {
    // Include seconds if it's the only part
    parts.push(`${secs.toFixed(1)}s`);
  }

  return parts.join(' ');
}

export function convertSecondsToTimeString(seconds: number): string {
  const hours = Math.floor(seconds / 3600);
  const minutes = Math.floor((seconds % 3600) / 60);
  const secs = seconds % 60;

  const parts: string[] = [];

  if (hours > 0) {
    parts.push(`${hours}h`);
  }
  if (minutes > 0) {
    parts.push(`${minutes}m`);
  }
  if (secs > 0 || parts.length === 0) {
    // Include seconds if it's the only part
    parts.push(`${secs}s`);
  }

  return parts.join(' ');
}

export function coloredTextCodeBlock(text: string, color: 'green' | 'blue' | 'red' | 'yellow'): string {
  switch (color) {
    case 'red':
      return '```ansi\n[2;31m' + text + '[0m```\n';
    case 'blue':
      return '```ansi\n[2;32m[2;36m[2;34m' + text + '[0m[2;36m[0m[2;32m[0m```\n';
    case 'yellow':
      return '```ansi\n[2;33m' + text + '[0m```\n';
    case 'green':
      return '```ansi\n[2;36m' + text + '[0m```\n';
  }
}
