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

MEMBER_NULL = "failed to query information about the specified" \
              " user resource of a server."


def getuser_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    get user information
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    """
    sub_parser = parser.add_parser('getuser',
                                   help='''get user information''')
    sub_parser.add_argument('-N', dest='name',
                            required=False,
                            help='''user name''')

    parser_dict['getuser'] = sub_parser

    return 'getuser'


def getuser(client, args):
    """
    Function Description:get user information
    Parameter:client refishClient: class object
    args object:CLI command
    """
    # url path storage directory
    url = "/redfish/v1/AccountService/Accounts"

    resp = client.get_resource(url)
    if not resp:
        raise common_function.CustomError(common_function.USERS_INFO_NULL)

    if resp.get('status_code') != common_function.REDFISH_STATUS_CODE_200:
        raise common_function.CustomError(resp)

    resp = getuser_info(client, resp, args)
    return resp


def getuser_info(client, resp, args):
    """
    Function Description:Displays user information.
    Parameter:client refishClient: class object
    resp dict:redfish interface value
    args object:CLI command
    """
    flag = False
    for user_id in resp['resource']['Members']:
        url = user_id['@odata.id']
        user_resp = client.get_resource(url)
        if not user_resp:
            raise common_function.CustomError(common_function.USER_NOT_EXITS)

        if user_resp['status_code'] != common_function.REDFISH_STATUS_CODE_200:
            raise common_function.CustomError(user_resp)

        if args.name is None or args.name == user_resp['resource'].get('UserName', ''):
            flag = True
            print_resource(user_resp)

    if not flag:
        print('Failure: the user does not exist')
    else:
        print('-' * 60)

    return resp


def print_resource(user_resp):
    """
    Function Description:print resource
    Parameter:info dict:redfish interface value
    """
    info = user_resp['resource']
    format_str = "%-28s%-2s%-s"
    print('-' * 60)
    print(format_str % ('UserId', ":", info.get('Id', '')))
    print(format_str % ('UserName', ":", info.get('UserName', '')))
    print(format_str % ('RoleId', ":", info.get('RoleId', '')))
    print(format_str % ('Locked', ":", info.get('Locked', '')))
    print(format_str % ('Enabled', ":", info.get('Enabled', '')))
    vendor_dict = common_function.get_vendor_value(user_resp)
    print(format_str % ('LoginInterface', ":",
                        ','.join(vendor_dict.get('LoginInterface', ''))))
    if 'AccountInsecurePromptEnabled' in vendor_dict:
        print(format_str %
              ('AccountInsecurePromptEnabled', ":",
               vendor_dict.get('AccountInsecurePromptEnabled', '')))

    print_vendor_infomation(info)


def print_vendor_infomation(info):
    """
    Function Description:print vendor infomation
    Parameter:user_resp dict: redfish result
    """
    RULE_KEY = "LoginRule"
    DATA_ID_KEY = '@odata.id'
    format_str = "%-28s%-2s%-s"
    keys = ("FirstLoginPolicy",
            "SnmpV3AuthProtocol",
            "SnmpV3PrivProtocol",
            "SNMPEncryptPwdInit",
            "Deleteable")
    oem_dict = info.get(common_function.OEM_KEY)
    if not oem_dict:
        return

    vendor_dict = oem_dict.get(common_function.COMMON_KEY)
    if not vendor_dict:
        return

    for key in keys:
        if key in vendor_dict:
            print(format_str % (key, ":", vendor_dict.get(key)))

    # 1.RULE_KEY:[] 2:RULE_KEY not exit
    if RULE_KEY not in vendor_dict:
        return

    login_value = vendor_dict.get(RULE_KEY)
    rule_ids_init = list(filter(lambda x: x.get(DATA_ID_KEY), login_value))
    rule_ids = [x.get(DATA_ID_KEY) for x in rule_ids_init]
    login_rules = ["Rule%s" % str(x).split('/')[-1] for x in rule_ids]
    print(format_str % (RULE_KEY, ":", login_rules))
