# -*- coding: utf-8 -*-  

from PyQt4.QtGui  import QHBoxLayout, QVBoxLayout, QLabel, QMovie, QWidget, QPushButton, QPixmap,QPalette,QColor,QFont
from PyQt4.QtCore import SIGNAL, QTranslator, QCoreApplication, Qt
from storeinfoparser import StoreInfoParser
    
class LoadingWidget(QWidget):
        
    def __init__(self,parent = None):
        super(LoadingWidget,self).__init__(parent)
        
        self.tryLinkStatus = True
        self.status = ""
        
        self.loadingMovie = QMovie("images/loading.gif")
        self.movieLabel = QLabel(self)
        self.movieLabel.setMovie(self.loadingMovie)
        self.loadingMovie.start()
        self.disconnectBtn = QPushButton(self.tr("Disconnect"), self)
        
        self.disconnectBtn.setStyleSheet("QPushButton{border:1px solid lightgray; width: 160px; height: 60px; font: bold 20px;color: white; background:transparent;}" \
                                        "QPushButton:hover{border-color:green; font: bold 20px; background:rgb(104,192,156);}")
        
        self.infoLabel = QLabel(self.tr("User logging, connecting to server..."))
        self.infoLabel.setFont(QFont("Times New Roman", 25))
        self.infoLabel.setFixedHeight(250)
        self.infoLabel.setFixedWidth(530)
        
        label1Palette = QPalette()
        label1Palette.setColor(QPalette.WindowText, QColor(255,255,255))
        self.infoLabel.setPalette(label1Palette)
        #self.infoLabel.setAlignment(Qt.AlignCenter)
        
        topLayout = QHBoxLayout()
        topLayout.addWidget(self.movieLabel)
        topLayout.addWidget(self.infoLabel)
        
        
        bottomLayout = QHBoxLayout()
        bottomLayout.addStretch()
        bottomLayout.addWidget(self.disconnectBtn)
        bottomLayout.addStretch()
        
        self.detailLabel = QLabel()
        self.detailLabel.hide()
        self.detailLabel.setPalette(label1Palette)
        self.detailLabel.setFont(QFont("Times New Roman", 20))
        #self.detailLabel.setAlignment(Qt.AlignCenter)
        detailLabelLayout = QHBoxLayout()
        detailLabelLayout.addStretch()
        detailLabelLayout.addWidget(self.detailLabel)
        detailLabelLayout.addStretch()
        detailLabelLayout.addSpacing(30)
        
        mainLayout = QVBoxLayout()
        mainLayout.addLayout(topLayout)
        mainLayout.addSpacing(20)
        mainLayout.addLayout(detailLabelLayout)
        mainLayout.addSpacing(100)
        mainLayout.addLayout(bottomLayout)
        
        self.setLayout(mainLayout)
        
        self.connect(self.disconnectBtn, SIGNAL("clicked()"),self.slotDisconnectLink)
        
        
    def resizeEvent(self,event):
        language = StoreInfoParser.instance().getLanguage()
        #if language == "english":
        self.infoLabel.setFixedWidth(self.width()-130)
        self.infoLabel.setFont(QFont("Times New Roman", self.width()/30))
        self.detailLabel.setFont(QFont("Times New Roman", self.width()/30))
        
    def initLanguage(self):
        language = StoreInfoParser.instance().getLanguage()
        m_pTranslator = QTranslator()
        exePath = "./"
        if language == "chinese":
            QmName = "zh_CN.qm"
        else:
            QmName = "en_US.qm"
        
        if(m_pTranslator.load(QmName, exePath)):
            QCoreApplication.instance().installTranslator(m_pTranslator)
        
    def slotDisconnectLink(self):
        
        language = StoreInfoParser.instance().getLanguage()
        m_pTranslator = QTranslator()
        exePath = "./"
        if language == "chinese":
            QmName = "zh_CN.qm"
        else:
            QmName = "en_US.qm"
        if(m_pTranslator.load(QmName, exePath)):
            QCoreApplication.instance().installTranslator(m_pTranslator)
        #self.initLanguage()
        if self.tryLinkStatus:
            self.emit(SIGNAL("changeLinkStatusButton"), "disconnect")
            
            self.infoLabel.setText(self.tr("Can not connect to server, please check") + "\n__________________________________")
            self.detailLabel.setText("\n" + self.tr("is network all right") + "\n" + self.tr("is server address all right"))
            self.detailLabel.show()
            self.loadingMovie.stop()
            self.movieLabel.setPixmap(QPixmap("images/error.png"))
            self.disconnectBtn.setText(self.tr("Reconnect"))

            self.tryLinkStatus = False
        else:
            self.restartLink()
            
    def updateWindow(self):
        
        if self.status == "failed":
            self.infoLabel.setText(self.tr("Can not connect to server, please check") + "\n__________________________________")
            self.detailLabel.setText("\n" + self.tr("is network all right") + "\n" + self.tr("is server address all right"))
            
        elif self.status == "noLicense":
            self.infoLabel.setText(self.tr("no license:") + "\n_____________________")
            self.detailLabel.setText("\n" + self.tr("get license") + "\n" + self.tr("is server address all right"))
            
        elif self.status == "curriculaIsNone" or self.status == "reachToplimit" or self.status == "cloudsServerStateDisable" or self.status == "terminalVMIsNone":
            if self.status == "curriculaIsNone":
                #self.infoLabel.setText(u"未查询到相应的课程，请同管理\n员联系")
                self.infoLabel.setText(self.tr("No course!"))
            elif self.status == "reachToplimit":
                #self.infoLabel.setText(u"云桌面数量已达上线，请同管理\n员联系")
                self.infoLabel.setText(self.tr("Clouds desktop is reach top"))
            elif self.status == "cloudsServerStateDisable":
                #self.infoLabel.setText(u"云主机处于禁止使用状态，请同\n管理员联系")
                self.infoLabel.setText(self.tr("Cloud server is disable"))
            elif self.status == "terminalVMIsNone":
                #self.infoLabel.setText(u"未监测到分配给此学生机的虚拟\n机")
                self.infoLabel.setText(self.tr("No cloud desktop!"))
            
        else:
            self.infoLabel.setText(self.tr("Can not connect to server, please check") + "\n__________________________________")
            self.detailLabel.setText("\n" + self.tr("is network all right") + "\n" + self.tr("is server address all right"))
            
                
                
        self.disconnectBtn.setText(self.tr("Reconnect"))
            
        
            
            
    def restartLink(self):
        
        
        language = StoreInfoParser.instance().getLanguage()
        m_pTranslator = QTranslator()
        exePath = "./"
        if language == "chinese":
            QmName = "zh_CN.qm"
        else:
            QmName = "en_US.qm"
        if(m_pTranslator.load(QmName, exePath)):
            QCoreApplication.instance().installTranslator(m_pTranslator)
            
            
        self.initLanguage()
        self.emit(SIGNAL("changeLinkStatus"), "restartLink")
        self.movieLabel.setPixmap(QPixmap(None))
        self.movieLabel.setMovie(self.loadingMovie)
        self.loadingMovie.start()
        self.infoLabel.setText(self.tr("User logging, connecting to server..."))
        self.disconnectBtn.setText(self.tr("Disconnect"))
        self.detailLabel.hide()
        self.tryLinkStatus = True
            
    def setHintInfo(self, status, failNum):
        self.status = status
        language = StoreInfoParser.instance().getLanguage()
        m_pTranslator = QTranslator()
        exePath = "./"
        if language == "chinese":
            QmName = "zh_CN.qm"
        else:
            QmName = "en_US.qm"
        if(m_pTranslator.load(QmName, exePath)):
            QCoreApplication.instance().installTranslator(m_pTranslator)
            
            
        if status == "connecting":
            self.infoLabel.setText(self.tr("trying restart connect, connect num:") + "%d" % failNum)
            self.movieLabel.setMovie(self.loadingMovie)
            self.detailLabel.hide()
            self.loadingMovie.start()
        elif status == "failed":
            self.infoLabel.setText(self.tr("Can not connect to server, please check") + "\n__________________________________")
            self.detailLabel.setText("\n" + self.tr("is network all right") + "\n" + self.tr("is server address all right"))
            self.detailLabel.show()
