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


def setpasswordpolicy_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    Setting the iBMC Password Complexity
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    """
    sub_parser = parser.add_parser('setpasswordpolicy',
                                   help='''set iBMC password complexity''')
    sub_parser.add_argument('-PC', dest='pcenabled',
                            type=str, required=True,
                            help='''password complexicy check change enabled''',
                            choices=['True', 'False'])

    parser_dict['setpasswordpolicy'] = sub_parser

    return 'setpasswordpolicy'


def setpasswordpolicy(client, args):
    """
    Function Description:Setting the iBMC Password Complexity
    Parameter:client refishClient: class object
    args object:CLI command
    """
    payload = init_payload(args)
    url = "/redfish/v1/AccountService"
    resp = client.get_resource(url)
    if resp.get('status_code') != common_function.REDFISH_STATUS_CODE_200:
        return None
    resp = client.set_resource(url, payload)
    if resp is None:
        return None
    print_setpassword_result(resp)
    return resp


def init_payload(args):
    """
    Function Description:init request parameters
    Parameter:args object:CLI command
    Return Value: payload dict
    """
    payload_inner_dic = {}
    if args.pcenabled is not None:
        payload_inner_dic['PasswordComplexityCheckEnabled'] \
            = common_function.stringtobool(args.pcenabled)
    payload = {"Oem": {common_function.COMMON_KEY: payload_inner_dic}}
    return payload


def print_setpassword_result(resp):
    """
    Function Description:print result.
    Parameter:resp dict: redfish interface Value
    """
    if resp['status_code'] == common_function.REDFISH_STATUS_CODE_200:
        print('Success: successfully completed request')
    else:
        error_code = "0"
        if resp['status_code']:
            error_code = resp['status_code']
        print('Failure: error code ' + error_code)
