# -*- coding:utf-8 -*-
"""
Function: get_loginrule.py moudle. This moudle mainly involves the
Querying Login Rule 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 getloginrule_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    Querying Login Rule Information
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    """
    sub_parser = parser.add_parser('getloginrule',
                                   help='''get login rule''')
    parser_dict['getloginrule'] = sub_parser

    return 'getloginrule'


def getloginrule(client, _):
    """
    Function Description:Querying Login Rule Information
    Parameter:client refishClient:class object
    """
    slotid = client.get_slotid()
    if slotid is None:
        return None
    # Query LoginRule information.
    resp = get_virtualmedia_info(client, slotid)

    return resp


def get_virtualmedia_info(client, slotid):
    """
    Function Description:LoginRule query information
    Parameter:client refishClient:class object
    slotid str:manager id
    """
    url = "/redfish/v1/Managers/%s" % slotid
    resp = client.get_resource(url)
    if resp is None:
        return None
    if resp['status_code'] == REDFISH_STATUS_CODE_200:
        vendor_dict = common_function.get_vendor_value(resp)
        message = vendor_dict.get("LoginRule", "")
        idx = 0
        print("-" * 40)
        while idx < len(message):
            print(FORMAT % ("MemberId", message[idx].get("MemberId", '')))
            print(FORMAT % ("RuleEnabled", message[idx].get("RuleEnabled", '')))
            print(FORMAT % ("StartTime", message[idx].get("StartTime", '')))
            print(FORMAT % ("EndTime", message[idx].get("EndTime", '')))
            print(FORMAT % ("Mac", message[idx].get("Mac", '')))
            print(FORMAT % ("IP", message[idx].get("IP", '')))
            idx += 1
            print("-" * 40)
    elif resp['status_code'] == REDFISH_STATUS_CODE_404:
        print('Failure: resource was not found')
    return resp
