# -*- coding:utf-8 -*-
"""
Function: get_sys_health.py moudle. This moudle mainly involves the
 querying the System Health Status function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2020-2021
"""


def getsyshealth_init(parser, parser_list):
    """
    #==========================================================================
    #   @Method:  Register functions to register methods.
     used for obtaining the system health status.
    #   @Param:   parser        major command argparser
                  parser_list   save subcommand parser list
    #   @Return:
    #   @author:
    #==========================================================================
    """
    sub_parser = parser.add_parser('getsyshealth',
                                   help='''get system health state''')
    parser_list['getsyshealth'] = sub_parser

    return 'getsyshealth'


def getsyshealth(client, _):
    """
    #==========================================================================
    #   @Method:  Obtain the system health status.
    #   @Param:   client   RedfishClient object
                  parser   subcommand argparser.
                   Export error messages when parameters are incorrect.
                  args     parameter list
    #   @Return:
    #   @author:
    #==========================================================================
    """
    slotid = client.get_slotid()
    if slotid is None:
        return None
    url = "/redfish/v1/systems/%s" % 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:
        # Obtain the value of the Health attribute.
        print('Health : %s' % resp['resource'].get('Status', {}).get('Health', ''))

    return resp
