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

from PyQt4.QtCore import Qt, SIGNAL, QString, QTimer, QTranslator,\
    QCoreApplication
from PyQt4.QtGui import QWidget, QLabel, QComboBox, QGridLayout, QVBoxLayout, QHBoxLayout, QPushButton,\
                        QMessageBox, QDialog
import commands
import os
from configFileParser import ConfigFileParser
from base.infohintdialog import InfoHintDialog
from base.messagebox import MessageBox
from storeinfoparser import StoreInfoParser

class ResolutionSettingWidget(QWidget):
        
    def __init__(self, app, parent = None):
        super(ResolutionSettingWidget,self).__init__(parent)
        self.setStyleSheet("font-size : 16px;")
        self.app = app
        
        self.resolutionLabel = QLabel(self.tr("Resolution setting"))
        
        self.resolutionCombox = QComboBox()
        self.resolutionCombox.setFixedSize(300, 30)
        
        self.saveBtn = QPushButton(self.tr("Save"))
        self.saveBtn.setStyleSheet("background: rgb(7,87,198); color: white; width: 90px; height: 30px;font-size : 16px;")
      
        gridLayout = QGridLayout()
        gridLayout.setSpacing(15)
        gridLayout.setMargin(10)
        gridLayout.addWidget(self.resolutionLabel, 0, 0, 1, 1)
        gridLayout.addWidget(self.resolutionCombox, 0, 1, 1, 1)
        
        topLayout = QHBoxLayout()
        topLayout.addStretch()
        topLayout.addSpacing(50)
        topLayout.addLayout(gridLayout)
        topLayout.addStretch(1)
        
        bottomHLayout = QHBoxLayout()
        bottomHLayout.addStretch()
        bottomHLayout.addWidget(self.saveBtn)
        bottomHLayout.addStretch()
        
        vLayout = QVBoxLayout()
        vLayout.addStretch()
        vLayout.addSpacing(50)
        vLayout.addLayout(topLayout)
        vLayout.addStretch(2)
        vLayout.addSpacing(10)
        vLayout.addLayout(bottomHLayout)
        
        self.setLayout(vLayout)
        
        self.updateResolutionCombox()
        
        self.timer = QTimer()
        self.timer.setSingleShot(True)
        self.connect(self.timer,SIGNAL("timeout()"),self.resetScreenResolution);
        
        self.connect(self.saveBtn, SIGNAL("clicked()"),self.slotSave)
    
    def updateWindow(self):
        self.resolutionLabel.setText(self.tr(self.tr("Resolution setting")))
        self.saveBtn.setText(self.tr("Save"))
        
        
    def getCmdExecValue(self, cmd):
        """得到命令执行的结果"""
        statusOutput = commands.getstatusoutput(cmd)
        monitorList = statusOutput[1].split("\n")
        monitorList = [i for i in monitorList if not i.startswith("xrandr")]
        return monitorList
    
    def showCurrentScreenResolution(self, curResolutionValue):
        """显示当前屏幕的分辨率"""
        count = self.resolutionCombox.count()
        for i in range(count):
            if self.resolutionCombox.itemText(i) == curResolutionValue:
                self.resolutionCombox.setCurrentIndex(i)
                return
            
        self.resolutionCombox.setCurrentIndex(-1)
        
    def getCurrentScreenResolution(self, screenName):
        cmd = "xrandr | awk '{ if(($1 == \"" + screenName + "\") && " +  "($2 == \"connected\"))  {print $3}}'"
        value =  self.getCmdExecValue(cmd)[0].split('+')[0]
        return value
        
    def updateResolutionCombox(self):
        """更新分辨率下拉框的内容"""
        #得到屏幕支持的分辨率
        self.resolutionCombox.clear()
        resolutionmMap = self.getScreenResolution()
        if resolutionmMap:
            screenName = resolutionmMap.keys()[0]
            for resolution in resolutionmMap[screenName]:
                self.resolutionCombox.addItem(resolution)
            self.resolutionCombox.setItemData(0, screenName, Qt.UserRole + 1);
        
        #获取当前屏幕的分辨率
        self.curResolutionValue = self.getCurrentScreenResolution(screenName)
        
        #显示当前屏幕的分辨率
        self.showCurrentScreenResolution(self.curResolutionValue)
        
    def getScreenResolution(self):
        """得到屏幕支持的分辨率"""
        rowStartNumber = QString()
        rowStopNumber = None
        
        #获得显示器名称，连接状态及行号
        Monitors = self.getCmdExecValue("xrandr | awk '{ if($2 ~ \"connected\") {print $1 \" \" $2 \" \" NR}}'");

        #根据组合列表把显示器名称和支持的分辨率放入到一个字典中
        resolutionMap = {}
        count = len(Monitors)
        for i in range(count):
            monitorName = Monitors[i].split(" ")[0]
            linkStatus = Monitors[i].split(" ")[1]
            rowStartNumber = Monitors[i].split(" ")[2]
            
            if linkStatus == "disconnected" or monitorName == "xrandr:":
                continue
     
            if i != count - 1:
                rowStopNumber = Monitors[i+1].split(" ")[2]
      
            resolutionCmd = QString()
