#include "PICUploaderGui.h"
#include "ui_PICUploaderGui.h"
#include <QFileDialog>
#include "PICUploader.h"
#include "ProtocolDefs.h"

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

    connect(ui->mHexFileSelectBtn,SIGNAL(clicked(bool)),this,SLOT(OpenHexFileBtnClicked(bool)));
    connect(ui->mConnectBtn,SIGNAL(clicked(bool)),this,SLOT(ConnectBtnClicked(bool)));
    connect(ui->mSendCmdBtn,SIGNAL(clicked(bool)),this,SLOT(SendSingleCmdCliked(bool)));
    connect(ui->mClearLogginWndwBtn,SIGNAL(clicked(bool)),SLOT(ClearLogScreen(bool)));
    connect(ui->mShowHexFileInfoBtn,SIGNAL(clicked(bool)),this,SLOT(ShowHexFileInfoClicked(bool)));
    ui->mUploadProgressBar->reset();

}

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

void CPICUploaderGui::OpenHexFileBtnClicked(bool checked)
{
    QString FileName = QFileDialog::getOpenFileName(0,"Select Firmware Hex File",QString(),"Hex files (*.hex)");
    if(mProgramHandle->OpenHexFileRequest(FileName) != 1)
    {
        ui->mOpenedHexFilePathLbl->setText(FileName);
    }

}

void CPICUploaderGui::SetHexFileStats(QString Stats)
{
    ui->mHexFileStatsLbl->setText(Stats);
}

void CPICUploaderGui::ConnectBtnClicked(bool)
{
    if(ui->mConnectBtn->text() == "Connect")
    {
        QString IP = ui->mIPAddressEdit->text();
        if(IP.isEmpty())
        {
            return;
        }

//        ui->mConnectBtn->setText("Disconnect");
       // ui->mConnectBtn->setEnabled(false);
        mProgramHandle->ConnectToBootloader(IP);
    }
    else
    {
       // ui->mConnectBtn->setText("Connect");
        mProgramHandle->DisconnectFromBootloader();
    }
}

void CPICUploaderGui::UploaderSocketConnected()
{
    ui->mConnectBtn->setEnabled(true);
    ui->mConnectBtn->setText("Disconnect");
}

void CPICUploaderGui::UploaderSocketDisconnected()
{
    ui->mConnectBtn->setEnabled(true);
    ui->mConnectBtn->setText("Connect");
}

void CPICUploaderGui::SendSingleCmdCliked(bool)
{
    QString CmdSelection = ui->mCmdSelectCombo->currentText();

    if(CmdSelection == "Heartbeat")
    {
//        mProgramHandle->SendSingleCmdRequest(BOOTLOADER_HEARTBEAT_REQUEST);
        mProgramHandle->GuiEvent(UPLOADER_GUI_HEARTBEAT_BTN_CLICK_EVENT);
    }
    else if(CmdSelection == "Flash Erase")
    {
        //mProgramHandle->SendSingleCmdRequest(BOOTLOADER_ERASE_BOOTLOADER_FLASH_REQUEST);
        mProgramHandle->GuiEvent(UPLOADER_GUI_FLASH_ERASE_BTN_CLICK_EVENT);
    }
    else if(CmdSelection == "Init Upload")
    {
//        mProgramHandle->SendSingleCmdRequest(BOOTLOADER_INIT_UPLOAD_REQUEST);
        mProgramHandle->GuiEvent(UPLOADER_GUI_INIT_UPLOAD_BTN_CLICK_EVENT);
    }
    else if(CmdSelection == "Get State")
    {
//        mProgramHandle->SendSingleCmdRequest(BOOTLOADER_GET_STATE_REQUEST);
        mProgramHandle->GuiEvent(UPLOADER_GUI_GET_STATE_BTN_CLICK_EVENT);
    }
    else if(CmdSelection == "Send Data Chunk")
    {
//        mProgramHandle->SendSingleCmdRequest(BOOTLOADER_SEND_DATA_CHUNK_REQUEST);
        mProgramHandle->GuiEvent(UPLOADER_GUI_SEND_DATA_CHUNK_BTN_CLICK_EVENT);
    }
    else if(CmdSelection == "Upload Finished")
    {
//        mProgramHandle->SendSingleCmdRequest(BOOTLOADER_UPLOAD_FINISHED_REQUEST);
        mProgramHandle->GuiEvent(UPLOADER_GUI_UPLOAD_FINISHED_BTN_CLICK_EVENT);
    }
    else if(CmdSelection == "Execute Upgrade")
    {
//        mProgramHandle->SendSingleCmdRequest(BOOTLOADER_EXECUTE_UPGRAGE_REQUEST);
        mProgramHandle->GuiEvent(UPLOADER_GUI_EXECUTE_UPGRADE_BTN_CLICK_EVENT);
    }
    else if(CmdSelection == "Abort")
    {
//        mProgramHandle->SendSingleCmdRequest(BOOTLOADER_ABORT_OPERATION_REQUEST);
        mProgramHandle->GuiEvent(UPLOADER_GUI_ABORT_BTN_CLICK_EVENT);
    }
    else if(CmdSelection == "Check Flash")
    {
        mProgramHandle->GuiEvent(UPLOADER_GUI_CHECK_FLASH_CLICK_EVENT);
    }
    else if(CmdSelection == "Get Stored Firmware Info")
    {
        mProgramHandle->GuiEvent(UPLOADER_GUI_GET_STORED_FIRMWARE_INFO_CLICK_EVENT);
    }
    else
    {
        qDebug("Unknown selection???!!!");
    }

}

void CPICUploaderGui::AddTextToLogScreen(QString Text)
{
    Text.append("\n");
    mLogScreenText.prepend(Text);
    ui->mLoggingWindowTextEdit->setPlainText(mLogScreenText);
}

void CPICUploaderGui::ClearLogScreen(bool dummy)
{
    mLogScreenText.clear();
    ui->mLoggingWindowTextEdit->setPlainText(mLogScreenText);
}

void CPICUploaderGui::ShowHexFileInfoClicked(bool)
{
    mProgramHandle->ShowHexFileInfoRequest();
}

void CPICUploaderGui::SetUploadProgressSettings(int max)
{
    ui->mUploadProgressBar->setRange(0,max);
}

void CPICUploaderGui::TickProgressBar()
{
    ui->mUploadProgressBar->setValue(ui->mUploadProgressBar->value()+1);
}

void CPICUploaderGui::ResetProgressBar()
{
    ui->mUploadProgressBar->reset();
}
