#ifndef _DBG_PRINTF_H
#define _DBG_PRINTF_H

//******************************************** Включаемые файлы *********************************************

#include "dbg_printf_cfg.h"

#include "lprintf.h"
#include <stdint.h>
#include <string.h>


//******************************************* Определения типов *********************************************

//************************************ Глобальные (публичные) константы *************************************

#if DBG_ON
    #define PRINTF(...)   lprintf(__VA_ARGS__)

#if DBG_COLOR
    #define DBG_LOGE(fmt, args...)   do {                                       \
                                         TERMINAL_FONT_RED();                   \
                                         PRINTF("[ERR] %s(%d): "fmt,            \
                                             __FUNCTION__, __LINE__, ##args);   \
                                         TERMINAL_FONT_WHITE();                 \
                                     } while(0)
    #define DBG_LOGI(fmt, args...)   do {                                       \
                                         TERMINAL_FONT_CYAN();                  \
                                         PRINTF("[INFO] %s(%d): "fmt,           \
                                             __FUNCTION__, __LINE__, ##args);   \
                                         TERMINAL_FONT_WHITE();                 \
                                     } while(0)
    #define DBG_LOGD(fmt, args...)   do {                                       \
                                         TERMINAL_FONT_YELLOW();                \
                                         PRINTF("[DBG] %s(%d): "fmt,            \
                                             __FUNCTION__, __LINE__, ##args);   \
                                         TERMINAL_FONT_WHITE();                 \
                                     } while(0)
#else
    #define DBG_LOGE(fmt, args...)   do {                                       \
                                         PRINTF("[ERR] %s(%d): "fmt,            \
                                             __FUNCTION__, __LINE__, ##args);   \
                                     } while(0)
    #define DBG_LOGI(fmt, args...)   do {                                       \
                                         PRINTF("[INFO] %s(%d): "fmt,           \
                                             __FUNCTION__, __LINE__, ##args);   \
                                     } while(0)
    #define DBG_LOGD(fmt, args...)   do {                                       \
                                         PRINTF("[DBG] %s(%d): "fmt,            \
                                             __FUNCTION__, __LINE__, ##args);   \
                                     } while(0)
#endif

#else
    #define PRINTF(...)
    #define DBG_LOGE(fmt, args...)
    #define DBG_LOGI(fmt, args...)
    #define DBG_LOGD(fmt, args...)
#endif


// Цвет шрифта
#define TERMINAL_FONT_BLACK()       PRINTF("\033[1;30m")
#define TERMINAL_FONT_L_RED()       PRINTF("\033[0;31m")
#define TERMINAL_FONT_RED()         PRINTF("\033[1;31m")
#define TERMINAL_FONT_GREEN()       PRINTF("\033[1;32m")
#define TERMINAL_FONT_YELLOW()      PRINTF("\033[1;33m")
#define TERMINAL_FONT_BLUE()        PRINTF("\033[1;34m")
#define TERMINAL_FONT_PURPLE()      PRINTF("\033[1;35m")
#define TERMINAL_FONT_CYAN()        PRINTF("\033[1;36m")
#define TERMINAL_FONT_WHITE()       PRINTF("\033[1;37m")

// Цвет фона
#define TERMINAL_BACK_BLACK()       PRINTF("\033[1;40m")
#define TERMINAL_BACK_L_RED()       PRINTF("\033[0;41m")
#define TERMINAL_BACK_RED()         PRINTF("\033[1;41m")    
#define TERMINAL_BACK_GREEN()       PRINTF("\033[1;42m")
#define TERMINAL_BACK_YELLOW()      PRINTF("\033[1;43m")
#define TERMINAL_BACK_BLUE()        PRINTF("\033[1;44m")
#define TERMINAL_BACK_PURPLE()      PRINTF("\033[1;45m")
#define TERMINAL_BACK_CYAN()        PRINTF("\033[1;46m")
#define TERMINAL_BACK_WHITE()       PRINTF("\033[1;47m")

#define NL1()                       do { PRINTF("\r\n"); } while(0)


#define DBG_ERR_STR_SIZE_MAX      (32)

// Текст ошибки fun_res_t
#define DBG_STR_FUN_RES_0         "OK\0"
#define DBG_STR_FUN_RES_1         "ASSERT ERROR\0"
#define DBG_STR_FUN_RES_2         "RUNTIME ERROR\0"
#define DBG_STR_FUN_RES_3         "INVALID ARGUMENT\0"
#define DBG_STR_FUN_RES_4         "NOT IMPLEMENTED\0"
#define DBG_STR_FUN_RES_5         "TIMEOUT EXPIRED\0"
#define DBG_STR_FUN_RES_X         "none\0"

//************************************ Глобальные (публичные) переменные ************************************


//******************************** Объявления глобальных (публичных) функций ********************************

static inline const char* get_err_name(fun_res_t err)
{
    const char* res;
    switch(err)
    {
        case(ERR_OK):
            res = DBG_STR_FUN_RES_0;
        break;
        case(ERR_ASSERT_ERROR):
            res = DBG_STR_FUN_RES_1;
        break;
        case(ERR_RUNTIME_ERROR):
            res = DBG_STR_FUN_RES_2;
        break;
        case(ERR_INVALID_ARGUMENT):
            res = DBG_STR_FUN_RES_3;
        break;
        case(ERR_NOT_IMPLEMENTED):
            res = DBG_STR_FUN_RES_4;
        break;
        case(ERR_TIMEOUT_EXPIRED):
            res = DBG_STR_FUN_RES_5;
        break;

        default:
            res = DBG_STR_FUN_RES_X;
        break;
    }
    return res;
}



#endif

//*********************************************** КОНЕЦ ФАЙЛА ***********************************************

