//
// Created by Fernando Cores Prado on 4/12/23.
//

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "Errors.h"

// Constants
char *COLOR_BLACK   = "\033[0;30m";
char *COLOR_RED     = "\033[0;31m";
char *COLOR_RED_B   = "\033[1;31m";
char *COLOR_GREEN   = "\033[0;32m";
char *COLOR_GREEN_B = "\033[1;32m";
char *COLOR_GREEN_D = "\033[0;42m";
char *COLOR_GREEN_L = "\033[0;92m";
char *COLOR_YELLOW  = "\033[0;33m";
char *COLOR_YELLOW_B= "\033[1;33m";
char *COLOR_BLUE    = "\033[0;34m";
char *COLOR_BLUE_B  = "\033[1;34m";
char *COLOR_MAGENTA = "\033[0;35m";
char *COLOR_CYAN    = "\033[0;36m";
char *COLOR_CYAN_B  = "\033[1;36m";
char *COLOR_RESET   = "\033[0m";


// Global variables
char MsgError[256], MsgDebug[256], Msg[256];

/*
* Show an Error message in red and finish the application
*/
void Error(char *msg)
{
    char infoColorError[300];

    sprintf(infoColorError, "%s%s: %s%s\n", COLOR_RED_B, msg, strerror(errno), COLOR_RESET);
    int cerr = write(STDERR_FILENO, infoColorError, strlen(infoColorError));
    exit(errno);
}

/*
* Show a message with the desired color
*/
void printMessage(char *msg, char *color)
{
    char infoColorMsg[300];

    sprintf(infoColorMsg, "%s%s%s", color, msg, COLOR_RESET);
    int cerr = write(STDOUT_FILENO, infoColorMsg, strlen(infoColorMsg));
    fsync(STDOUT_FILENO);
}


/*
* Show a normal message
*/
void print(char *msg)
{
    printMessage(msg, COLOR_GREEN);
    fsync(STDOUT_FILENO);
}

/*
* Show a debug message
*/
void printDebug(char *msg)
{
    printMessage(msg, COLOR_YELLOW_B);
    fsync(STDOUT_FILENO);
}