# -*- coding:utf-8 -*-
"""
Function: get_fru_info.py moudle. This moudle mainly involves the
Get fru info function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2022-2022
"""


INVALID_VALUE = None


def getfruinfo_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    Get fru info
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    """
    sub_parser = parser.add_parser('getfruinfo',
                                   help='''get fru info''')
    parser_dict['getfruinfo'] = sub_parser
    return 'getfruinfo'


def getfruinfo(client, _):
    """
    Function Description:Get fru info
    Parameter:client refishClient: class object
    """
    slotid = client.get_slotid()
    if slotid is None:
        return INVALID_VALUE
    url = "/redfish/v1/Chassis/%s/Boards" % slotid
    board_resp = get_resp(client, url)
    main_board_urls = board_resp.get("resource", {}).get("Members", {})
    for main_board_url in main_board_urls:
        print("FruName:             MainBoardInfo")
        main_board_resp = get_resp(client, main_board_url.get("@odata.id", {}))
        print_resource(main_board_resp)
    back_board_urls = board_resp.get("resource", {}).get("Oem", {}).get("xFusion", {}).get("Boards", {})
    for back_board_key in back_board_urls.keys():
        print("FruName:             %s" % back_board_key)
        back_board_links = back_board_urls[back_board_key].get("Links")
        for back_board_link in back_board_links:
            back_board_url = back_board_link.get("@odata.id", {})
            back_board_resp = get_resp(client, back_board_url)
            print_resource(back_board_resp)
    return board_resp


def print_resource(resp):
    """
    Function Description:Displaying the returned BIOS data.
    Parameter:resp: response
    args object:CLI command
    Modify: 2022.09.15 Modify the prompt when the -A parameter does not exist
    """
    print("Name:                %s" % resp.get("resource", {}).get("Name", {}))
    print("Description:         %s" % resp.get("resource", {}).get("Description", {}))
    print("CardNo:              %s" % resp.get("resource", {}).get("CardNo", {}))
    print("State:               %s" % resp.get("resource", {}).get("Status", {}).get("State", {}))
    print("Severity:            %s" % resp.get("resource", {}).get("Status", {}).get("Severity", {}))
    print("Health:              %s" % resp.get("resource", {}).get("Status", {}).get("Health", {}))
    print("DeviceLocator:       %s" % resp.get("resource", {}).get("DeviceLocator", {}))
    print("DeviceType:          %s" % resp.get("resource", {}).get("DeviceType", {}))
    print("Location:            %s" % resp.get("resource", {}).get("Location", {}))
    print("Manufacturer:        %s" % resp.get("resource", {}).get("Manufacturer", {}))
    print("ProductName:         %s" % resp.get("resource", {}).get("ProductName", {}))
    print("SerialNumber:        %s" % resp.get("resource", {}).get("SerialNumber", {}))
    print("PartNumber:          %s" % resp.get("resource", {}).get("PartNumber", {}))
    print("AssetTag:            %s" % resp.get("resource", {}).get("AssetTag", {}))
    print("CPLDVersion:         %s" % resp.get("resource", {}).get("CPLDVersion", {}))
    print("PCBVersion:          %s" % resp.get("resource", {}).get("PCBVersion", {}))
    print("BoardName:           %s" % resp.get("resource", {}).get("BoardName", {}))
    print("BoardId:             %s" % resp.get("resource", {}).get("BoardId", {}))
    print("ManufactureDate:     %s" % resp.get("resource", {}).get("ManufactureDate", {}))
    print("AssociatedResource:  %s" % resp.get("resource", {}).get("AssociatedResource", {}))
    print("PositionId:          %s" % resp.get("resource", {}).get("PositionId", {}))
    print("PowerWatts:          %s" % resp.get("resource", {}).get("PowerWatts", {}))
    print("SupportedRAIDLevels: %s" % resp.get("resource", {}).get("SupportedRAIDLevels", {}))
    print("PchModel:            %s" % resp.get("resource", {}).get("PchModel", {}))
    print("LinkWidthAbility:    %s" % resp.get("resource", {}).get("LinkWidthAbility", {}))
    print("LinkSpeedAbility:    %s" % resp.get("resource", {}).get("LinkSpeedAbility", {}))
    print("LinkWidth:           %s" % resp.get("resource", {}).get("LinkWidth", {}))
    print("LinkSpeed:           %s" % resp.get("resource", {}).get("LinkSpeed", {}))
    print("-" * 70)


def get_resp(client, url):
    """
    Function Description:Querying fru Information
    Parameter:client refishClient: class object
    """
    if len(url) == 0:
        return {}
    slotid = client.get_slotid()
    if slotid is None:
        return INVALID_VALUE
    resp = client.get_resource(url)
    if resp is None:
        return INVALID_VALUE
    if "Oem" in resp.get("resource", {}) and "Huawei" in resp.get("resource", {}).get("Oem", {}):
        # 此处用来兼容seed
        resp["resource"]["Oem"]["xFusion"] = resp.get("resource", {}).get("Oem", {}).get("Huawei", {})
    if resp.get("status_code", {}) == 404:
        print('Failure: resource was not found')
    if resp.get("status_code", {}) != 200:
        print('Failure: get NetworkAdapters error')
        return resp
    return resp

