# -*- coding:utf-8 -*-
"""
Function: get_powersupply.py moudle. This moudle mainly involves the
Querying Power Capping function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2017-2020
"""
from scripts.common_function import REDFISH_STATUS_CODE_200
from scripts.common_function import REDFISH_STATUS_CODE_404


def getpowerlimit_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    Querying Power Capping
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    """
    sub_parser = parser.add_parser('getpowerlimit',
                                   help='''get power limit''')
    parser_dict['getpowerlimit'] = sub_parser

    return 'getpowerlimit'


def getpowerlimit(client, _):
    """
    Function Description:Querying Power Capping
    Parameter:client refishClient: class object
    """
    slotid = client.get_slotid()
    if slotid is None:
        return None

    url = "/redfish/v1/Chassis/%s/Power" % slotid
    resp = client.get_resource(url)
    if resp is None:
        return None

    print_reslut(resp)
    return resp


def print_reslut(resp):
    """
    Function Description:print result
    Parameter:resp (dict): redfish interface value
    """
    if resp['status_code'] == REDFISH_STATUS_CODE_200:
        power_limit = resp['resource'].get('PowerControl', {})[0].get('PowerLimit', {})
        if power_limit is None:
            print('no data available for the resource')
            return
        limit_inwatts = power_limit.get('LimitInWatts', '')
        if limit_inwatts is None:
            limit_inwatts = 'Disabled'
        limit_exception = power_limit.get('LimitException', '')

        print('LimitInWatts  : %s' % limit_inwatts)
        print('LimitException: %s' % limit_exception)
    elif resp['status_code'] == REDFISH_STATUS_CODE_404:
        print('Failure: resource was not found')
