# -*- coding:utf-8 -*-
"""
Function: del_twofactor_cert.py moudle. This moudle mainly involves the
Deleting a Dual-Factor Certificate function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2017-2020
"""
from scripts.common_function import REDFISH_STATUS_CODE_404
from scripts.common_function import REDFISH_STATUS_CODE_200
from scripts import common_function


def deltwofactorcert_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    Deleting a Dual-Factor Certificate
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    """
    sub_parser = parser.add_parser('del2factorcert',
                                   help='''delete two factor certificate''')

    sub_parser.add_argument('-T', dest='Type',
                            required=True,
                            choices={'root', 'client'},
                            help='''the type of certificate''')

    sub_parser.add_argument('-I', dest='userID',
                            type=int,
                            required=True,
                            help='root certificate ID or '
                                 'client certificate user ID')

    parser_dict['del2factorcert'] = sub_parser

    return 'del2factorcert'


def _respparse(args, resp):
    """
    Function Description:parse the response code and body.
    Parameter:resp dict: redfish interface Value
    """
    if resp['status_code'] == REDFISH_STATUS_CODE_200:
        print('Success: successfully completed request\r')
    elif args.Type == 'client' \
            and resp['status_code'] == REDFISH_STATUS_CODE_404:
        print('Failure: the user ID does not exist\r')
    elif args.Type == 'root' \
            and resp['status_code'] == REDFISH_STATUS_CODE_404:
        print('Failure: the certificate ID does not exist\r')
    else:
        # The two-factor root certificate you want to delete does not exist
        # The two-factor client certificate you want to delete does not exist
        msgs = resp['message']['error']['@Message.ExtendedInfo']
        if "PropertyValueNotInList" in msgs[0].get('MessageId'):
            print("Failure: The two-factor root certificate "
                  "you want to delete does not exist")
        else:
            message = msgs[0].get('Message').lower()
            print('Failure: %s' % message[:-1])


def deltwofactorcert(client, args):
    """
    Function Description:Deleting a Dual-Factor Certificate
    Parameter:client refishClient: class object
    args object:CLI command
    """
    url_dict = {
        'root': "/redfish/v1/AccountService/oem/%s/Actions/AccountService."
                "DeleteRootCertificate" % common_function.COMMON_KEY,
        'client': "/redfish/v1/AccountService/Accounts/%s/"
                  "Oem/%s/Actions/Account.DeleteMutualAuthClientCert"
                  % (args.userID,
                     common_function.COMMON_KEY)}
    payload = {}
    if args.Type == 'root':
        payload = {"CertId": args.userID}
    resp = client.create_resource(url_dict.get(args.Type), payload)
    if resp is None:
        return None
    _respparse(args, resp)
    return resp
