/**
 * @copyright nhcarrigan
 * @license Naomi's Public License
 * @author Naomi Carrigan
 */
import { EmbedBuilder, type APIEmbedField, type MessageCreateOptions }
  from "discord.js";
import { generateBanner } from "./images/generateBanner.js";
import { getSeasonIcon } from "./images/getSeasonIcon.js";
import type { WeatherForecast } from "../interfaces/weather/weatherForecast.js";

/**
 * Parses a weather forecast into a Discord message embed.
 * @param forecast - The forecast to parse.
 * @returns A Discord message embed.
 */
export const generateWeatherEmbed = async(
  forecast: WeatherForecast | null,
): Promise<MessageCreateOptions> => {
  if (!forecast) {
    const embed = new EmbedBuilder().
      setTitle("Error").
      setDescription("No forecast was generated.");
    return { embeds: [ embed ] };
  }

  const weatherEmbed = new EmbedBuilder();
  weatherEmbed.setTitle(`${forecast.region}'s Daily Weather Forecast`);

  let emoteString = forecast.temperature.emote + forecast.wind.emote;

  const fields: Array<APIEmbedField> = [
    {
      name:  "Temperature",
      value: `${String(forecast.temperature.fahrenheit)}°F / ${String(forecast.temperature.celsius)}°C [${forecast.temperature.name}]`,
    },
    {
      name:  "Wind",
      value: `${forecast.wind.name} [${String(forecast.wind.lowSpeed)} - ${String(forecast.wind.highSpeed)}kph]`,
    },
  ];

  if (forecast.precipitation) {
    fields.push({
      name:  "Precipitation",
      value: forecast.precipitation.name,
    });
    emoteString = emoteString + forecast.precipitation.emote;
  }
  if (forecast.special) {
    fields.push({
      name:  "Special Conditions",
      value: forecast.special.name,
    });
    emoteString = emoteString + forecast.special.emote;
  }
  weatherEmbed.setDescription(emoteString);
  weatherEmbed.addFields(fields);

  const seasonIcon = getSeasonIcon(forecast.season);
  weatherEmbed.setThumbnail(seasonIcon.attachmentString);
  const banner = await generateBanner(forecast);
  weatherEmbed.setImage(banner.attachmentString);

  switch (forecast.region) {
    case "Rudania":
      weatherEmbed.setColor(0xFF_00_00);
      break;
    case "Inariko":
      weatherEmbed.setColor(0x00_00_FF);
      break;
    case "Vhintl":
      weatherEmbed.setColor(0x00_FF_00);
      break;
    default:
      weatherEmbed.setColor(0x00_00_00);
  }

  return {
    embeds: [ weatherEmbed ],
    files:  [ seasonIcon.filePath, banner.filePath ],
  };
};
