#include "commonbuttongroupwidget.h"
#include "ui_commonbuttongroupwidget.h"

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

void CommonButtonGroupWidget::initialize(ExternalExecuter *extExec,SendSystem *sendSystem,TCPClient *client)
{
    externalExecuter = extExec;
    this->sendSystem = sendSystem;
    ui->loadingProgressBar->setValue(0);
    ui->loadingProgressBar->hide();
    ui->updateButton->hide();
    ui->startButton->hide();
    ui->startButton->setEnabled(false);
    ui->startButton->setEnabled(false);

    connect(this,&CommonButtonGroupWidget::sigSendCommand,client,&TCPClient::slotSendCommand,Qt::AutoConnection);
    connect(this,&CommonButtonGroupWidget::sigSendXMLAnswer,sendSystem,&SendSystem::xmlAnswer,Qt::DirectConnection);
}

void CommonButtonGroupWidget::updateProgressBar(float value)
{
    ui->loadingProgressBar->setValue(value);
}

void CommonButtonGroupWidget::loadCompleteState()
{
    ui->updateButton->setEnabled(false);
    ui->startButton->setEnabled(true);
    ui->loadingProgressBar->setValue(100);
    ui->startButton->show();
    ui->loadingProgressBar->hide();
}

void CommonButtonGroupWidget::lastVerInstalledState()
{
    show();
    ui->loadingProgressBar->hide();
    ui->startButton->show();
}

void CommonButtonGroupWidget::disconnectState()
{
    ui->startButton->hide();
    ui->loadingProgressBar->hide();
    ui->updateButton->hide();
    ui->updateButton->setEnabled(false);
}

void CommonButtonGroupWidget::startUpdateState()
{
    ui->updateButton->hide();
    ui->startButton->hide();
    ui->loadingProgressBar->setValue(0);
    ui->loadingProgressBar->show();
}

void CommonButtonGroupWidget::showProgressBar(bool flag)
{
    if (flag) ui->loadingProgressBar->show();
    else ui->loadingProgressBar->hide();
}

void CommonButtonGroupWidget::needUpdateState(bool flag)
{
    ui->startButton->show();
    ui->updateButton->setEnabled(flag);
    ui->startButton->setEnabled(externalExecuter->findApp());
    ui->updateButton->show();
}

void CommonButtonGroupWidget::startButtonActive(bool flag)
{
    ui->startButton->setEnabled(flag);
}

void CommonButtonGroupWidget::on_updateButton_clicked()
{
    emit sigSendCommand("update");
    startUpdateState();
}

void CommonButtonGroupWidget::on_startButton_clicked()
{
    externalExecuter->callApp();
    emit sigSendXMLAnswer("DISABLE");
}

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