#             if rowStopNumber.isEmpty() or rowStopNumber.isNull():
            if rowStopNumber is None:
                resolutionCmd = "xrandr | awk '{ if(NR > " + rowStartNumber + ")" + "{print $1}}'"
            else:
                resolutionCmd = "xrandr | awk '{ if((NR > " + rowStartNumber + ") && (NR < " + rowStopNumber + "))" + "{print $1}}'"
      
            resolutionList = self.getCmdExecValue(resolutionCmd)
            resolutionMap[monitorName] = resolutionList
          
        return resolutionMap
    
    
        
    def slotSave(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)
            
        """改变当前的分辨率"""
        screenName = self.resolutionCombox.itemData(0, Qt.UserRole + 1).toString()
        if not screenName.isEmpty() and not screenName.isNull():
            self.curResolutionValue = self.getCurrentScreenResolution(str(screenName))
            if self.curResolutionValue:
                reScreen = self.resolutionCombox.currentText()
                cmd = "xrandr -s %s" % reScreen
                if os.system(cmd) != 0:
#                     QMessageBox.information(None,u"错误",u'设置分辨率失败')
                    InfoHintDialog(self.tr("set resolution failed")).exec_()
                    return
                
                if QString(reScreen).contains("x"):
                    width = reScreen.split("x")[0]
                    height = reScreen.split("x")[1]
                
                    #调整位置
                    self.emit(SIGNAL("resizePosition"), width, height)
                
                self.timer.start(10*1000)
                
                #弹出提示框，默认为取消
                rb = MessageBox(self.tr("making sure set the resolution to current"), self).exec_()
                if rb == QDialog.Accepted:
                    #关闭定时器，禁止回复原来的分辨率
                    self.timer.stop()
        
                    #把当前的分辨率写入文件
                    self.writeConfigFile(reScreen)
                    
                    #刷新屏幕
                    self.app.slotRefreshVMList()
                    
                elif rb == QDialog.Rejected:
                    self.resetScreenResolution()
                    return
        
                self.curResolutionValue = reScreen
                    
    def resetScreenResolution(self):
        """还原分辨率"""
        #关闭提示窗口
        children = self.children()
        count = len(children)
        for i in range(count):
            if isinstance(children[i], MessageBox):
                children[i].close()
        
        #还原之前的分辨率
        cmd = "xrandr -s %s" % self.curResolutionValue
        if os.system(cmd) != 0:
#             QMessageBox.information(None,u"错误",u'还原分辨率失败')
            InfoHintDialog(self.tr("reserve failed")).exec_()
            return
        
        #刷新分辨率下拉框
        self.updateResolutionCombox()
        
        if QString(self.curResolutionValue).contains("x"):
            width = self.curResolutionValue.split("x")[0]
            height = self.curResolutionValue.split("x")[1]
            self.emit(SIGNAL("resizePosition"), width, height)
            
        #刷新屏幕
        self.app.slotRefreshVMList()
    
    def writeConfigFile(self, curResolution):
        """记录修改后的配置文件"""
        StoreInfoParser.instance().setResolutionValue(curResolution)
        # parser = ConfigFileParser.instance()
        # if parser.has_section("SCREEN_RESOLUTION"):
        #     if parser.has_option("SCREEN_RESOLUTION", "resolution"):
        #         parser.changeSectionValue("SCREEN_RESOLUTION", "resolution", curResolution)
        #     else:
        #         parser.addOption("SCREEN_RESOLUTION", "resolution", curResolution)
        # else:
        #     parser.addSection("SCREEN_RESOLUTION", "resolution", curResolution)
        
        
        
        
        