# -*- coding: utf-8 -*-
"""
Function: del_ssh_public_key.py moudle. This moudle mainly involves the
Deleting an SSH Public Key function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2017-2020
"""
from scripts import common_function


def delsshpublickey_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    Deleting an SSH Public Key
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    """
    sub_parser = parser.add_parser('delsshkey',
                                   help='''delete SSH public key''')

    sub_parser.add_argument('-I', dest='userID',
                            type=str, required=True,
                            help='''user ID''')

    parser_dict['delsshkey'] = sub_parser

    return 'delsshkey'


def _respparse(resp):
    """
    Function Description:Parse the response code and body.
    Parameter:resp dict: redfish Interface Value
    """
    if resp['status_code'] == common_function.REDFISH_STATUS_CODE_200:
        print('Success: successfully completed request\r')
    elif resp['status_code'] == common_function.REDFISH_STATUS_CODE_404:
        print('Failure: The user ID does not exist\r')
    else:
        # The SSH public key does not exist.
        msgs = resp['message']['error']['@Message.ExtendedInfo']
        message = msgs[0].get('Message').lower()
        print('Failure: %s' % message[:-1])


def delsshpublickey(client, args):
    """
    Function Description:Deleting an SSH Public Key
    Parameter:client refishClient: class object
    args object:CLI command
    """
    url = "/redfish/v1/AccountService/Accounts/%s/Oem/%s/" \
          "Actions/Account.DeleteSSHPublicKey" % (args.userID,
                                                  common_function.COMMON_KEY)

    payload = {}

    resp = client.create_resource(url, payload)

    if resp is None:
        return None

    _respparse(resp)
    return resp
