# -*- coding: utf-8 -*-
"""
Function: set_ldap_state.py moudle. This moudle mainly involves the
Setting the LDAP Enabling Status function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2017-2020
"""
from scripts import common_function


def setldapstate_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    Setting the LDAP Enabling Status
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    """
    sub_parser = parser.add_parser('enableldap',
                                   help='''enable or disable ldap''')
    sub_parser.add_argument('-S', dest='LdapServiceEnabled', type=str,
                            required=True,
                            help='''set LDAP service enabled state''',
                            choices=['True', 'False'])
    parser_dict['enableldap'] = sub_parser

    return 'enableldap'


def setldapstate(client, args):
    """
    Function Description:Setting the LDAP Enabling Status
    Parameter:client refishClient: class object
    args object:CLI command
    """
    url = "/redfish/v1/AccountService/LdapService"

    resp = client.get_resource(url)

    if resp is None:
        return None

    if resp['status_code'] != common_function.REDFISH_STATUS_CODE_200:
        return None

    payload = init_payload(args)

    resp = client.set_resource(url, payload)
    if resp is None:
        return None
    if resp['status_code'] == common_function.REDFISH_STATUS_CODE_200:
        print('Success: successfully completed request')

    else:
        common_function.print_status_code(resp)

    return resp


def init_payload(args):
    """
    Function Description:init payload
    Parameter:args object:CLI command
    """
    payload = {}
    if args.LdapServiceEnabled is not None:
        if args.LdapServiceEnabled == 'False':
            payload['LdapServiceEnabled'] = False
        else:
            payload['LdapServiceEnabled'] = True
    return payload
