#include "mtitlebar.h"
#include "LoUIClass/aboutdlg.h"
#include "LoUIClass/updaterdialog.h"
#include "LoUIClass/updaterfirmwaredialog.h"
#include "LoUIClass/SoftConfigDialog.h"
#include "LoUIClass/x_uimsgboxok.h"
#include "globaldefine.h"
#include <QDesktopServices>
#include "wDevicesManager/upgradeapkdialog.h"
//标题工具栏
static const QString DEFS_URL = "https://www.ledok.cn/download/definitions/updates.json";

mTitleBar::mTitleBar(QWidget *parent) :
    LoQTitleBar(parent)
{
    bn_Setting = new QPushButton(this);
    bn_Setting->setObjectName("bnSetting");
    //水平布局，放置配置按钮，最小化，最大化，关闭按钮
    QHBoxLayout *pLayout = new QHBoxLayout(this);
    pLayout->addStretch();
    pLayout->addWidget(bn_Setting);
    pLayout->addWidget(bn_Minimize);
    pLayout->addWidget(bn_Maximize);
    pLayout->addWidget(bn_Close);
    pLayout->setContentsMargins(0, 3, 3, 0);
    setLayout(pLayout);

    QMenu *menu_setting = new QMenu(this);
    act_lang   = new QAction(tr("Language"));
   // QAction *act_radio  = new QAction(tr("Radio Setting"));
  //  QAction *act_custom = new QAction(tr("Custom Server"));
   // QAction *act_dhcp   = new QAction(tr("DHCP Setting"));
   // act_setting= new QAction(tr("Setting"));
    act_help   = new QAction(tr("Help"));
    act_update   = new QAction(tr("Check for updates"));
    act_updatefirmware= new QAction(tr("firmware manager"));
    act_softconfiguration= new QAction(tr("Software Configuration"));


    act_about  = new QAction(tr("About"));
    //act_setting->setObjectName("Setting");
    act_help->setObjectName("Help");
    act_update->setObjectName("Update");
    act_updatefirmware->setObjectName("Update firmware");
    act_softconfiguration->setObjectName("Software Configuration");
    act_about->setObjectName("About");
    menu_setting->addAction(act_lang);
    //menu_setting->addAction(act_radio);
   // menu_setting->addAction(act_custom);
   // menu_setting->addAction(act_dhcp);
    //menu_setting->addAction(act_setting);
    menu_setting->addAction(act_softconfiguration);
    menu_setting->addAction(act_update);
    menu_setting->addAction(act_updatefirmware);
    menu_setting->addAction(act_help);
    menu_setting->addAction(act_about);
    bn_Setting->setMenu(menu_setting);//按钮上添加下拉菜单
    //创建语言菜单的子菜单栏
    QMenu *menu_lang = new QMenu(this);
    QActionGroup *actg_lang = new QActionGroup(this);
    actg_lang->setObjectName("Language");
    QAction *lang_zhCN = new QAction(tr("中文"),  actg_lang);
    QAction *lang_zhTH = new QAction(tr("中文繁体"),  actg_lang);
    QAction *lang_enUS = new QAction(tr("English"),  actg_lang);
    QAction *lang_jaJP = new QAction(tr("日本語"), actg_lang);
    lang_zhCN->setCheckable(true);
    lang_zhTH->setCheckable(true);
    lang_enUS->setCheckable(true);
    lang_jaJP->setCheckable(true);
    lang_zhCN->setObjectName("zhCN");
    lang_zhTH->setObjectName("zhTH");
    lang_enUS->setObjectName("enUS");
    lang_jaJP->setObjectName("jaJP");
    menu_lang->addAction(lang_zhCN);
    menu_lang->addAction(lang_zhTH);
    menu_lang->addAction(lang_enUS);
    menu_lang->addAction(lang_jaJP);
    act_lang->setMenu(menu_lang);
//获取当前的语言选型
    QAction *lang_cur;
    LoAppConfig *cfg = LoAppConfig::getInstance();

    QSettings *settings = new QSettings(cfg->OrganizationName(), cfg->ApplicationName());
    if(settings->value("MainSetting/Language").isValid())
    {
        QString lang = settings->value("MainSetting/Language").toString();
        if(lang == "enUS")
        {
            lang_cur = lang_enUS;
        }
        else if(lang == "jaJP")
        {
            lang_cur = lang_jaJP;
        }
        else  if(lang == "zhTH")
        {
            lang_cur = lang_zhTH;
        }
        else
        {
            lang_cur = lang_zhCN;
        }
    }
    else
    {
        lang_cur = lang_zhCN;
    }
    if(lang_cur) {
        lang_cur->setChecked(true);
        m_lang = lang_cur->objectName();
    }
    bn_Setting->setToolTip(tr("Setting"));
//语言菜单的信号和槽函数
    connect(menu_lang, SIGNAL(triggered(QAction*)), this, SLOT(onLanguageChanged(QAction*)));
    connect(menu_setting, SIGNAL(triggered(QAction*)), this, SLOT(onMainMenuChanged(QAction*)));

    QSimpleUpdater *m_updater1=QSimpleUpdater::getInstance();
    m_updater=m_updater1;
    connect(m_updater1,SIGNAL(checkingFinished(QString)),this,SLOT(updateChangelog(QString)));
    m_updater1->getInstance()->setModuleVersion(DEFS_URL,APP_VERSION);
    m_updater1->getInstance()->setNotifyOnUpdate(DEFS_URL,false);
    m_updater1->getInstance()->setNotifyOnFinish(DEFS_URL,false);
    m_updater1->getInstance()->checkForUpdates(DEFS_URL);

}

