#include "colors.h"
#include <string.h>
#include <stdio.h>

static const char *ansi_colors[] = {
    "\033[30m",  // BLACK
    "\033[31m",  // RED
    "\033[32m",  // GREEN
    "\033[33m",  // YELLOW
    "\033[34m",  // BLUE
    "\033[35m",  // MAGENTA
    "\033[36m",  // CYAN
    "\033[37m",  // WHITE
};

static const char *ansi_colors_dark[] = {
    "\033[90m",  // BLACK (bright)
    "\033[91m",  // RED (bright)
    "\033[92m",  // GREEN (bright)
    "\033[93m",  // YELLOW (bright)
    "\033[94m",  // BLUE (bright)
    "\033[95m",  // MAGENTA (bright)
    "\033[96m",  // CYAN (bright)
    "\033[97m",  // WHITE (bright)
};

static const char *ansi_bg_colors[] = {
    "\033[40m",  // BLACK
    "\033[41m",  // RED
    "\033[42m",  // GREEN
    "\033[43m",  // YELLOW
    "\033[44m",  // BLUE
    "\033[45m",  // MAGENTA
    "\033[46m",  // CYAN
    "\033[47m",  // WHITE
};

const char *color_get_ansi(int fg, int bg, int dark_mode) {
    static char buffer[32];
    
    const char *fg_code = dark_mode && fg < 8 ? ansi_colors_dark[fg] : ansi_colors[fg % 8];
    const char *bg_code = bg < 8 ? ansi_bg_colors[bg] : "";
    
    snprintf(buffer, sizeof(buffer), "%s%s", bg_code, fg_code);
    return buffer;
}

const char *color_irc_to_ansi(int irc_color) {
    switch (irc_color) {
        case IRC_WHITE: return "\033[37m";
        case IRC_BLACK: return "\033[30m";
        case IRC_BLUE: return "\033[34m";
        case IRC_GREEN: return "\033[32m";
        case IRC_RED: return "\033[31m";
        case IRC_BROWN: return "\033[33m";
        case IRC_MAGENTA: return "\033[35m";
        case IRC_ORANGE: return "\033[38;5;208m";
        case IRC_YELLOW: return "\033[33m";
        case IRC_LIGHT_GREEN: return "\033[92m";
        case IRC_CYAN: return "\033[36m";
        case IRC_LIGHT_CYAN: return "\033[96m";
        case IRC_LIGHT_BLUE: return "\033[94m";
        case IRC_LIGHT_MAGENTA: return "\033[95m";
        case IRC_GRAY: return "\033[90m";
        case IRC_LIGHT_GRAY: return "\033[37m";
        default: return "\033[0m";
    }
}