#coding:utf-8
import os
from PyQt4.QtCore import QObject,QString
from model.cfg.storeinfoparser import StoreInfoParser
from common.logrecord import  LogRecord
import json


class CenterApi(QObject):
    def __init__(self,parent = None):
        super(CenterApi,self).__init__(parent)

    #显示中心当前设置，保存在一个文件中，并且返回一个列表。
    def getCaFile(self,ip,wget):
        pass
    
    def addNetSwitchItem(self,centerName,centerIp,centerPort,networkName):
        tempV=[]
        temp={}
        temp["centerName"]=str(unicode(centerName, "utf-8"))
        temp["centerIp"]=str(centerIp)
        temp["centerPort"]=str(centerPort)
        temp["networkName"]=str(networkName)
        tempV.append(temp)
        centerList0 = StoreInfoParser.instance().getCenterList()
        if(centerList0!= None):
            centerList=json.loads(centerList0)
        else:
            centerList={}
        centerList[str(unicode(centerName, "utf-8"))]=temp
        StoreInfoParser.instance().setCenterList(json.dumps(centerList, ensure_ascii=False))
        return 0
    
    def toList(self,listStr):
        listT = []
        listS = "a"
        if(type(listStr) == type(listS)):
            if (listStr == "[]" or listStr == "None" or listStr == ""):
                return []
            else:
                listList = []
                list = listStr.split(",")
                for i in range(len(list)):
                    listList.append(list[i].split("'")[1])
                return listList
            
        elif(type(listStr) == type(listT)):
            return listStr  
        return []  
    

    def setNetworkSwitch(self,centerName,centerIp,centerPort,networkName):#检查重复
        result = self.addNetSwitchItem(centerName, centerIp, centerPort, networkName)
        return result
    
    def getNetSwitchList(self):#获得文件中中心地址的一个列表
        netSwitchList = []
        LogRecord.instance().recordLog(u"进入centerapi的getNetSwitchList函数，获取文件的中心列表")
        contentList0 = StoreInfoParser.instance().getCenterList()    
        LogRecord.instance().recordLog(u"将获取的数据转换为一个真正的列表")
        if not contentList0:
            return netSwitchList
         
        contentList = json.loads(contentList0)
        
        for i in contentList:
            subNetList = {}
            subNetList["netName"]=contentList[i]["centerName"]
            subNetList["netIp"] = contentList[i]["centerIp"]
            subNetList["netPort"] = contentList[i]["centerPort"]
            subNetList["networkName"] = contentList[i]["networkName"]
            netSwitchList.append(subNetList)
            
        LogRecord.instance().recordLog(u"退出函数centerapi的getNetSwitchList函数，返回一个字典的列表")
        return netSwitchList
        

    def delNetWorkSwitchItem(self,caName):#删除中心
        contentCenterList0 = StoreInfoParser.instance().getCenterList()
        if contentCenterList0==None:
            pass
        else:
            contentCenterList = json.loads(contentCenterList0)
            for i in contentCenterList:
                if(i==str(caName)):
                    contentCenterList.pop(i)
                    break
            StoreInfoParser.instance().setCenterList(json.dumps(contentCenterList, ensure_ascii=False)) 
        return 0
    def getText(self,list,text):
        for i in range(len(list)):
            if QString(list[i]).contains(text):
                return list[i]
        return ""

    def restartNetwork(self):
        cmd = QString("service network restart");
        ok = os.system(str(cmd))
        if (ok != 0):
            return False
        return True
    