mTitleBar::~mTitleBar()
{
    //关闭的时候保存当前语言的选项
    QActionGroup *ag = this->findChild<QActionGroup*>("Language");
    if(ag) {
        LoAppConfig *cfg = LoAppConfig::getInstance();
        QSettings *settings = new QSettings(cfg->OrganizationName(), cfg->ApplicationName());
        QString lang = ag->checkedAction()->objectName();
        if(lang.isEmpty()) lang = "zhCN";
        settings->setValue("MainSetting/Language", lang);
    }
}

void mTitleBar::setupLang()
{
    emit sigLanguageChanged(m_lang);
}

void mTitleBar::onLanguageChanged(QAction* act)
{
    QActionGroup *ag = this->findChild<QActionGroup*>("Language");
    if(ag) {
        LoAppConfig *cfg = LoAppConfig::getInstance();
        QSettings *settings = new QSettings(cfg->OrganizationName(), cfg->ApplicationName());
        settings->setValue("MainSetting/Language", act->objectName());
    }
    emit sigLanguageChanged(act->objectName());
}
void mTitleBar::onMainMenuChanged(QAction* act)
{
    if(act->objectName()=="About")
    {
        AboutDlg *pDlg=new AboutDlg(this);
        pDlg->exec();


    }
    else if(act->objectName()=="Help")
    {
        QString qexeFullPath = QCoreApplication::applicationDirPath();
        QString qtManulFile=qexeFullPath+"/help/"+m_lang+".pdf";

        QDesktopServices::openUrl(QUrl::fromLocalFile(qtManulFile));
    }
    else if(act->objectName()=="Update")
    {
        UpdaterDialog *pDlg=new UpdaterDialog(this);
        pDlg->exec();
    }
    else if(act->objectName()=="Update firmware")
    {
        //UpdaterFirmwareDialog *pDlg=new UpdaterFirmwareDialog(this);
       // pDlg->exec();
        UpgradeApkDialog *pDlg=new UpgradeApkDialog(this,"","");
        pDlg->exec();
    }
    else if(act->objectName()=="Software Configuration")
    {
        SoftConfigDialog *pDlg=new SoftConfigDialog(this);
        pDlg->exec();
        emit sigGuangYingPinChanged();
    }


    //emit sigLanguageChanged(act->objectName());
}
void mTitleBar::refreshLable()
{
     act_lang->setText(tr("Language"));
    // act_radio->setText(tr("Radio Setting"));
    //  act_custom->setText(tr("Custom Server"));
    // act_dhcp->setText(tr("DHCP Setting"));
    //act_setting->setText(tr("Setting"));
    act_help->setText(tr("Help"));
    act_about->setText(tr("About"));
    act_update->setText(tr("Check for updates"));
    act_updatefirmware->setText(tr("firmware manager"));
    act_softconfiguration->setText(tr("Software Configuration"));
    bn_Setting->setToolTip(tr("Setting"));
    LoQTitleBar::refreshLable();


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

   if(m_updater->getInstance()->getUpdateAvailable(DEFS_URL))//如果版本大于
   {
       if(m_updater->getInstance()->getUpdateSameVersionAvailable(DEFS_URL))//如果同样的版本，不显示红点提示
       {
           bn_Setting->setStyleSheet("image: url(:/res/AppSetting.png);");
       }
       else//如果不同
       {
           bn_Setting->setStyleSheet("image: url(:/res/AppSettingTip.png);");
           QIcon icon(":/res/reddot.png");
           act_update->setIcon(icon);
       }

   }
   else {
           bn_Setting->setStyleSheet("image: url(:/res/AppSetting.png);");
    }
  // disconnect(m_updater,SIGNAL(checkingFinished(QString)),this,SLOT(updateChangelog(QString)));

}
void mTitleBar::Close()
{
    bn_Close->click();
}
