# -*- coding: utf-8 -*-
# 更新触发器
# 作者: 陈磊
# 时间: 2019-11-19
from WorkUtils.UtilsRequest import UtilsRequest
from WorkUtils.UtilsLog import UtilsLog
from WorkApi.ApiBase import ApiBase


class TriggerUpdate(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 = "/trigger/update"
        self._url = self._host + self._path
        self._params = {}
        self._json = {}
        self.local_json = {}
        self.response = ""

        self.triggerid = None
        self.description = None
        self.expression = None
        self.comments = None
        self.error = None
        self.flags = None
        self.lastchange = None
        self.priority = None
        self.state = None
        self.status = None
        self.templateid = None
        self.type = None
        self.url = None
        self.recovery_mode = None
        self.recovery_expression = None
        self.correlation_mode = None
        self.correlation_tag = None
        self.manual_close = None

        self.dependencies = None
        self.tags = None

        self.dependencies_triggerid = None
        self.tags_tag = None
        self.tags_value = None

        self.api = UtilsRequest()

    def get_response(self):
        if self.tags_tag is not None:
            self.tags = []
            for x, y in enumerate(self.tags_tag):
                base = ApiBase()
                base.dict_add_key(_key="tag", value=self.tags_tag[x])
                base.dict_add_key(_key="value", value=self.tags_value[x])
                self.tags.append(base._json)
        else:
            pass

        if self.dependencies_triggerid is not None:
            self.dependencies = []
            for x, y in enumerate(self.dependencies_triggerid):
                base = ApiBase()
                base.dict_add_key(_key="triggerid", value=self.dependencies_triggerid, num=x)
                self.tags.append(base._json)
        else:
            pass

        base = ApiBase()
        base.dict_add_key(_key="triggerid", value=self.triggerid)
        base.dict_add_key(_key="description", value=self.description)
        base.dict_add_key(_key="expression", value=self.expression)
        base.dict_add_key(_key="comments", value=self.comments)
        base.dict_add_key(_key="priority", value=self.priority)
        base.dict_add_key(_key="status", value=self.status)
        base.dict_add_key(_key="templateid", value=self.templateid)
        base.dict_add_key(_key="type", value=self.type)
        base.dict_add_key(_key="url", value=self.url)
        base.dict_add_key(_key="recovery_mode", value=self.recovery_mode)
        base.dict_add_key(_key="recovery_expression", value=self.recovery_expression)
        base.dict_add_key(_key="correlation_mode", value=self.correlation_mode)
        base.dict_add_key(_key="correlation_tag", value=self.correlation_tag)
        base.dict_add_key(_key="manual_close", value=self.manual_close)
        base.dict_add_key(_key="dependencies", value=self.dependencies)
        base.dict_add_key(_key="tags", value=self.tags)

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