# -*- coding:utf-8 -*-
"""
Function: get_password_policy.py moudle. This moudle mainly involves the
querying the iBMC Password Complexity function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2018-2020
"""
from scripts.common_function import REDFISH_STATUS_CODE_200
from scripts import common_function


def getpasswordpolicy_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    Querying the iBMC Password Complexity
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    """
    sub_parser = parser.add_parser('getpasswordpolicy',
                                   help='get iBMC password '
                                        'complexity check information')

    parser_dict['getpasswordpolicy'] = sub_parser

    return 'getpasswordpolicy'


def getpasswordpolicy(client, _):
    """
    Function Description:Querying the iBMC Password Complexity
    Parameter:client refishClient: class object
    """
    url = "/redfish/v1/AccountService"
    resp = client.get_resource(url)
    if resp is None:
        return None
    print_result(resp)
    return resp


def print_result(resp):
    """
    Function Description:print result.
    Parameter:resp dict: redfish interface Value
    """
    mformat = '%-*s:%s'
    length = 26
    if resp['status_code'] == REDFISH_STATUS_CODE_200:
        vendor_dict = common_function.get_vendor_value(resp)
        complexity = vendor_dict['PasswordComplexityCheckEnabled']
        print(mformat % (length, 'PasswordComplexityCheckEnabled', complexity))
    else:
        error_code = resp['status_code'] if resp['status_code'] else "0"
        print('Failure: error code ' + error_code)
