#include "qgloggerdockwidget.h"
#include "ui_qgloggerdockwidget.h"

#include <QTime>
#include <QTextFormat>
#include <QTextCharFormat>
#include <QFont>

#include <QThread>

class QgLoggerDockWidget::Implementation
{
public:
    QTextCharFormat timeFmt;
    QTextCharFormat msgFmt;
    QTextCharFormat srcFmt;

};

QgLoggerDockWidget::QgLoggerDockWidget(QWidget *parent) :
    QDockWidget(parent),
    ui(new Ui::QgLoggerDockWidget),
    p_(*new Implementation)
{
    ui->setupUi(this);

    ui->textEdit->setFont(QFont("courier new", 10));
    ui->textEdit->setLineWrapMode(QTextEdit::NoWrap);
    ui->textEdit->document()->setMaximumBlockCount(1000);
    //ui->textEdit->setCenterOnScroll(true);
    ui->textEdit->setUndoRedoEnabled(false);
    ui->textEdit->ensureCursorVisible();

    p_.timeFmt.setForeground(Qt::cyan);
    p_.msgFmt.setForeground(Qt::black);
    p_.srcFmt.setForeground(Qt::red);
    //loggerTime.start();
    //qRegisterMetaType(QgLogger::LogMessageType);

    connect(this, &QgLoggerDockWidget::log_asynch_, this, &QgLoggerDockWidget::onLogAsynch, Qt::QueuedConnection);
}

QgLoggerDockWidget::~QgLoggerDockWidget()
{
    delete &p_;
    delete ui;
}

void QgLoggerDockWidget::log(const QString& str, QgLogger::LogMessageType type, const QString &originator)
{    
    if (QThread::currentThread()!=this->thread())
      Q_EMIT log_asynch_(str, type, originator);
    else
        log_(str, type, originator);
}

static const QString& logType2Str(QgLogger::LogMessageType type)
{
    static const QString invLevel=QString("?");
    static const QString cvstr[]=
    {
        "D", //DebugMsg=0,
        "I", //InfoMsg,
        "S", //SuccessMsg,
        "W", //WarningMsg,
        "E", //ErrorMsg,
        "F", //FatalMsg,
        "E", //SystemErrorMsg,
        "F" //SystemFatalMsg
    };
    if ((type>=QgLogger::DebugMsg) && (type<=QgLogger::SystemFatalMsg))
        return cvstr[type];
    return invLevel;
}

void QgLoggerDockWidget::log_(const QString& str, QgLogger::LogMessageType type, const QString &originator)
{
    QString tmp=QTime::currentTime().toString("H:mm:ss.zzz: "); //+QString(": %1").arg(str);

    //ui->textEdit->setTextColor(Qt::cyan);
    ui->textEdit->moveCursor (QTextCursor::End);
    ui->textEdit->insertHtml(logType2Str(type)+": ");
    ui->textEdit->textCursor().setCharFormat(p_.timeFmt);
    ui->textEdit->insertHtml("<font color=\"cyan\">");
    ui->textEdit->insertHtml(tmp);
    ui->textEdit->insertHtml("</font>");
    ui->textEdit->insertHtml("<font color=\"green\">");
    ui->textEdit->insertHtml("["+originator+"]</font>");
    ui->textEdit->insertHtml("<font color=\"black\">");
    ui->textEdit->insertPlainText(str);
    ui->textEdit->insertHtml("</font>");
    ui->textEdit->insertPlainText("\n");
    ui->textEdit->moveCursor (QTextCursor::End);
}

void QgLoggerDockWidget::onLogAsynch(const QString& msg, int rawType/*QgLogger::LogMessageType type*/, const QString& originator)
{
  QgLogger::LogMessageType type=(QgLogger::LogMessageType)rawType;
  log_(msg, type, originator);
}

void QgLoggerDockWidget::log_asynch(const QString& msg, QgLogger::LogMessageType type, const QString& originator)
{
  Q_EMIT log_asynch_(msg, (int)type, originator);
}
