# -*- coding:utf-8 -*-
"""
Function: get_net_card_info.py moudle. This moudle mainly involves the
Get net card info function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2022-2022
"""
from scripts import common_function


INVALID_VALUE = None


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


def getnetcardinfo(client, _):
    """
    Function Description:Deleting a sp upgrade file
    Parameter:client refishClient: class object
    """
    slotid = client.get_slotid()
    if slotid is None:
        return INVALID_VALUE
    resp = get_resp(client, "/redfish/v1/Chassis/%s/NetworkAdapters" % slotid)
    net_card_list = resp.get('resource', {}).get('Oem', {}).get("xFusion", {}).get("NetCard", {})
    if len(net_card_list) == 0:
        common_function.print_info('net card not found')
        return INVALID_VALUE
    for net_card in net_card_list:
        # 循环打印每个网卡信息
        net_card_url = net_card.get('@odata.id', {})
        card_resp = get_resp(client, net_card_url)
        vender_id = get_resource_oem_xfusion(card_resp).get('VenderID', {})
        device_id = get_resource_oem_xfusion(card_resp).get('DeviceID', {})
        vid = "--"
        if vender_id is not None and device_id is not None and len(vender_id) > 3 and len(device_id) > 3:
            vid = vender_id[2:] + ":" + device_id[2:]
        card_detail = card_resp.get('resource', {}).get('Oem', {}).get("xFusion", {})
        port_url_list = card_resp.get('resource', {}).get('Controllers', {})[0].get('Links', {}).get('NetworkPorts', {})
        print_common_info(card_detail, vid, card_resp)
        if port_url_list is not None and len(port_url_list) != 0:
            for port_url in port_url_list:
                # 循环打印每个port
                port_resp = get_resp(client, port_url.get("@odata.id", {}))
                related_port_url = get_resource_oem_xfusion(port_resp).get('RelatedPort', {}).get('@odata.id', {})
                related_port_resp = get_resp(client, related_port_url)
                network_port_url = get_resource_oem_xfusion(related_port_resp).get('RelatedPort', {}).get('@odata.id',
                                                                                                          {})
                network_port_resp = get_resp(client, network_port_url)
                print_port_info(port_resp, related_port_resp, network_port_resp)
        common_function.print_info("-" * 60)


def print_common_info(card_detail, vid, card_resp):
    common_function.print_info("NIC Name：                %s" % card_detail.get("Name", {}))
    common_function.print_info("VID：                     %s" % vid)
    common_function.print_info("Model：                   %s" % card_detail.get("CardModel", {}))
    common_function.print_info("Chip Manufacturer：       %s" % card_resp.get('resource', {}).get("Manufacturer", {}))
    common_function.print_info("Manufacturer：            %s" % card_detail.get("CardManufacturer", {}))
    common_function.print_info("Chip Model：              %s" % card_resp.get('resource', {}).get("Model", {}))
    common_function.print_info("PCB Version：             %s" % card_detail.get("PCBVersion", {}))
    common_function.print_info("Resource Attribution：    %s" % card_detail.get("AssociatedResource", {}))
    common_function.print_info("Bus Information：         %s" % card_detail.get("RootBDF", {}))
    common_function.print_info("Driver Name：             %s" % card_detail.get("DriverName", {}))
    common_function.print_info("Driver Version：          %s" % card_detail.get("DriverVersion", {}))
    common_function.print_info("Slot Number：             %s" % card_detail.get("SlotNumber", {}))
    common_function.print_info("Position：                %s" % card_detail.get("Position", {}))
    common_function.print_info("NetworkTechnology：       %s" % card_detail.get("NetworkTechnology", {}))
    common_function.print_info("BoardId：                 %s" % card_detail.get("BoardId", {}))
    common_function.print_info("BoardIdHex：              %s" % card_detail.get("BoardIdHex", {}))
    common_function.print_info("AssociatedResource：      %s" % card_detail.get("AssociatedResource", {}))
    common_function.print_info("Port Properties:")


def print_port_info(port_resp, related_port_resp, network_port_resp):
    common_function.print_info("-" * 40)
    common_function.print_info("PortName：                port%s" % port_resp.get("resource", {}).get("Name", {}))
    common_function.print_info("LinkStatus：              %s" % port_resp.get("resource", {}).get("LinkStatus", {}))
    common_function.print_info("PortTechnology：          %s" % get_resource_oem_xfusion(port_resp).get("PortType", {}))
    common_function.print_info("MediaType：               %s" % get_resource_oem_xfusion(port_resp)
                               .get("PortTechnology", {}))
    common_function.print_info("MACAddress：              %s" % related_port_resp.get('resource', {})
                               .get('MACAddress', {}))
    common_function.print_info("Speed：                   %s" % related_port_resp.get('resource', {})
                               .get('SpeedMbps', {}))
    common_function.print_info("IPAddresses：             %s" % get_ip(related_port_resp, 'Address'))
    common_function.print_info("Gateway：                 %s" % get_ip(related_port_resp, 'Gateway'))
    common_function.print_info("InterfaceType：           %s" % get_resource_oem_xfusion(related_port_resp)
                               .get('InterfaceType', {}))
    common_function.print_info("BDF：                     %s" % get_resource_oem_xfusion(related_port_resp)
                               .get('BDF', {}))
    common_function.print_info("FwVersion：               %s" % get_resource_oem_xfusion(network_port_resp).get(
        'FirmwarePackageVersion', {}))


def get_ip(related_port_resp, param):
    ip_address = related_port_resp.get('resource', {}).get('IPv4Addresses', {})
    if ip_address is not None and len(ip_address) > 0:
        return ip_address[0].get(param, {})
    return INVALID_VALUE


def get_resource_oem_xfusion(resp):
    return resp.get('resource', {}).get('Oem', {}).get('xFusion', {})


def get_resp(client, url):
    """
    Function Description:Querying PCIe 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:
        common_function.print_info('Failure: resource was not found')
    if resp.get("status_code", {}) != 200:
        common_function.print_info('Failure: get NetworkAdapters error')
        return resp
    return resp

