/**
 ******************************************************************************
 *
 * @file       debuggadgetwidget.cpp
 * @author     The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
 * @addtogroup GCSPlugins GCS Plugins
 * @{
 * @addtogroup DebugGadgetPlugin Debug Gadget Plugin
 * @{
 * @brief A place holder gadget plugin
 *****************************************************************************/
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
#include "debuggadgetwidget.h"

#include <QDebug>
#include <QStringList>
#include <QWidget>
#include <QTextEdit>
#include <QVBoxLayout>
#include <QPushButton>
#include "debugengine.h"
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include <QScrollBar>
#include <QTime>

QPointer<QTextBrowser> m_textedit;

void DebugGadgetWidget::customMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    Q_UNUSED(context);
    QString txt;
    QColor color = Qt::black;

    switch (type) {
    case QtDebugMsg:
        txt   = QString("Debug: %1").arg(msg);
        color = Qt::black;
        break;
#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0))
    case QtInfoMsg:
        txt   = QString("Info: %1").arg(msg);
        color = Qt::blue;
        break;
#endif
    case QtWarningMsg:
        txt   = QString("Warning: %1").arg(msg);
        color = Qt::red;
        break;
    case QtCriticalMsg:
        txt   = QString("Critical: %1").arg(msg);
        color = Qt::red;
        break;
    case QtFatalMsg:
        txt   = QString("Fatal: %1").arg(msg);
        abort();
    }

    debugengine::getInstance()->setTextEdit(m_textedit);
    debugengine::getInstance()->setColor(color);
    debugengine::getInstance()->writeMessage(txt);
}

DebugGadgetWidget::DebugGadgetWidget(QWidget *parent) : QLabel(parent)
{
    m_config = new Ui_Form();
    m_config->setupUi(this);

    // m_textedit = m_config->plainTextEdit;
    // MyplainTextEdit=m_config->plainTextEdit;
    // debugengine *de = new debugengine();
    // qInstallMessageHandler(customMessageHandler);
    // connect(de, SIGNAL(dbgMsg(QString, QList<QVariant>)), this, SLOT(dbgMsg(QString, QList<QVariant>)));
    // connect(de, SIGNAL(dbgMsgError(QString, QList<QVariant>)), this, SLOT(dbgMsgError(QString, QList<QVariant>)));
    connect(m_config->pushButton, SIGNAL(clicked()), this, SLOT(saveLog()));
}

DebugGadgetWidget::~DebugGadgetWidget()
{
    // Do nothing
}

void DebugGadgetWidget::dbgMsg(const QString &level, const QList<QVariant> &msgs)
{
    m_config->plainTextEdit->setTextColor(Qt::red);

    m_config->plainTextEdit->append(QString("%2[%0]%1").arg(level).arg(msgs[0].toString()).arg(QTime::currentTime().toString()));

    QScrollBar *sb = m_config->plainTextEdit->verticalScrollBar();
    sb->setValue(sb->maximum());
}

void DebugGadgetWidget::dbgMsgError(const QString &level, const QList<QVariant> &msgs)
{
    m_config->plainTextEdit->setTextColor(Qt::black);


    m_config->plainTextEdit->append(QString("%2[%0]%1").arg(level).arg(msgs[0].toString()).arg(QTime::currentTime().toString()));

    QScrollBar *sb = m_config->plainTextEdit->verticalScrollBar();
    sb->setValue(sb->maximum());
}
void DebugGadgetWidget::saveLog()
{
    QString fileName = QFileDialog::getSaveFileName(0, tr("Save log File As"), "");

    if (fileName.isEmpty()) {
        return;
    }

    QFile file(fileName);
    if (file.open(QIODevice::WriteOnly) &&
        (file.write(m_config->plainTextEdit->toHtml().toLatin1()) != -1)) {
        file.close();
    } else {
        QMessageBox::critical(0,
                              tr("Log Save"),
                              tr("Unable to save log: ") + fileName,
                              QMessageBox::Ok);
        return;
    }
}
