#coding:utf-8
'''
Created on Sep 3, 2014

@author: root
'''
from PyQt4.QtCore import SIGNAL, Qt, QString
from PyQt4.QtGui import QComboBox
from PyQt4.QtGui import QGridLayout
from PyQt4.QtGui import QGroupBox
from PyQt4.QtGui import QLineEdit, QRadioButton
from PyQt4.QtGui import QPushButton
from PyQt4.QtGui import QWidget, QLabel, QHBoxLayout, QVBoxLayout

from base.infohintdialog import InfoHintDialog
from setting.getlogin import AutoLog
from setting.logrecord import LogRecord


class LoginSetting(QWidget):
       
    def __init__(self,parent = None):
        super(LoginSetting,self).__init__(parent)
       
        self.autoLogin = AutoLog()
        
        self.staticStay = QLabel(u"当前状态:")
        self.textLabel = QLabel()
        
        self.radioButton_nologin = QRadioButton()
        self.radioButton_nologin.setText(u"自动登录")
        
        self.radioButton_login = QRadioButton()
        self.radioButton_login.setText(u"取消自动登录")
        
        self.nologinSetting = QGroupBox()
        self.nologinSetting.setTitle(u"自动登录设置")
        
        self.userNameLabel = QLabel(u"用户名")
        self.userNameLabel.setFixedWidth(80)
        self.passwdLabel = QLabel(u"密码")
        self.passwdLabel.setFixedWidth(80)
        self.dnsLabel = QLabel(u"域名")
        self.dnsLabel.setFixedWidth(80)
        
        self.lineEditUserName = QLineEdit()
    
        self.lineEditPasswd = QLineEdit()
        self.lineEditPasswd.setEchoMode(QLineEdit.Password)
        
        self.comboDomin = QComboBox()
       
        self.comboDomin.setEditable(True)
        self.comboDomin.setFocusPolicy(Qt.NoFocus)
        
        self.okButton = QPushButton(u"确定")
        self.okButton.setObjectName("settingDlgBtn")
        self.okButton.setFixedSize(75,27)
        self.okButton.setFlat(True)
        self.closeButton = QPushButton(u"关闭")
        self.closeButton.setObjectName("settingDlgBtn")
        self.closeButton.setFixedSize(75,27)
        self.closeButton.setFlat(True)
        
        saveButtonLayout = QHBoxLayout()
        saveButtonLayout.setMargin(30)
        saveButtonLayout.addStretch()
        saveButtonLayout.addWidget(self.okButton)
        saveButtonLayout.addStretch()
        
        groupLayout = QGridLayout()
    
        groupLayout.addWidget(self.userNameLabel,0,0,1,1)
        groupLayout.addWidget(self.lineEditUserName,0,1,1,1)
        groupLayout.addWidget(self.passwdLabel,1,0,1,1)
        groupLayout.addWidget(self.lineEditPasswd,1,1,1,1)
        groupLayout.addWidget(self.dnsLabel,2,0,1,1)
        groupLayout.addWidget(self.comboDomin,2,1,1,1)
        groupLayout.addLayout(saveButtonLayout,4,0,2,2)
        myGroupLayout = QHBoxLayout()
        myGroupLayout.addSpacing(80)
        myGroupLayout.addLayout(groupLayout)
        myGroupLayout.addSpacing(80)
        
        self.nologinSetting.setLayout(myGroupLayout)
        
        labelLayout = QHBoxLayout()
        labelLayout.addWidget(self.staticStay)
        labelLayout.addWidget(self.textLabel)
        labelLayout.addStretch()
        
        radioLayout = QHBoxLayout()
        radioLayout.addWidget(self.radioButton_nologin)
        radioLayout.addWidget(self.radioButton_login)
        radioLayout.addStretch()
        
        closeButtonLayout = QHBoxLayout()
        closeButtonLayout.addStretch()
        closeButtonLayout.addWidget(self.closeButton)
        
        miniWidget = QWidget()
        vLayout = QVBoxLayout()
        vLayout.setMargin(50)
        vLayout.addLayout(labelLayout)
        vLayout.addSpacing(20)
        vLayout.addLayout(radioLayout)
        vLayout.addSpacing(20)
        vLayout.addWidget(self.nologinSetting)
        vLayout.addSpacing(40)
        vLayout.addLayout(closeButtonLayout)
        miniWidget.setLayout(vLayout)
        
        self.mainLayout = QHBoxLayout(self)
        self.mainLayout.addSpacing(30)
        self.mainLayout.addWidget(miniWidget)
        self.mainLayout.addSpacing(30)
        
        self.refreshWidget()
            
        self.nologinSetting.setEnabled(False)
        
        
        
        self.connect(self.closeButton, SIGNAL("clicked()"),self.closeTabWidget)
        self.connect(self.radioButton_nologin, SIGNAL("clicked()"),self.on_radioButtonNologin_clicked)
        self.connect(self.radioButton_login, SIGNAL("clicked()"),self.on_radioButtonlogin_clicked)
        self.connect(self.okButton,SIGNAL("clicked()"),self.on_okButton_clicked)
    def refreshWidget(self):
        LogRecord.instance().logger.info(u"读取自动登录文件，初始化界面显示")
        username = self.autoLogin.showCurSet()
    
        if(username == ""):
            self.textLabel.setText(u"未设置自动登录")
            self.radioButton_login.setEnabled(False)
        else:
            self.textLabel.setText(u"自动登录")
            self.lineEditUserName.setText(username)
            self.radioButton_nologin.setEnabled(False)    
    def on_radioButtonNologin_clicked(self):
        LogRecord.instance().logger.info(u"点击设置自动登录按钮")
        self.nologinSetting.setEnabled(True)
        domains = self.autoLogin.getDomains()
        if len(domains)==0:
          
            InfoHintDialog(u"平台连接错误！").exec_()
            self.nologinSetting.setEnabled(False)
            self.radioButton_nologin.setCheckable(False)
            return
        self.comboDomin.clear()
        self.comboDomin.addItems(domains)
    def on_radioButtonlogin_clicked(self):
        LogRecord.instance().logger.info(u"点击取消自动登录按钮")
        LogRecord.instance().logger.info(u"取消自动登录，保存到文件")
        result = self.autoLogin.cancelAuto()
        if (result):
          
            InfoHintDialog(u"取消自动登录成功").exec_()
            self.radioButton_login.setEnabled(False)
            self.radioButton_nologin.setEnabled(True)
            self.lineEditUserName.clear()
            self.lineEditPasswd.clear()
            self.comboDomin.clear()
            self.textLabel.setText(u"未设置自动登录")
        return
    def on_okButton_clicked(self):
        usernametext = self.lineEditUserName.text()
        sl = usernametext.split(" ",QString.SkipEmptyParts)
        username = ""
        for i in range(len(sl)):
            username+=sl[i]
        passwd = self.lineEditPasswd.text()
        domain = self.comboDomin.currentText()
        if(username == "" or passwd == "" or domain == ""):
           
            InfoHintDialog(u"任意一项内容不得为空").exec_()
            return
        LogRecord.instance().logger.info(u"获取虚拟机信息")
        getreply = self.autoLogin.sendRequest(username, passwd, domain)
        if getreply == 0:
          
            InfoHintDialog(u"设置自动登录成功").exec_()
            self.nologinSetting.setEnabled(False)
            self.radioButton_login.setEnabled(True)
            self.radioButton_nologin.setEnabled(False)
            self.textLabel.setText(u"自动登录")
            return
        elif getreply == -1:
           
            InfoHintDialog(u"平台地址为空，请先设置平台").exec_()
            return
        elif getreply == -2:
            
            InfoHintDialog(u"服务器返回结果为空，请检查服务器状态").exec_()
            return
        elif getreply == -3:
            
            InfoHintDialog(u"验证失败，请检查用户名、密码").exec_()
            return
        elif getreply == -4:
            
            InfoHintDialog(u"设置自动登录失败，未知错误").exec_()
            return
        return
    
    def closeTabWidget(self):
        self.emit(SIGNAL("closetabwidget"))        
