#include "updaterdialog.h"
#include "ui_updaterdialog.h"
#include "x_uimsgboxok.h"
static const QString DEFS_URL = "https://www.ledok.cn/download/definitions/updates.json";

UpdaterDialog::UpdaterDialog(QWidget *parent) :
    LoQDialog(parent),
    ui(new Ui::UpdaterDialog)
{
    ui->setupUi(this);
    ui->pushButton_2->setVisible(false);
    ui->label_2->setText(tr("CurVersion")+":"+APP_VERSION);
    QSimpleUpdater *m_updater=QSimpleUpdater::getInstance();
    connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(OnCheckForUpdates()));
    connect(m_updater,SIGNAL(checkingFinished(QString)),this,SLOT(updateChangelog(QString)));
    m_updater->getInstance()->setModuleVersion(DEFS_URL,APP_VERSION);
    m_updater->getInstance()->setNotifyOnUpdate(DEFS_URL,false);
    m_updater->getInstance()->setNotifyOnFinish(DEFS_URL,false);
    m_updater->getInstance()->checkForUpdates(DEFS_URL);
//    X_UIMsgBoxOk *pDlg=new X_UIMsgBoxOk(tr("Tip"),tr("UpdaterDialog!"),this,1);
//    pDlg->exec();



}

UpdaterDialog::~UpdaterDialog()
{
    delete ui;
}
void UpdaterDialog::OnCheckForUpdates()
{
    m_updater->getInstance()->setNotifyOnUpdate(DEFS_URL,true);
    m_updater->getInstance()->setNotifyOnFinish(DEFS_URL,false);
    m_updater->getInstance()->setMandatoryUpdate(DEFS_URL,true);

    m_updater->getInstance()->checkForUpdates(DEFS_URL);
//    if(m_updater->getInstance()->getUpdateAvailable(DEFS_URL))
//   {

//    }
//m_updater->getInstance()->checkForUpdates(DEFS_URL);
//    m_updater->getInstance()->usesCustomAppcast (DEFS_URL) ;
//    m_updater->getInstance()->getNotifyOnUpdate (DEFS_URL) ;
//    m_updater->getInstance()->getNotifyOnFinish (DEFS_URL) ;
//    m_updater->getInstance()->getUpdateAvailable (DEFS_URL) ;
//    m_updater->getInstance()->getDownloaderEnabled (DEFS_URL) ;
//    m_updater->getInstance()->usesCustomInstallProcedures (DEFS_URL) ;
//  strtip+=  m_updater->getInstance()->getModuleName (DEFS_URL) ;
//  strtip+=  m_updater->getInstance()->getDownloadUrl (DEFS_URL) ;
//  strtip+=  m_updater->getInstance()->getPlatformKey (DEFS_URL) ;
//  strtip+=  m_updater->getInstance()->getLatestVersion (DEFS_URL) ;
//  strtip+=  m_updater->getInstance()->getModuleVersion (DEFS_URL) ;
//  strtip+=  m_updater->getInstance()->getUserAgentString (DEFS_URL) ;



}
void UpdaterDialog::updateChangelog(QString strTip)
{
     Q_UNUSED(strTip)
//    X_UIMsgBoxOk *pDlg=new X_UIMsgBoxOk(tr("Tip"),DEFS_URL,this,1);
//    pDlg->exec();

   if(m_updater->getInstance()->getUpdateAvailable(DEFS_URL))
   {
//       X_UIMsgBoxOk *pDlg=new X_UIMsgBoxOk(tr("Tip"),"1",this,1);
//       pDlg->exec();
       QString strtip=tr("LatestVersion:");
       strtip+=  m_updater->getInstance()->getLatestVersion(DEFS_URL) +"\r\n";
       strtip+=  tr("Update log:")+"\r\n"+m_updater->getInstance()->getChangelog (DEFS_URL) ;
      // ui->textEdit->setVisible(true);
       ui->textEdit->setText(strtip);
       ui->pushButton_2->setVisible(true);
   }
   else {
//       X_UIMsgBoxOk *pDlg=new X_UIMsgBoxOk(tr("Tip"),"2",this,1);
//       pDlg->exec();
       //ui->textEdit->setVisible(false);
        ui->textEdit->setText(tr("The current version is already the latest version"));
    }
//   disconnect(m_updater,SIGNAL(checkingFinished(QString)),this,SLOT(updateChangelog(QString)));
//    X_UIMsgBoxOk *pDlg1=new X_UIMsgBoxOk(tr("Tip"),tr("Update finished!"),this,1);
//    pDlg1->exec();
}
