#ifndef MYDEBUG_H
#define MYDEBUG_H

#include "QDebug"
#include <QDateTime>

//level for log into debug
enum log_level_t
{
    log_level_min   = 0,        //minimun level
    log_level_error = 1,        //1 only error
    log_level_max   = 2,         //2 maximun level
    log_level_event = 3
};

//log level set into debug.log file or debug info into qt creator
#define LOG_LEVEL   log_level_max
                                //0 minimun level   log_level_min
                                //1 only error      log_level_error
                                //2 maximun level   log_level_max

//type message into log
enum type_log_t
{
    type_log_error = -1,            //error
    type_log_info = 0,              //info
    type_log_success = 1,           //success
    type_log_debug = 2,             //debug
    type_log_event =3               //event
};

//origin of message
enum origin_msg_t
{
    origin_msg_generic  = 0,        //generic
    origin_msg_tftp     = 1,        //tftp
    origin_msg_engine   = 2,        //info
    origin_msg_srio     = 3,        //srio
    origin_msg_event    = 4,         //event
    origin_msg_history  = 5         //History
};

#define str_origin_msg_generic      "GENERIC"
#define str_origin_msg_tftp         "TFTP   "
#define str_origin_msg_engine       "ENGINE "
#define str_origin_msg_srio         "SRIO   "
#define str_origin_msg_event        "EVENT  "
#define str_origin_msg_history      "HISTORY"

//**************************************************************
// Debug message handling class
// Input parameters:
// Returns:
// *************************************************************
class MyDbg
{

public:

    struct lastErrorStruct
    {
         QString description;
         unsigned int code;
         unsigned int origin;
    };

    //**************************************************************
    // default constructor with default level
    // Input parameters:
    // Returns:
    // *************************************************************
   MyDbg()
   {
        setLevel(LOG_LEVEL);
   }

   //**************************************************************
   // operator << overloading
   // Input parameters:         t = input string
   // Returns:                  << overloading
   // *************************************************************
   template<typename T_> MyDbg& operator<<(T_ t)
   {
        qDebug()<<t;
        return *this;
   }

   //**************************************************************
   // Set level for message into debug
   // Input parameters:         _newLevel = new log level for debug
   // Returns:
   // *************************************************************
   void setLevel(log_level_t _newLevel)
   {
        log_level = _newLevel;
   }

   //**************************************************************
   // Get level for message into debug
   // Input parameters:
   // Returns:                  actual log level
   // *************************************************************
   log_level_t getLevel()
   {
        return log_level;
   }

   //**************************************************************
   // Returns the string that identifies the origin of the message
   // Input parameters:         _origin = enumerative that identifies the origin of the message
   // Returns:                  String about origin
   // *************************************************************
   QString getOrigin(origin_msg_t _origin)
   {
       switch (_origin)
       {
       case origin_msg_generic:
           return str_origin_msg_generic;
           break;
       case origin_msg_tftp:
           return str_origin_msg_tftp;
           break;
       case origin_msg_engine:
           return str_origin_msg_engine;
           break;
       case origin_msg_srio:
           return str_origin_msg_srio;
           break;
       case origin_msg_event:
           return str_origin_msg_event;
           break;
       case origin_msg_history:
           return str_origin_msg_history;
           break;
       default:
           return str_origin_msg_generic;
           break;
       }
       return "";
   }

   //**************************************************************
   // Send the log message to debug, with the appropriate source and type information
   // Input parameters:         _origin = enumerative that identifies the origin of the message
   //                           _level = type of message
   //                           msg = message
   // Returns:                  String about origin
   // *************************************************************
   void logMsg(type_log_t _level, origin_msg_t _origin , QString msg)
   {
        log_level_t actual_level = getLevel();
        QString origin = getOrigin(_origin);
        //QString tick = QDateTime::currentDateTime().toString("yyyy/MM/dd-hh:mm:ss.zzz");
        QString tick = QDateTime::currentDateTime().toString("hh:mm:ss.zzz");
        switch (_level)
        {
            case type_log_error:    //if error, print always
                qDebug()<<tick.toStdString().c_str()<<"ER-"<<origin.toStdString().c_str()<<":"<<msg.toStdString().c_str();
                //lastError = QString("%1 %2").arg(origin).arg(msg);
                setLastError(msg, 0, _origin);
                break;
            case type_log_info:     //if info, print only if log level is max
                if (actual_level==log_level_max)
                    qDebug()<<tick.toStdString().c_str()<<"IN-"<<origin.toStdString().c_str()<<":"<<msg.toStdString().c_str();
                break;
            case type_log_success:
                if (actual_level==log_level_max || actual_level==log_level_min)
                    qDebug()<<tick.toStdString().c_str()<<"OK-"<<origin.toStdString().c_str()<<":"<<msg.toStdString().c_str();
                break;
            case type_log_debug:
                qDebug()<<tick.toStdString().c_str()<<"DE-"<<origin.toStdString().c_str()<<":"<<msg.toStdString().c_str();
                break;
            case type_log_event:
                if (actual_level==log_level_event || actual_level==log_level_max)
                   qDebug()<<tick.toStdString().c_str()<<"EV-"<<origin.toStdString().c_str()<<":"<<msg.toStdString().c_str();
            default:
                qDebug()<<tick.toStdString().c_str()<<"---"<<origin.toStdString().c_str()<<":"<<msg.toStdString().c_str();
                break;
        }
   }

   //**************************************************************
   // return last error
   // Input parameters:
   // Returns:                  string about last error
   // *************************************************************
   lastErrorStruct getLastError()
   {
       return lastError;
   }

   QString getLastErrorStr()
   {
       return lastError.description;
   }

   void setLastError(QString _des, unsigned int _code, unsigned int _origin)
   {
        lastError.code = _code;
        lastError.origin = _origin;
        lastError.description = _des;
   }

private:
   log_level_t log_level;



   lastErrorStruct lastError;
};


//#define MyDebug MyDbg
static MyDbg MyDebug;

#endif // MYDEBUG_H
