#include "updaterfirmwaredialog.h"
#include "ui_updaterfirmwaredialog.h"
#include "x_uimsgboxok.h"
#include <QSettings>
#include <loappconfig.h>
static const QString DEFS_FIRMWARE_URL = "https://www.ledok.cn/download/definitions/player.json";
UpdaterFirmwareDialog::UpdaterFirmwareDialog(QWidget *parent) :
    LoQDialog(parent),
    ui(new Ui::UpdaterFirmwareDialog)
{
    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_FIRMWARE_URL).toString();
       if(strCurApkVersion.isEmpty())
           strCurApkVersion="10.9.9-8";


       QString strfilepath = settings->value("xixunplayer").toString();
       if(strfilepath.isEmpty())
       {
           strfilepath=QCoreApplication::applicationDirPath() + "/Downloads/"+"xixunplayer10.9.9-8.apk";
       }

            QFile tempApk(strfilepath);
            if(!tempApk.exists())
                strCurApkVersion="10.9.9-8";
            else {
                if(strCurApkVersion.isEmpty())
                     strCurApkVersion="10.9.9-8";
            }
    ui->label_2->setText(tr("CurVersion")+":"+strCurApkVersion);
    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_FIRMWARE_URL,strCurApkVersion);
    m_updater->getInstance()->setNotifyOnUpdate(DEFS_FIRMWARE_URL,false);
    m_updater->getInstance()->setNotifyOnFinish(DEFS_FIRMWARE_URL,false);
    m_updater->getInstance()->setCompareBySameString(DEFS_FIRMWARE_URL,false);
    m_updater->getInstance()->checkForUpdates(DEFS_FIRMWARE_URL);


 }

UpdaterFirmwareDialog::~UpdaterFirmwareDialog()
{
    delete ui;
}
void UpdaterFirmwareDialog::OnDownloadFinished( QString url,  QString filepath)
{
    ui->label_2->setText(tr("CurVersion")+":"+m_updater->getInstance()->getLatestVersion(DEFS_FIRMWARE_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("xixunplayer", 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 UpdaterFirmwareDialog::OnCheckForUpdates()
{
    m_updater->getInstance()->setNotifyOnUpdate(DEFS_FIRMWARE_URL,true);
    m_updater->getInstance()->setNotifyOnFinish(DEFS_FIRMWARE_URL,false);
    m_updater->getInstance()->setUseCustomInstallProcedures(DEFS_FIRMWARE_URL,true);
    m_updater->getInstance()->setNoNotifyDownload(DEFS_FIRMWARE_URL,true);
    m_updater->getInstance()->setCompareBySameString(DEFS_FIRMWARE_URL,true);
    m_updater->getInstance()->checkForUpdates(DEFS_FIRMWARE_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 UpdaterFirmwareDialog::updateChangelog(QString strTip)
{
     Q_UNUSED(strTip)

   if(m_updater->getInstance()->getUpdateAvailable(DEFS_FIRMWARE_URL))
   {
       QString strtip=tr("LatestVersion:");
       m_strLastestVersion=m_updater->getInstance()->getLatestVersion(DEFS_FIRMWARE_URL);
       strtip+=  m_strLastestVersion +"\r\n";
       strtip+=  tr("Update log:")+"\r\n"+m_updater->getInstance()->getChangelog (DEFS_FIRMWARE_URL) ;
      // ui->textEdit->setVisible(true);
       ui->textEdit->setText(strtip);
       ui->pushButton_2->setVisible(true);
   }
   else {
       //ui->textEdit->setVisible(false);
        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)));
//    X_UIMsgBoxOk *pDlg=new X_UIMsgBoxOk(tr("Tip"),tr("Update finished!"),this,1);
//    pDlg->exec();
}
