# -*- coding:utf-8 -*-
"""
Function: get_voltage.py moudle. This moudle mainly involves the
Querying Voltage Information function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2017-2020
"""
DIVIDING_LINE = "-" * 50


def getvoltage_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    Querying Voltage Information
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    """
    sub_parser = parser.add_parser('getvolt',
                                   help='''get voltage information''')
    parser_dict['getvolt'] = sub_parser

    return 'getvolt'


def getvoltage(client, _):
    """
    Function Description: Querying Voltage Information
    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

    if resp['status_code'] == 200:
        voltages = resp['resource'].get('Voltages', {})
        if not voltages:
            print('no data available for the resource')
            return resp
        print_result(voltages)
    elif resp['status_code'] == 404:
        print('Failure: resource was not found')

    return resp


def print_result(voltages):
    """
    Function Description:print result
    Parameter:voltages (list): voltage informations
    """
    voltages_len = len(voltages)
    print(DIVIDING_LINE)
    idx = 0
    while idx < voltages_len:
        voltage = voltages[idx]
        print("%-30s: %s" % ("MemberId", voltage.get("MemberId", '')))
        print("%-30s: %s" % ("Name", voltage.get("Name", '')))
        for key in voltage:
            if key in ["@odata.id", "MemberId", "Status", "Name"]:
                continue
            print("%-30s: %s" % (key, voltage.get(key, '')))
        print('\n[Status]')
        for status_key in voltage.get("Status", ''):
            print("%-30s: %s" % (status_key,
                                 voltage.get("Status", {}).get(status_key, '')))
        print(DIVIDING_LINE)
        idx += 1
