# -*- coding: utf-8 -*-
# 测试用例
# 作者: 陈磊
# 时间: 2019-11-27
import allure

from WorkApi.API.Problem.problem_get import ProblemGet
from WorkApi.API.Trigger.trigger_create import TriggerCreate
from WorkUtils.UtilsResponse import UtilsResponse


class ApiBase(object):
    def __init__(self, host):
        self.host = host
        self.token = None

    @allure.step("断言返回结果：校验排序")
    def check_sortfield(self, response, possible):
        result = UtilsResponse().get_result(response=response)
        UtilsResponse().check_sort(_list=result, key="groupid", possible=possible)

    @allure.step("断言返回结果：校验返回数据的数量")
    def check_num(self, response, num):
        result = UtilsResponse().get_result(response=response)
        assert len(result) == num

    @allure.step("断言返回结果")
    def check_code(self, response, code):
        _code = UtilsResponse().get_code(response=response)
        assert _code == code

    @allure.step("断言返回错误信息")
    def check_msg(self, response, msg):
        _msg = UtilsResponse().get_msg(response=response)
        assert _msg == msg

    @allure.step("断言返回错误结果")
    def check_status(self, response, status):
        _status = UtilsResponse().get_status(response=response)
        assert _status == status

    @allure.step("调用接口：problem.get")
    def problem_get(self, eventids=None, groupids=None, hostids=None, objectids=None, applicationids=None,
                    source=None, _object=None, acknowledged=None, suppressed=None, severities=None, evaltype=None,
                    tags=None, recent=None, eventid_from=None, eventid_till=None, time_from=None,
                    time_till=None, selectAcknowledges=None, selectTags=None, selectSuppressionData=None,
                    countOutput=None, editable=None, excludeSearch=None,
                    _filter=None, limit=None, output=None, preservekeys=None, search=None, searchByAny=None,
                    searchWildcardsEnabled=None, sortfield=None, sortorder=None, startSearch=None):
        api = ProblemGet(_host=self.host)
        api.eventids = eventids
        api.groupids = groupids
        api.hostids = hostids
        api.objectids = objectids
        api.applicationids = applicationids
        api.source = source
        api.object = _object
        api.acknowledged = acknowledged
        api.suppressed = suppressed
        api.severities = severities
        api.evaltype = evaltype
        api.tags = tags
        api.recent = recent
        api.eventid_from = eventid_from
        api.eventid_till = eventid_till
        api.time_from = time_from
        api.time_till = time_till
        api.selectAcknowledges = selectAcknowledges
        api.selectTags = selectTags
        api.selectSuppressionData = selectSuppressionData
        api.countOutput = countOutput
        api.editable = editable
        api.excludeSearch = excludeSearch
        api.filter = _filter
        api.limit = limit
        api.output = output
        api.preservekeys = preservekeys
        api.search = search
        api.searchByAny = searchByAny
        api.searchWildcardsEnabled = searchWildcardsEnabled
        api.sortfield = sortfield
        api.sortorder = sortorder
        api.startSearch = startSearch
        api.get_response()
        return api.response

    @allure.step("调用接口：trigger.create")
    def trigger_create(self, description=None, expression=None, tags_tag=None, tags_value=None,
                       priority=None, _type=None, correlation_mode=None, status=None):
        api = TriggerCreate(_host=self.host)
        api.description = description
        api.expression = expression
        api.priority = priority
        api.type = _type
        api.correlation_mode = correlation_mode
        api.status = status
        api.tags_tag = tags_tag
        api.tags_value = tags_value
        api.get_response()
        return api.response