#include "updateledset3dialog.h"
#include "ui_updateledset3dialog.h"
#include "x_uimsgboxok.h"
#include <QSettings>
#include <loappconfig.h>
static const QString DEFS_LEDSET_URL = "https://www.ledok.cn/download/definitions/ledset3updates.json";

UpdateLedset3Dialog::UpdateLedset3Dialog(QWidget *parent) :
    LoQDialog(parent),
    ui(new Ui::UpdateLedset3Dialog)
{
    ui->setupUi(this);
    ui->pushButton_2->setVisible(false);

    QString strCurApkVersion="";
        LoAppConfig *cfg = LoAppConfig::getInstance();
       QSettings *settings = new QSettings(cfg->OrganizationName(), cfg->ApplicationName());
       strCurApkVersion=settings->value(DEFS_LEDSET_URL).toString();
       if(strCurApkVersion.isEmpty())
           strCurApkVersion="0.0.0";
       strCurApkVersion="0.0.0";

    setStyleSheet("background-color:#D8D8D8");
    ui->label_2->setText(tr("CurVersion")+":"+strCurApkVersion);
    ui->label_2->setVisible(false);

    QSimpleUpdater *m_updater=QSimpleUpdater::getInstance();
    connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(OnCheckForUpdates()));
    connect(m_updater,SIGNAL(checkingFinished(QString)),this,SLOT(updateChangelog(QString)));
    connect(m_updater,SIGNAL(downloadFinished(QString,QString)),this,SLOT(OnDownloadFinished(QString,QString)));


    m_updater->getInstance()->setModuleVersion(DEFS_LEDSET_URL,strCurApkVersion);
    m_updater->getInstance()->setNotifyOnUpdate(DEFS_LEDSET_URL,false);
    m_updater->getInstance()->setNotifyOnFinish(DEFS_LEDSET_URL,false);
    m_updater->getInstance()->setCompareBySameString(DEFS_LEDSET_URL,true);
    m_updater->getInstance()->checkForUpdates(DEFS_LEDSET_URL);
    ui->pushButton->setProperty("ssType", "progManageTool");
    ui->pushButton->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButton_2->setProperty("ssType", "progManageTool");
    ui->pushButton_2->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");

}

UpdateLedset3Dialog::~UpdateLedset3Dialog()
{
    delete ui;
}
void UpdateLedset3Dialog::OnDownloadFinished( QString url,  QString filepath)
{
    ui->label_2->setText(tr("CurVersion")+":"+m_updater->getInstance()->getLatestVersion(DEFS_LEDSET_URL));
    ui->textEdit->setText(tr("Apk is download finished from internet,you can select LED Screen SYNC firmware now!"));
    ui->pushButton_2->setVisible(false);
    LoAppConfig *cfg = LoAppConfig::getInstance();
    QSettings *settings = new QSettings(cfg->OrganizationName(), cfg->ApplicationName());
    settings->setValue(url, m_strLastestVersion);
    settings->setValue("ledset", filepath);
//    m_updater->getInstance()->setNotifyOnUpdate(DEFS_FIRMWARE_URL,false);
//    m_updater->getInstance()->setNotifyOnFinish(DEFS_FIRMWARE_URL,false);
//    m_updater->getInstance()->checkForUpdates(DEFS_FIRMWARE_URL);
}

void UpdateLedset3Dialog::OnCheckForUpdates()
{
    m_updater->getInstance()->setNotifyOnUpdate(DEFS_LEDSET_URL,true);
    m_updater->getInstance()->setNotifyOnFinish(DEFS_LEDSET_URL,false);
   // m_updater->getInstance()->setUseCustomInstallProcedures(DEFS_LEDSET_URL,true);
    m_updater->getInstance()->setNoNotifyDownload(DEFS_LEDSET_URL,true);
    m_updater->getInstance()->setCompareBySameString(DEFS_LEDSET_URL,true);
    m_updater->getInstance()->setMandatoryUpdate(DEFS_LEDSET_URL,false);
    m_updater->getInstance()->checkForUpdates(DEFS_LEDSET_URL);

}
void UpdateLedset3Dialog::updateChangelog(QString strTip)
{
     Q_UNUSED(strTip)

   if(m_updater->getInstance()->getUpdateAvailable(DEFS_LEDSET_URL))
   {
       QString strtip=tr("LatestVersion:");
       m_strLastestVersion=m_updater->getInstance()->getLatestVersion(DEFS_LEDSET_URL);
       strtip+=  m_strLastestVersion +"\r\n";
       strtip+=  tr("Update log:")+"\r\n"+m_updater->getInstance()->getChangelog (DEFS_LEDSET_URL) ;
       ui->textEdit->setText(strtip);
       ui->pushButton_2->setVisible(true);
   }
   else {
        ui->textEdit->setText(tr("The current version is already the latest version,,you can select LED Screen SYNC firmware now!"));
    }
   disconnect(m_updater,SIGNAL(checkingFinished(QString)),this,SLOT(updateChangelog(QString)));
}
