# -*- coding: utf-8 -*-
# 创建告警规则
# 作者: 陈磊
# 时间: 2019-11-28
from WorkUtils.UtilsRequest import UtilsRequest
from WorkUtils.UtilsLog import UtilsLog
from WorkApi.ApiBase import ApiBase, GetBase


class RuleCreate(object):
    def __init__(self, _host):
        """
        :param _host: 域名
        :return:
        """
        self.log = UtilsLog()
        self.log.info("调用创建告警规则")
        self.log.info(self.__class__)

        self._host = _host
        self._headers = {}
        self._path = "/rule/create"
        self._url = self._host + self._path
        self._params = {}
        self._json = {}
        self._data = {}
        self.response = ""

        self.token = None
        self.name = None
        self.severity = None
        self.url = None
        self.description = None
        self.status = None
        self.manualClose = None
        self.enableTimeDefault = None
        self.period = None
        self.periodType = None
        self.triggersList = None
        self.triggersList_hostid = None
        self.triggersList_hostName = None
        self.triggersList_itemid = None
        self.triggersList_itemName = None
        self.triggersList_function = None
        self.triggersList_parameters = None
        self.triggersList_parameters_value = None
        self.triggersList_parameters_unit = None
        self.triggersList_operator = None
        self.triggersList_constant = None
        self.triggersList_logicalSymbol = None
        self.triggersList_onlineFlag = None

        self.api = UtilsRequest()

    def get_response(self):
        base = ApiBase()
        base.dict_add_key(_key="token", value=self.token)
        self._headers = base._json

        if self.triggersList_parameters_value is not None:
            self.triggersList_parameters = []
            for x, y in enumerate(self.triggersList_parameters_value):
                base = ApiBase()
                base.dict_add_key(_key="value", value=self.triggersList_parameters_value, num=x)
                base.dict_add_key(_key="unit", value=self.triggersList_parameters_unit, num=x)
                self.triggersList_parameters.append(base._json)
        else:
            pass

        if self.triggersList_hostid is not None:
            self.triggersList = []
            for x, y in enumerate(self.triggersList_hostid):
                base = ApiBase()
                base.dict_add_key(_key="hostid", value=self.triggersList_hostid, num=x)
                base.dict_add_key(_key="hostName", value=self.triggersList_hostName, num=x)
                base.dict_add_key(_key="itemid", value=self.triggersList_itemid, num=x)
                base.dict_add_key(_key="itemName", value=self.triggersList_itemName, num=x)
                base.dict_add_key(_key="function", value=self.triggersList_function, num=x)
                base.dict_add_key(_key="parameters", value=self.triggersList_parameters)
                base.dict_add_key(_key="operator", value=self.triggersList_operator, num=x)
                base.dict_add_key(_key="constant", value=self.triggersList_constant, num=x)
                base.dict_add_key(_key="logicalSymbol", value=self.triggersList_logicalSymbol, num=x)
                base.dict_add_key(_key="onlineFlag", value=self.triggersList_onlineFlag, num=x)
                self.triggersList.append(base._json)
        else:
            pass

        base = ApiBase()
        base.dict_add_key(_key="name", value=self.name)
        base.dict_add_key(_key="severity", value=self.severity)
        base.dict_add_key(_key="url", value=self.url)
        base.dict_add_key(_key="description", value=self.description)
        base.dict_add_key(_key="status", value=self.status)
        base.dict_add_key(_key="manualClose", value=self.manualClose)
        base.dict_add_key(_key="enableTimeDefault", value=self.enableTimeDefault)
        base.dict_add_key(_key="period", value=self.period)
        base.dict_add_key(_key="periodType", value=self.periodType)
        base.dict_add_key(_key="triggersList", value=self.triggersList)
        base.dict_add_key(_key="period", value=self.period)
        base.dict_add_key(_key="severity", value=self.severity)

        self._json = base._json
        self.response = self.api.post(url=self._url, headers=self._headers, json=self._json)
