# -*- coding:utf-8 -*-
"""
Function: get_sensor_list.py moudle. This moudle mainly involves the
 querying sensor list function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2022-2022
"""
import sys

from scripts import common_function


INVALID_VALUE = None


def getsensorlist_init(parser, parser_list):
    """
    #=====================================================================
    #   @Method:  get sensor list
    #   @Param:   parser, major command argparser
    #             parser_list, save subcommand parser list
    #   @Return:
    #   @author:
    #=====================================================================
    """
    sub_parser = parser.add_parser('getsensorlist',
                                   help='''get sensor list information''')
    parser_list['getsensorlist'] = sub_parser
    return 'getsensorlist'


def getsensorlist(client, _):
    """
    #=====================================================================
    #   @Method: get sensor list functions
    #   @Param:  client, RedfishClient object
                 args, parameter list
    #   @Return:
    #   @author:
    #   @date:
    #=====================================================================
    """
    slotid = client.get_slotid()
    if slotid is None:
        return INVALID_VALUE

    url = "/redfish/v1/Chassis/%s/Power" % slotid
    resp = client.get_resource(url)
    if resp is None:
        return INVALID_VALUE
    if resp['status_code'] == 200:
        info = resp['resource'].get('Voltages', {})
        if info is None:
            common_function.print_info('no data available for the resource')
            return resp
        print_resource(info, client)
    elif resp['status_code'] == 404:
        common_function.print_info('Failure: resource was not found')
    else:
        common_function.print_status_code(resp)
    return resp


def print_resource(voltages, client):
    """
    Function Description:Displaying the returned BIOS data.
    Parameter:voltages dict: sensor list
    args object:CLI command
    Modify: 2022.09.06 Modify the prompt when the -A parameter does not exist
    """
    for voltage in voltages:
        if "Oem" in voltage.get("Status", {}) and "Huawei" in voltage.get("Status", {}).get("Oem", {}):
            # 此处用来兼容seed
            voltage["Status"]["Oem"]["xFusion"] = voltage.get("Status", {}).get("Oem", {}).get("Huawei", {})
        common_function.print_info("MemberId:                  %s" % voltage.get("MemberId", {}))
        common_function.print_info("Name:                      %s" % voltage.get("Name", {}))
        common_function.print_info("SensorNumber:              %s" % voltage.get("SensorNumber", {}))
        common_function.print_info("State:                     %s" % voltage.get("Status", {}).get("State", {}))
        common_function.print_info("Severity:                  %s" % voltage.get("Status", {}).get("Oem", {})
                                   .get("xFusion", {}).get("Severity", {}))
        common_function.print_info("Health:                    %s" % voltage.get("Status", {}).get("Health", {}))
        common_function.print_info("ReadingVolts:              %s" % voltage.get("ReadingVolts", {}))
        common_function.print_info("UpperThresholdNonCritical: %s" % voltage.get("UpperThresholdNonCritical", {}))
        common_function.print_info("UpperThresholdCritical:    %s" % voltage.get("UpperThresholdCritical", {}))
        common_function.print_info("UpperThresholdFatal:       %s" % voltage.get("UpperThresholdFatal", {}))
        common_function.print_info("LowerThresholdNonCritical: %s" % voltage.get("LowerThresholdNonCritical", {}))
        common_function.print_info("LowerThresholdCritical:    %s" % voltage.get("LowerThresholdCritical", {}))
        common_function.print_info("LowerThresholdFatal:       %s" % voltage.get("LowerThresholdFatal", {}))
        common_function.print_info("MinReadingRange:           %s" % voltage.get("MinReadingRange", {}))
        common_function.print_info("MaxReadingRange:           %s" % voltage.get("MaxReadingRange", {}))
        common_function.print_info("-" * 70)
