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

from PyQt4.QtCore import QObject
from model.cfg.configfileparser import ConfigFileParser


class StoreInfoParser(QObject):
    _instance = 0 
    def __init__(self):
        super(StoreInfoParser,self).__init__()
        self.parser = ConfigFileParser()

        
    @classmethod 
    def instance(cls):
        if (cls._instance == 0):
            cls._instance = StoreInfoParser()
            
        return cls._instance
    def getAutoAdjust(self):#分辨率自适应
        parser = ConfigFileParser()
        if parser.has_section("autoadjust"):
            if parser.has_option("autoadjust", "autoadjust"):
                return parser.getValue("autoadjust", "autoadjust")
        return ""

    def setAutoAdjust(self,autoadjust):
        parser = ConfigFileParser()
        if parser.has_section("autoadjust"):
            if parser.has_option("autoadjust", "autoadjust"):
                parser.changeSectionValue("autoadjust", "autoadjust", autoadjust)
            else:
                parser.addOption("autoadjust", "autoadjust", autoadjust)
        else:
            parser.addSection("autoadjust", "autoadjust", autoadjust)


    def setAutoVmScreenState(self, autoScreenState):
        parser = ConfigFileParser()
        if parser.has_section("autoScreenState"):
            if parser.has_option("autoScreenState", "autoScreenState"):
                parser.changeSectionValue("autoScreenState", "autoScreenState", autoScreenState)
            else:
                parser.addOption("autoScreenState", "autoScreenState", autoScreenState)
        else:
            parser.addSection("autoScreenState", "autoScreenState", autoScreenState)

    def getAutoVmScreenState(self):
        parser = ConfigFileParser()
        if parser.has_section("autoScreenState"):
            if parser.has_option("autoScreenState", "autoScreenState"):
                return parser.getValue("autoScreenState", "autoScreenState")
        return ""

    def setConfigFileValue(self,option,value):
        parser = ConfigFileParser()
        if parser.has_section(option):
            if parser.has_option(option,option):
                parser.changeSectionValue(option,option,value)
            else:
                parser.addOption(option,option,value)
        else:
            parser.addSection(option,option,value)

    def getConfigFileValue(self,option):
        parser = ConfigFileParser()
        if parser.has_section(option):
            if parser.has_option(option,option):
                return parser.getValue(option,option)
        return ""


    def changeConfigFile(self, section, option, value):
        parser = ConfigFileParser()
        if parser.has_section(section):
            if parser.has_option(section, option):
                parser.changeSectionValue(section, option, value)
            else:
                parser.addOption(section, option, value)
        else:
            parser.addSection(section, option, value)

    def getConfigFileInfo(self, section, option):
        parser = ConfigFileParser()
        if parser.has_section(section):
            if parser.has_option(section, option):
                return parser.getValue(section, option)
        return None
    
    def deleteSection(self, section):
        if self.parser.has_section(section):
            self.parser.remove_section(section)

    def getCloudsServerIP(self):
        return self.getConfigFileInfo("cloudsServerAddress", "ip")
        
    
    
    def setServerAddress(self, serverIp):
        #保存服务器地址到配置文件
        self.changeConfigFile("cloudsServerAddress", "ip", serverIp)

            
    def getScreenSaver(self):#屏保
        return self.getConfigFileInfo("screensaver", "screensaver")
        
    
    def setScreenSaver(self, screensaver):
        self.changeConfigFile("screensaver", "screensaver", screensaver)


    def getScreen(self):#屏幕分辨率设置
        return self.getConfigFileInfo("screen", "screen")
    
      
    def setScreen(self, screen):
        self.changeConfigFile("screen", "screen", screen)
       
    def getNetworkSwitch(self):
        return self.getConfigFileInfo("networkswitch", "networkswitch")
       
       
    def setNetworkSwitch(self, networkswitch):
        self.changeConfigFile("networkswitch", "networkswitch", networkswitch)
       
    def setLoginUserName(self, name):
        self.changeConfigFile("loginUser", "name", name)
            
    def getLoginUserName(self):
        return self.getConfigFileInfo("loginUser", "name")
    
    def setLoginCenterName(self, centerName):
        self.changeConfigFile("centerName", "name", centerName)
        
    def getLoginCenterName(self):
        return self.getConfigFileInfo("centerName", "name")
    
    def setAutoLoginInfo(self, autoLogininfo):
        self.changeConfigFile("autoLogin", "info", autoLogininfo)
    
    def getAutoLoginInfo(self):
        return self.getConfigFileInfo("autoLogin", "info")
       
       
    def deleteAutoLoginInfo(self):
        self.deleteSection("autoLogin")
        
    def deleteLoginUserName(self):
        self.deleteSection("loginUser")
        
        
    def getUsbIsAutoConnect(self):
        return self.getConfigFileInfo("usbset", "usbset")
    
    def setUsbAutoConnectState(self, value):
        self.changeConfigFile("usbset", "usbset", value)
        
    def getMultiConnectState(self):#多平设置
        return self.getConfigFileInfo("multimonitor", "multimonitor")
    
    def setMultiConnectState(self, statue):
        self.changeConfigFile("multimonitor", "multimonitor", statue)
       
        