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

FORMAT = '%-20s: %s'


def getnetport_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    Querying iBMC Network Port Information
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    """
    sub_parser = parser.add_parser('getnetport',
                                   help='get iBMC management '
                                        'network port information')
    parser_dict['getnetport'] = sub_parser

    return 'getnetport'


def get_port_info(members_uri, client):
    """
    Function Description:Query specific network port information.
    Parameter:client refishClient:class object
    slotid str:manager id
    """
    resp_net = client.get_resource(members_uri)
    if resp_net is None:
        return None
    if resp_net['status_code'] != REDFISH_STATUS_CODE_200:
        if resp_net['status_code'] == REDFISH_STATUS_CODE_404:
            print('Failure: resource was not found')
        return resp_net

    if resp_net["resource"].get("Oem", None) is None:
        print('no data available for the resource')
        return resp_net

    port_message = common_function.get_vendor_value(resp_net)
    print(FORMAT %
          ("NetworkPortMode", port_message.get("NetworkPortMode")))
    print_net_port(port_message)
    print_allow_values(port_message)
    print_adapt_port(port_message)
    return resp_net


def print_adapt_port(port_message):
    """
    Function Description:Print the result of the
    AdaptivePort request.
    Parameter:port_message dict: redfish interface value
    """
    print("\n[AdaptivePort]")
    idx = 0
    # AdaptivePort is the [] type.
    ad_port = port_message.get("AdaptivePort", "[]")
    len_port = len(ad_port)
    if len_port > 0:
        print("-" * 40)
        while idx < len(ad_port):
            for key in ad_port[idx]:
                print(FORMAT % (key, ad_port[idx][key]))
            idx += 1
            print("-" * 40)


def print_allow_values(port_message):
    """
    Function Description:Print the result of the
    ManagementNetworkPort@AllowableValues request.
    Parameter:port_message dict: redfish interface value
    """
    print("\n[ManagementNetworkPort@AllowableValues]")
    idx = 0
    # @Redfish.AllowableValues is the [] type.
    man_port_all = port_message. \
        get("ManagementNetworkPort@Redfish.AllowableValues", "[]")
    len_man = len(man_port_all)
    if len_man > 0:
        print("-" * 40)
        while idx < len(man_port_all):
            for key in man_port_all[idx]:
                print(FORMAT % (key, man_port_all[idx][key]))
            idx += 1
            print("-" * 40)


def print_net_port(port_message):
    """
    Function Description:Print the result of the ManagementNetworkPort request.
    Parameter:port_message dict: redfish interface value
    """
    print("\n[ManagementNetworkPort]")
    port_messageinfo = port_message.get("ManagementNetworkPort", None)
    if port_messageinfo is not None:
        for key in port_messageinfo:
            print(FORMAT % (key, port_messageinfo[key]))


def get_port_collection(client, slotid):
    """
    Function Description:Query network port collection information.
    Parameter:client refishClient:class object
    slotid str:manager id
    """
    url = "/redfish/v1/managers/%s/EthernetInterfaces" % slotid
    resp_net = client.get_resource(url)
    if resp_net is None:
        return None
    if resp_net['status_code'] != REDFISH_STATUS_CODE_200:
        if resp_net['status_code'] == REDFISH_STATUS_CODE_404:
            print('Failure: resource was not found')
        return resp_net

    members_count = resp_net['resource']["Members@odata.count"]
    if members_count == 0:
        print("no data available for the resource")
        return resp_net
    # Query specific network port information.
    members_uri = resp_net['resource']['Members'][0]["@odata.id"]
    resp_net = get_port_info(members_uri, client)
    return resp_net


def getnetport(client, _):
    """
    Function Description:Querying iBMC Network Port Information
    Parameter:client refishClient:class object
    """
    slotid = client.get_slotid()
    if slotid is None:
        return None
    # Collection information query
    ret = get_port_collection(client, slotid)

    return ret
