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


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

    return 'gettemp'


def gettemperature(client, _):
    """
    Function Description:Querying Temperature Information
    Parameter:client refishClient: class object
    """
    slotid = client.get_slotid()
    if slotid is None:
        return None

    url = "/redfish/v1/Chassis/%s/Thermal" % slotid
    resp = client.get_resource(url)
    if resp is None:
        return None
    if resp['status_code'] == 200:
        temperatures = resp['resource'].get('Temperatures', {})
        if not temperatures:
            print('no data available for the resource')
            return resp
        print_result(temperatures)
    elif resp['status_code'] == 404:
        print('Failure: resource was not found')
    return resp


def print_result(temperatures):
    """
    Function Description:print result
    Parameter:temperatures (list): temperature informations
    """
    temperatures_len = len(temperatures)
    print("-" * 50)
    idx = 0
    while idx < temperatures_len:
        temperature = temperatures[idx]
        print("%-30s: %s" % ('MemberId', temperature.get('MemberId', '')))
        print("%-30s: %s" % ('Name', temperature.get('Name', '')))
        for key in temperature:
            if key in ["@odata.id", "MemberId", "Status", "Name"]:
                continue
            print("%-30s: %s" % (key, temperature.get(key, '')))
        print('\n[Status]')
        print("%-30s: %s" % ('Health',
                             temperature.get('Status', {}).get('Health', '')))
        print("%-30s: %s" % ('State',
                             temperature.get('Status', {}).get('State', '')))
        print("-" * 50)
        idx += 1