#             self.infoLabel.setText(u"无法连接云主机，请检查：\n______________________")
#             self.detailLabel.setText(u"\n● 网络是否正常\n● 云主机地址是否配置正确")
#             self.detailLabel.show()
            self.loadingMovie.stop()
            self.movieLabel.setPixmap(QPixmap("images/error.png"))
        elif status == "noLicense":
            self.infoLabel.setText(self.tr("no license:") + "\n__________________________________")
            self.detailLabel.setText("\n" + self.tr("get license") + "\n" + self.tr("is server address all right"))
            self.detailLabel.show()
            self.loadingMovie.stop()
            self.movieLabel.setPixmap(QPixmap("images/error.png"))
        elif status == "curriculaIsNone" or status == "reachToplimit" or status == "cloudsServerStateDisable" or status == "terminalVMIsNone":
            if status == "curriculaIsNone":
                #self.infoLabel.setText(u"未查询到相应的课程，请同管理\n员联系")
                self.infoLabel.setText(self.tr("No course!"))
            elif status == "reachToplimit":
                #self.infoLabel.setText(u"云桌面数量已达上线，请同管理\n员联系")
                self.infoLabel.setText(self.tr("Clouds desktop is reach top"))
            elif status == "cloudsServerStateDisable":
                #self.infoLabel.setText(u"云主机处于禁止使用状态，请同\n管理员联系")
                self.infoLabel.setText(self.tr("Cloud server is disable"))
            elif status == "terminalVMIsNone":
                #self.infoLabel.setText(u"未监测到分配给此学生机的虚拟\n机")
                self.infoLabel.setText(self.tr("No cloud desktop!"))
                
            self.detailLabel.hide()
            self.loadingMovie.stop()
            self.movieLabel.setPixmap(QPixmap("images/close.png"))
            self.emit(SIGNAL("changeLinkStatus"), "disconnect")
            self.disconnectBtn.setText(self.tr("Reconnect"))
            self.tryLinkStatus = False
            
        elif status == "restart":
            self.emit(SIGNAL("changeLinkStatus"),"restartLink")
            self.tryLinkStatus = True
            
        
    def hide(self):
        self.loadingMovie.stop()
        QWidget.hide(self)
        
    def show(self):
        self.loadingMovie.start()
        QWidget.show(self)
        
        
    