# -*- coding:utf-8 -*-
"""
Function: get_snmp_trap.py moudle. This moudle mainly involves the
get snmp trap function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2023-2023
"""
from scripts import common_function

INVALID_VALUE = None
FORMAT = '%-*s:%-*s'
LEN = 32


def getsnmptrap_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    get snmp trap
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    """
    sub_parser = parser.add_parser('getsnmptrap',
                                   help='''get snmp trap''')
    sub_parser.add_argument('-I', dest='id',
                            type=str, required=False,
                            help='''SNMP Trap Server ID''',
                            choices=['0', '1', '2', '3'])
    parser_dict['getsnmptrap'] = sub_parser
    return 'getsnmptrap'


def print_trap(snmp_trap_notification):
    common_function.print_info('-' * 50)
    common_function.print_info('[SNMP Trap]')
    common_function.print_info('-' * 50)
    common_function.print_info(FORMAT % (LEN, 'ServiceEnabled', LEN, snmp_trap_notification.get('ServiceEnabled', '')))
    common_function.print_info(FORMAT % (LEN, 'TrapVersion', LEN, snmp_trap_notification.get('TrapVersion', '')))
    common_function.print_info(FORMAT % (LEN, 'TrapMode', LEN, snmp_trap_notification.get('TrapMode', '')))
    common_function.print_info(FORMAT % (LEN, 'TrapServerIdentity', LEN,
                                         snmp_trap_notification.get('TrapServerIdentity', '')))
    common_function.print_info(FORMAT % (LEN, 'CommunityName', LEN, "*" * 6))
    common_function.print_info(FORMAT % (LEN, 'AlarmSeverity', LEN, snmp_trap_notification.get('AlarmSeverity', '')))
    common_function.print_info('-' * 50)


def print_trap_server_detail(trap_server):
    common_function.print_info(FORMAT % (LEN, 'MemberId', LEN, trap_server.get("MemberId", "")))
    common_function.print_info(FORMAT % (LEN, 'TrapServerAddress', LEN, trap_server.get("TrapServerAddress", "")))
    common_function.print_info(FORMAT % (LEN, 'TrapServerPort', LEN, trap_server.get("TrapServerPort", "")))
    common_function.print_info(FORMAT % (LEN, 'BobEnabled', LEN, trap_server.get("BobEnabled", "")))
    common_function.print_info(FORMAT % (LEN, 'Enabled', LEN, trap_server.get("Enabled", "")))
    common_function.print_info('-' * 50)


def print_trap_server(trap_server_list):
    for trap_server in trap_server_list:
        print_trap_server_detail(trap_server)


def print_one_trap_server(trap_server_list, member_id):
    for trap_server in trap_server_list:
        if member_id and member_id is trap_server.get("MemberId", ""):
            print_trap_server_detail(trap_server)


def getsnmptrap(client, args):
    """
    Function Description:get snmp trap
    Parameter:client refishClient: class object
    args object:CLI command
    """
    slot_id = client.get_slotid()
    if slot_id is None:
        return
    resp = client.get_resource("/redfish/v1/Managers/%s/SnmpService" % slot_id)
    if resp is None:
        return
    if resp['status_code'] != 200:
        if resp['status_code'] == 404:
            common_function.print_info('Failure: resource was not found')
        return
    snmp_trap_notification = resp["resource"].get('SnmpTrapNotification', {})
    trap_server_list = snmp_trap_notification.get('TrapServer', '')
    if not args.id:
        print_trap(snmp_trap_notification)
        common_function.print_info('[Trap Server]')
        common_function.print_info('-' * 50)
        print_trap_server(trap_server_list)
    else:
        print_one_trap_server(trap_server_list, args.id)
