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

FORMAT = "%-20s: %s"


def getpowersupply_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    Querying PSU Information
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    """
    sub_parser = parser.add_parser('getpsu',
                                   help='''get power supply unit information''')
    parser_dict['getpsu'] = sub_parser

    return 'getpsu'


def getpowersupply(client, _):
    """
    Function Description:Querying PSU Information
    Parameter:client refishClient: class object
    """
    slotid_power = client.get_slotid()
    if slotid_power is None:
        return None

    url = "/redfish/v1/Chassis/%s/Power" % slotid_power
    resp = client.get_resource(url)
    if resp is None:
        return None
    if resp['status_code'] == 200:
        powersupplies = resp['resource'].get('PowerSupplies', {})
        powersupplies_len = len(powersupplies)
        if powersupplies_len == 0:
            print('no data available for the resource')
            return resp

        show_console(powersupplies, powersupplies_len, resp)
    elif resp['status_code'] == 404:
        print('Failure: resource was not found')
    return resp


def show_console(powersupplies, powersupplies_len, resp):
    """
    Function Description:Displaying the found content on the client
    Parameter:powersupplies list: powersupplies info
    powersupplies_len int:powersupplies info's length
    resp (dict):redfish interface value
    """
    print("-" * 40)
    idx = 0
    while idx < powersupplies_len:
        key = powersupplies[idx]
        oem_key = key['Oem'][common_function.COMMON_KEY]
        print(FORMAT % ('Name', key.get('Name', None)))
        print(FORMAT % ("Position", oem_key.get("Position", None)))
        for oem in oem_key:
            if oem == "Position":
                continue
            print(FORMAT % (oem, oem_key[oem]))
        print(FORMAT % ('Manufacturer', key.get('Manufacturer', None)))
        print(FORMAT % ('PartNumber', key.get('PartNumber', None)))
        print(FORMAT %
              ('PowerSupplyType', key.get('PowerSupplyType', None)))
        print(FORMAT %
              ('LineInputVoltage', key.get('LineInputVoltage', None)))
        print(FORMAT %
              ('PowerCapacityWatts', key.get('PowerCapacityWatts', None)))
        print(FORMAT % ('Model', key.get('Model', None)))
        print(FORMAT %
              ('FirmwareVersion', key.get('FirmwareVersion', None)))
        print(FORMAT % ('SerialNumber', key.get('SerialNumber', None)))
        redundancy_key = powersupplies[idx].get('Redundancy', {})
        print('\n[Redundancy]')
        mode = redundancy_key[0]["@odata.id"].split('/')
        if len(mode) > 0:
            str_id = int(mode[len(mode) - 1])
            redundancy = resp['resource'].get('Redundancy', [])
            if len(redundancy) > str_id:
                print(FORMAT % ('Mode', redundancy[str_id].get('Mode', {})))
        print('\n[Status]')
        print(FORMAT % ('Health', key.get('Status', {}).get('Health', None)))
        print(FORMAT % ('State', key.get('Status', {}).get('State', None)))

        print("-" * 40)
        idx += 1
