# coding:utf-8
import AppObject

###############################
#
#       set is API
#
###############################

class AppUser:
    """
    应用用户类，用户相关的各种信息，包括：
    用户名，密码，平台ip，平台端口，代理ip，代理端口，登录状态，应用列表，会话状态等
    """
    #def __init__(self,name_,password=",agentIP=None.....):
    def __init__(self,name_):
        self.name = name_      # RDP_name
        self.password=''     # RDP_passwd
        self.centerIP=''     # center_Ip
        self.centerPort=''   # center_port
        self.agentIP=''      # RDP_IP
        self.agentPort=''    # RDP_port
        self.rdpState=''     # RDP_state
        self.sessionState='' # center_sessionState
        self.appDictonary={} # 应用列表
        self.appPositionDic={}  # 私有化 self.__appPositionDic={}
        self.loginState=''      #  ？？？？
        self.timeout=30         #  打开应用超时时间。
        self.hasActs=False   #是否具有活动的应用

    def getName(self):  # 私有化
        return self.name
    #
    def setPassword(self,passWord_):
        self.password=passWord_
    def getPassword(self): # 私有化
        return self.password
    
    def setCenterIP(self,centerIP_):
        self.centerIP=centerIP_
    def getCenterIP(self):  # 私有化
        return self.centerIP
    
    def setCenterPort(self,centerPort_):
        self.centerPort=centerPort_
    def getCenterPort(self):# 私有化
        return self.centerPort
    
    def setAgentIP(self,agentIP_):
        self.agentIP=agentIP_
    def getAgentIP(self):# 私有化
        return self.agentIP
    
    def setAgentPort(self,agetPort_):
        self.agentPort=agetPort_
    def getAgentPort(self):# 私有化
        return self.agentPort
    
    def setRdpState(self,rdpState_):
        self.rdpState=rdpState_
    def getRdpState(self):# 私有化
        return self.rdpState
    
    def setSessionState(self,sessionState_):
        self.sessionState=sessionState_
    def getSessionState(self):# 私有化
        return self.sessionState
    
    def getAppDictionary(self): # 私有化
        return self.appDictonary
    #-------------------------------------
    #
    def updateAppDictionary(self,appObject):
        """
        更新应用列表
        """
        appPosition=appObject.getAppPosition()
        appObject.setAppCommand('[startApp::'+appObject.getAppCommand()+']')
        appID=self.findAppID(appPosition)
        if appID == '':
            if appPosition != 0:
                if self.findAppID(appPosition-1) == '':
                    return False
        elif appID != appObject.getAppID():
            while appID != appObject.getAppID():
                appPosition+=1
                appID=self.findAppID(appPosition)
                if appID == '':
                    appObject.setAppPosition(appPosition)
                    break
        self.appDictonary[appObject.getAppID()]=appObject
        self.appPositionDic[appObject.getAppPosition()]=appObject.getAppID()
        return True
    def deleteAppInDictionary(self,appID):
        """
        删除应用列表中的应用
        """
        appObj=self.findAppObjectFromDic(appID)
        if appObj == '':
            return
        appPosition=appObj.getAppPosition()
        if appPosition == '':
            return
        del self.appDictonary[appID]
        id_1=self.findAppID(appPosition+1)
        if id_1 == '':
            return
        appObj=self.findAppObjectFromDic(id_1)
        if appObj == '':
            return
        self.updateAppPosition(appObj,appPosition)

    def findAppObjectFromDic(self,appID):
        return self.appDictonary.get(appID,'')
    
    def findAppID(self,appPosition_):
        return self.appPositionDic.get(appPosition_,'')
    def updateAppPosition(self,appObject,position_):
        """
        更新应用位置
        """
        orgPosition=appObject.getAppPosition()
        if orgPosition == position_:
            return True
        elif orgPosition < position_:
            for i in range(position_-orgPosition):
                appID=self.findAppID(orgPosition+i+1)
                if(appID==''):
                    return False
                appObj=self.findAppObjectFromDic(appID)
                if(appObj==''):
                    return False
                appObj.setAppPosition(appObj.getAppPosition()-1)
                self.updateAppDictionary(appObj)
            appObject.setAppPosition(position_)
            self.updateAppDictionary(appObject)
            return True
        else:
            for i in range(orgPosition-position_):
                appID=self.findAppID(orgPosition-i-1)
                if(appID==''):
                    return False
                appObj=self.findAppObjectFromDic(appID)
                if(appObj==''):
                    return False
                appObj.setAppPosition(appObj.getAppPosition()+1)
                self.updateAppDictionary(appObj)
            appObject.setAppPosition(position_)
            self.updateAppDictionary(appObject)
            return True
    
    def setLoginState(self,loginState_):
        self.loginState=loginState_
    def getLoginState(self):
        return self.loginState
    
    def setTimeout(self,timeout_):
        self.timeout=timeout_
    def getTimeout(self):
        return self.timeout

    def setHasActs(self,flag):
        self.hasActs=flag
    def getHasActs(self):
        return self.hasActs
    
    