# -*- coding: utf-8 -*-
# 创建动作
# 作者: 陈磊
# 时间: 2019-10-31
from WorkUtils.UtilsRequest import UtilsRequest
from WorkUtils.UtilsLog import UtilsLog


class ActionCreate(object):
    def __init__(self, _host, esc_period=None, name=None, eventsource=None, def_shortdata=None, def_longdata=None):
        """
        :param _host: 域名
        :param esc_period:
        :param name:
        :param eventsource:
        :param def_shortdata:
        :param def_longdata:
        :return:
        """
        self.log = UtilsLog()
        self.log.info("调用创建动作")
        self.log.info(self.__class__)

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

        self.esc_period = esc_period
        self.eventsource = eventsource
        self.name = name
        self.def_shortdata = def_shortdata
        self.def_longdata = def_longdata

        self.filter = {}
        self.filter_conditions = []

        self.operations = []
        self.operations_opconditions = []
        self.operations_opmessage_grp = []
        self.operations_opmessage = []
        self.operations_opcommand = []

        self.recovery_operations = []
        self.recovery_operations_opmessage = []

        self.acknowledge_operations = []
        self.acknowledge_operations_opmessage = []

        self.api = UtilsRequest()

    def filter_conditions_add(self, conditiontype=None, operator=None, value=None):
        self.filter_conditions.append({
            "conditiontype": conditiontype,
            "operator": operator,
            "value": value
        })

    def operations_opconditions_add(self, conditiontype=None, operator=None, value=None):
        self.operations_opconditions.append({
            "conditiontype": conditiontype,
            "operator": operator,
            "value": value
        })

    def operations_opmessage_grp_add(self, opcommand_grpid=None, operationid=None, groupid=None):
        self.operations_opmessage_grp.append({
            "opcommand_grpid": opcommand_grpid,
            "operationid": operationid,
            "groupid": groupid
        })

    def operations_opmessage_add(self, operationid=None, default_msg=None, mediatypeid=None, message=None, subject=None):
        self.operations_opmessage.append({
            "operationid": operationid,
            "default_msg": default_msg,
            "mediatypeid": mediatypeid,
            "message": message,
            "subject": subject
        })

    def get_response(self):
        self._json = {
            "esc_period": self.esc_period,
            "eventsource": self.eventsource,
            "name": self.name,
            "def_shortdata": self.def_shortdata,
            "def_longdata": self.def_longdata,
        }
        self.response = self.api.post(url=self._url, json=self._json)
