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

from PyQt4.QtCore import QObject
from storeinfoparser import StoreInfoParser
import json
class VMInfoManager(QObject):
    _instance = 0 
    def __init__(self):
        super(VMInfoManager,self).__init__()
#         self.originalVMInfoList = {}
        self.vmInfoList = {}
        lessonlist = StoreInfoParser.instance().getLessonList()
        offlessonlist = StoreInfoParser.instance().getOffLessonList()
        if not lessonlist or not offlessonlist:
            locallessonlist = []
            offlocallessonlist = []
        else:
            locallessonlist = json.loads(lessonlist)
            offlocallessonlist = json.loads(offlessonlist)
            
        self.vmLessonInfoList = locallessonlist
        self.vmOnlineLessonInfoList = offlocallessonlist
        self.cloudsServerState = False

        
    def setVMSInfo(self, infoList):
        self.originalVMInfoList = infoList
        self.vmInfoList = infoList

    def setLessonListInfo(self, lessonInfoList):
        self.vmLessonInfoList = lessonInfoList
        
    def setOnlineLessonListInfo(self, lessonInfoList):
        self.vmOnlineLessonInfoList = lessonInfoList
        
    def setCurCloudServerState(self, state):
        self.cloudsServerState = state
        
    def getCurCloudServerState(self):
        return self.cloudsServerState
        
    def getLessonListInfo(self):
        return self.vmLessonInfoList
    
    def getOnlineLessonListInfo(self):
        return self.vmOnlineLessonInfoList
    
            
    def getVMSInfo(self):
        return self.vmInfoList
    
    def getVMInfo(self, name):
        vmInfo = []
        for subVmInfo in self.vmInfoList:
            if subVmInfo and "name" in subVmInfo.keys():
                if subVmInfo["name"] == name:
                    vmInfo.append(subVmInfo)
                    return vmInfo
                
        return vmInfo
                
    @classmethod 
    def instance(cls):
        if (cls._instance == 0):
            cls._instance = VMInfoManager()
            
        return cls._instance