#!/usr/bin/python
#coding=utf8

from PyQt4.QtGui import QDialog,QDesktopWidget
from PyQt4.QtCore import Qt,SIGNAL,QString

from ui_qss.winCheckUpgradeQss import WinCheckUpgradeQss
from upgradeDownload.upgradeDownload import UpgradeDownload
from checkUpgrade.checkUpgrade import CheckUpgrade
from common.info_dialog import InfoDialog

class WinCheckUpgrade (WinCheckUpgradeQss):

    def __init__(self,versionUpgradeInfo, url=None,parent=None):
        super(WinCheckUpgrade, self).__init__(parent)
        self.__rejectInfo = 'cancel'
        self.__flag = False
        self.__url = None
        self.__upgradeDowndload = None
        self.__checkupgrade = CheckUpgrade(versionUpgradeInfo["centerIp"],
                                           versionUpgradeInfo["curVersion"])



        # self.connect(self.btn_cancle,SIGNAL("clicked()"),self.close)
        self.connect(self.btn_cancle,SIGNAL("clicked()"),self.__cancle_upgrade) # 取消 升级　
        self.connect(self.btn_ok,SIGNAL("clicked()"),self.__sendLogoutSingnal)  # 确定 升级

        # self.__checkupgradeFunc()

    def getRejectInfo(self):
        return self.__rejectInfo

    def __cancle_upgrade(self):
        self.__rejectInfo = 'cancel'
        CheckUpgrade.omitVersion = CheckUpgrade.centerVersion
        self.reject()

    def checkUpgradeFunc(self): # 检查 升级
        try:
            flag ,upgradeInfo = self.__checkupgrade.checkUpgrade()
            if flag == True:
                # upgradeInfo = {
                #       "update"   : False,
                #       "isForced" : False,
                #       "version"  : None,
                #       "upgrade_url" : None
                #   }

                if upgradeInfo["update"] == True:
                    self.__upgradeDowndload = UpgradeDownload(upgradeInfo["upgrade_url"])

                    # 设置 显示版本号
                    msg = "发现新版本: V%s" %  upgradeInfo["version"]
                    self.label.setText(QString(msg))             # 设置 显示版本号

                    if upgradeInfo["isForced"] != "0":          # 如果为强制升级
                        self.btn_cancle.hide()                  # 强制　升级　隐藏　取消　按钮

                    return True  # 需要升级
                else:
                    return False  # 不需要升级
            else:
                return False

        except Exception as e:
            return False

    def __sendLogoutSingnal(self):
        # self.close()
        self.hide()
        if self.__flag == False:
            self.__flag = True
        else:
            self.__rejectInfo = 'downloading'
            self.reject()
            # return None

        try:
            if self.__upgradeDowndload.download() == True:
                InfoDialog.instance().InformationTimer(u"下载成功",u"下载成功,准备升级...",2)# timer waiting ....
                self.__checkupgrade.upgrade()           # 向父线程,发送升级安装请求

                self.accept()
            else:
                InfoDialog.instance().InformationTimer(u"升级失败",u"下载失败",5)# timer waiting ....
                self.__rejectInfo = 'downloadfailed'
                self.reject()
        finally:
            self.__flag = False
            # self.close()

    def keyPressEvent(self,event):
        if (event.key() == Qt.Key_Enter) or (event.key() == Qt.Key_Return):
            self.__sendLogoutSingnal()

##############################
#   单元测试
##############################
if __name__ == "__main__":
    from PyQt4 import QtGui
    from os import environ
    environ["WIN_UI_IMAGES_PATH"]="../../images"
    app = QtGui.QApplication([])

    versionUpgradeInfo = {
        "centerIp"  :"192.168.102.33:8080",
        "curVersion":"4.5.0.20160405"
    }
    from PyQt4.QtCore import QTextCodec
    QTextCodec.setCodecForCStrings(QTextCodec.codecForLocale())  # 支持中文
    win = WinCheckUpgrade(versionUpgradeInfo)
    #win.show()
    app.exec_()