#include "StatusWindow.h"
#include "ui_StatusWindow.h"
#include <QScrollBar>
#include <QDateTime>
#include "defines.h"
#include "LogsSorter.h"

CStatusWindow::CStatusWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CStatusWindow)
{
    ui->setupUi(this);
    setAutoFillBackground(true);
    mProgramPtr = 0;

    connect(ui->mForceScanBtn,&QPushButton::pressed,this,&CStatusWindow::ManualScanBtnPressed);

    ui->mSoftVersionLbl->setText(QString("Version: %1").arg(SOFTWARE_VERSION));

    ui->mMsgTxtEditBox->setReadOnly(true);
}

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

int CStatusWindow::AddGeneralMsgBoxLineEntry(QString NewLine, QColor Color)
{
    NewLine.remove('\n');
    NewLine.remove('\r');
    NewLine.append('\n');

    NewLine.prepend(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss - "));

    AddColoredLineToGenMsgBox(NewLine,Color);
    //Limit the number of lines in the MsgBox to avoid overruns...

//    if(mGenMsgListBoxTextLines.size() >= GENERAL_MESSAGES_MAX_LOG_LINES)
//    {
//        mGenMsgListBoxTextLines.removeFirst();
//        mGenMsgListBoxTextLines.append(NewLine);

//        ui->mMsgTxtEditBox->clear();
//        for(int i = 0; i < mGenMsgListBoxTextLines.size(); i++)
//        {
//            AddColoredLineToGenMsgBox(mGenMsgListBoxTextLines[i],Color);
//        }
//    }
//    else
//    {
//        mGenMsgListBoxTextLines.append(NewLine);
//        AddColoredLineToGenMsgBox(NewLine,Color);
//    }

    ui->mMsgTxtEditBox->verticalScrollBar()->setValue(ui->mMsgTxtEditBox->verticalScrollBar()->maximum());


    return RET_OK;
}

int CStatusWindow::AddColoredLineToGenMsgBox(QString Line, QColor Color)
{
    if(Line.isEmpty())
    {
        return RET_GENERAL_ERROR;
    }

//    if(Line.at(0) == QChar('%'))
//    {
//        if(Line.at(1) == QChar('E'))    //The line is describing an error... write it red
//        {
//            ui->mMsgTxtEditBox->setTextColor(Qt::red);
//        }
//        else if(Line.at(1) == QChar('W'))   //The line is describing a warning... write it yellow
//        {
//            ui->mMsgTxtEditBox->setTextColor(Qt::blue);
//        }
//        else if(Line.at(1) == QChar('S'))   //The line is describing a warning... write it yellow
//        {
//            ui->mMsgTxtEditBox->setTextColor(Qt::darkGreen);
//        }
//        else
//        {
//            qDebug("GeneralStatusPage: Logic error in general message box line encoding...  you should check into that");
//            return RET_GENERAL_ERROR;
//        }

//        Line.remove(0,2);
//    }

    ui->mMsgTxtEditBox->setTextColor(Color);
    ui->mMsgTxtEditBox->insertPlainText(Line);
    ui->mMsgTxtEditBox->setTextColor(Qt::black);

    return RET_OK;
}

void CStatusWindow::ManualScanBtnPressed()
{
    AddGeneralMsgBoxLineEntry("Lancement d'un scan manuel.",Qt::darkBlue);
    mProgramPtr->LauchManualScanRequest();
    AddGeneralMsgBoxLineEntry("Scan manuel terminé.",Qt::darkBlue);
}
