#include "debugprintpage.h"
#include "ui_debugprintpage.h"
#include <QTimer>
#include <QTextCursor>
#include <QDateTime>

DebugPrintPage *DebugPrintPage::currentMsgHandler = 0;

DebugPrintPage::DebugPrintPage(QWidget *parent) :
    QFrame(parent),
    ui(new Ui::DebugPrintPage)
{
    ui->setupUi(this);

    currentMsgHandler = this;

    connect(ui->clearButton, &QPushButton::clicked, this, &DebugPrintPage::on_clearButton_clicked);
}

DebugPrintPage::~DebugPrintPage()
{
    delete ui;
}

void DebugPrintPage::on_clearButton_clicked()
{
    clearConsole();
}

void DebugPrintPage::clearConsole()
{
    ui->consoleBrowser->clear();
}

BMSInterface *DebugPrintPage::bms() const
{
    return mDieBieMS;
}

void DebugPrintPage::setDieBieMS(BMSInterface *dieBieMS)
{
    mDieBieMS = dieBieMS;

    if (mDieBieMS) {
        connect(mDieBieMS->commands(), SIGNAL(printReceived(QString)),
                this, SLOT(printReceived(QString)));
    }
}


void DebugPrintPage::printConsole(QString str)
{
    ui->consoleBrowser->moveCursor(QTextCursor::End);
    ui->consoleBrowser->insertHtml(QDateTime::currentDateTime().
                                   toString("yyyy-MM-dd hh:mm:ss: ")
                                   + str);
    ui->consoleBrowser->moveCursor(QTextCursor::End);
}
