# -*- coding:utf-8 -*-
"""
Function: get_health_events.py moudle. This moudle mainly involves the
 querying Health Events function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2020-2021
"""
from scripts import common_function


def gethealthevents_init(parser, parser_list):
    """
    #==========================================================================
    #   @Method:  gethealthevents_init
    #   @Param:
    #   @Return:
    #   @author:
    #==========================================================================
    """
    sub_parser = parser.add_parser('gethealthevents',
                                   help='''get health events''')
    sub_parser.add_argument('-S', dest='simplify', required=False,
                            help='Simplified version of alarm information')
    parser_list['gethealthevents'] = sub_parser

    return 'gethealthevents'


def print_health_events(resp_inner_dic_len, resp_inner_dic, args):
    """
    #==========================================================================
    #   @Method:  print_health_events
    #   @Param:
    #   @Return:
    #   @author:
    #==========================================================================
    """
    health_event_count = 0
    health_event_id = 1
    print('-' * 50)
    while health_event_count < resp_inner_dic_len:
        health_event = resp_inner_dic['HealthEvent'][health_event_count]
        severity = health_event.get('Severity', {})
        level = health_event.get('Level', {})
        event_id = health_event.get('EventId', {})
        event_subject = health_event.get('EventSubject', {})
        message = health_event.get('Message', {})
        if args.simplify and args.simplify == "True":
            print("[%s][%s][ID:%s][Locate:%s]%s" % (health_event.get('Created', {}),
                                                    level, event_id, event_subject, message))
        else:
            print("%-14s%-2s%-20s" % ('Id', ':', health_event_id))
            print("%-14s%-2s%-20s" % ('Severity', ':', severity))
            print("%-14s%-2s%-20s" % ('Level', ':', level))
            print("%-14s%-2s%-20s" % ('EventTimestamp', ':', health_event.get('Created', {})))
            print("%-14s%-2s%-20s" % ('EntryType', ':', health_event.get('EntryType', {})))
            print("%-14s%-2s%-20s" % ('EventType', ':', health_event.get('EventType', {})))
            print("%-14s%-2s%-20s" % ('MessageId', ':', health_event.get('MessageId', {})))
            print("%-14s%-2s%-20s" % ('EventId', ':', event_id))
            print("%-14s%-2s%-20s" % ('EventSubject', ':', event_subject))
            print("%-14s%-2s%-20s" % ('Message', ':', message))
            print('-' * 50)
        health_event_count += 1
        health_event_id += 1


def gethealthevents(client, args):
    """
    #==========================================================================
    #   @Method:  gethealthevents
    #   @Param:
    #   @Return:
    #   @author:
    #==========================================================================
    """
    slotid = client.get_slotid()
    if slotid is None:
        return None
    url = "/redfish/v1/systems/%s/logservices/log1" % slotid
    resp = client.get_resource(url)

    if resp is None:
        return None
    if resp['status_code'] == 404:
        print('Failure: resource was not found')
    elif resp['status_code'] == 200:
        print_result(resp, args)

    return resp


def print_result(resp, args):
    """
    功能描述：打印 请求结果
    参数：resp (dict):redfish 接口返回值
    返回值：None
    异常描述：None
    修改：None
    """
    resp_inner_dic = common_function.get_vendor_value(resp)
    resp_inner_dic_len = len(resp_inner_dic['HealthEvent'])
    if resp_inner_dic_len:
        print_health_events(resp_inner_dic_len, resp_inner_dic, args)
    else:
        print('System in health state')
