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

FORMAT = '%-20s: %s'


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

    return 'getdns'


def get_dns_info(members_uri, client):
    """
    Function Description:Query the DNS information.
    Parameter:members_uri str: members uri
    client refishClient: class object
    """
    get_resp = client.get_resource(members_uri)
    if get_resp is None:
        return None
    if get_resp['status_code'] != REDFISH_STATUS_CODE_200:
        if get_resp['status_code'] == REDFISH_STATUS_CODE_404:
            print('Failure: resource was not found')
        return get_resp
    name_service = get_resp["resource"].get("NameServers", {})
    vendor_dict = common_function.get_vendor_value(get_resp)
    mode = vendor_dict.get("DNSAddressOrigin", {})
    # Display information.
    print("")
    print(FORMAT % ("HostName", get_resp["resource"].get("HostName", {})))
    print(FORMAT % ("FQDN", get_resp["resource"].get("FQDN", {})))
    print(FORMAT % ("DNSAddressOrigin", mode))
    # nameservice The length must be 2.
    if len(name_service) >= 2:
        print(FORMAT % ("PreferredServer", name_service[0]))
        print(FORMAT % ("AlternateServer", name_service[1]))

    return get_resp


def get_port_collection(client, slotid):
    """
    Function Description:Query the collection information.
    Parameter:client refishClient:class object
    slotid str:manager id
    """
    url = "/redfish/v1/managers/%s/EthernetInterfaces" % slotid
    get_resp = client.get_resource(url)
    if get_resp is None:
        return None
    if get_resp['status_code'] != REDFISH_STATUS_CODE_200:
        if get_resp['status_code'] == REDFISH_STATUS_CODE_404:
            print('Failure: resource was not found')
        return get_resp
    members_count = get_resp['resource']["Members@odata.count"]
    if members_count == 0:
        print("no data available for the resource")
    # Query the DNS information.
    else:
        members_uri = get_resp['resource']['Members'][0]["@odata.id"]
        get_resp = get_dns_info(members_uri, client)
    return get_resp


def getdns(client, _):
    """
    Function Description:Querying DNS Information
    Parameter:client refishClient:class object
    """
    # Query the slot information.
    slotid = client.get_slotid()
    if slotid is None:
        return None
    # Query the collection information.
    ret = get_port_collection(client, slotid)
    return ret
