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

import os
import time
import requests
import json
import common
import globalfunc
from configFileParser import ConfigFileParser
from storeinfoparser import StoreInfoParser
from windowMonitor import WindowMonitor
from logrecord import LogRecord
from PyQt4.QtCore import QObject, QProcess


class Requests(QObject):
    _instance = 0
    def __init__(self):
        super(Requests,self).__init__()
        self.curVMInfo = []

    @classmethod
    def instance(cls):
        if (cls._instance == 0):
            cls._instance = Requests()
        return cls._instance

    def getRequestInfo(self, url):
        s = self.getSession()
        if not s:
            return None
        try:
            resp = s.get(url, verify=False)
            if resp.status_code == 200 and resp.ok:
                return resp.json()
            else:
                return None
        except requests.exceptions.ReadTimeout as e:
            LogRecord.instance().logger.info(u"request请求失败，%s" % e.message)
            return None
        except Exception as e:
            LogRecord.instance().logger.info(u"request请求失败，%s" % e.message)
        finally:
            s.close()
        return None
    
    def getHostRequestInfo(self, url):
        s = self.getSession()
        if not s:
            return None
        try:
            resp = s.get(url,timeout=5, verify=False)
            if resp.status_code == 200 and resp.ok:
                return resp.json()
        except requests.exceptions.ReadTimeout as e:
            LogRecord.instance().logger.info(u"request请求超时，%s" % e.message)
            return {"state":"enable"}
        except Exception as e:
            LogRecord.instance().logger.info(u"request请求失败，%s" % e.message)
        finally:
            s.close()

        return None

    def getLicenseRequestInfo(self, url):
        s = self.getSession()
        if not s:
            return None
        try:
            resp = s.get(url,verify=False)
            if resp.status_code == 200 and resp.ok:
                return resp.content
        except requests.exceptions.ConnectionError, e:
            LogRecord.instance().logger.info(u"request请求失败，%s" % e.message)
        finally:
            s.close()

        return None

    def getUsbStateRequestInfo(self, url):
        s = self.getSession()
        if not s:
            return None
        try:
            resp = s.get(url,verify=False)
            if resp.status_code == 200 and resp.ok:
                return resp.content
        except requests.exceptions.ConnectionError, e:
            LogRecord.instance().logger.info(u"request请求失败，%s" % e.message)
        finally:
            s.close()

        return None
    
    def getTerminalRequestInfo(self, url, info = None):
        s = self.getSession()
        if not s:
            return None
        try:
            resp = s.post(url,data=json.dumps(info),verify=False)
            if resp.status_code == 200 and resp.ok:
                return resp.content
        except requests.exceptions.ConnectionError, e:
            LogRecord.instance().logger.info(u"request请求失败，%s" % e.message)
        finally:
            s.close()

        return None

    def postRequestInfo(self, url, info=None, value=False):
        s = self.getSession()
        if not s:
            return False
        try:
            resp = s.post(url, data=json.dumps(info),verify=False)
            s.close()
            if resp.ok:
                if value:
                    return resp.json()
                else:
                    return []
        except Exception, e:
            LogRecord.instance().logger.info(u"request请求失败，%s" % e.message)
        finally:
            s.close()
        return False

    def getSession(self):
        s = requests.Session()
        s.headers.update(common.headers)
        s.auth = common.auth
        serverIp = StoreInfoParser.instance().getCloudsServerIP()
        if serverIp == None:
            LogRecord.instance().logger.info(u"没有设置云中心IP地址，无法连接云主机")
            return None
        return s

