# -*- utf-8 -*-
"""
Function: get_security_service.py moudle. This moudle mainly
 involves the modifying Security Service Resource Collection Information
  function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2021
"""
import sys

from scripts import common_function

sub_command = 'setsecurityservice'
security_service_url = "/redfish/v1/Managers/{}/SecurityService"
error_info = "failed to set the verification of the remote HTTPS" \
             " server certificate."


def set_security_service_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    importing the certificate of the remote HTTPS transport server.
    Parameter:parser object: action object
              parser_dict dict: dictionary format of subcommand and
               ArgumentParser
    """
    sub_parser = parser.add_parser(sub_command, help="modifying security"
                                                     " service resource"
                                                     " collection information")
    sub_parser.add_argument("-HTCV",
                            dest="verification",
                            required=True,
                            help="enable or disable the certificate"
                                 " verification for the remote HTTPS server.",
                            choices=["True", "False"])
    parser_dict[sub_command] = sub_parser
    return sub_command


def set_security_service(client, args):
    """
    Function Description:set security service
    Parameter:client refishClient: class object
              args object:CLI command
    """
    slot_id = client.get_slotid()
    if slot_id is None:
        sys.exit(common_function.UREST_STATUS_CODE_127)

    url = security_service_url.format(slot_id)
    resp = client.get_resource(url)
    if not resp:
        raise common_function.CustomError(
            common_function.error_info)

    if resp.get('status_code') != common_function.REDFISH_STATUS_CODE_200:
        raise common_function.CustomError(resp)

    verification_flag = True if args.verification == "True" else False
    set_resp = client.set_resource(url,
                                   {"HttpsTransferCert"
                                    "Verification": verification_flag})
    if not set_resp:
        raise common_function.CustomError(error_info)

    if set_resp.get('status_code') != common_function.REDFISH_STATUS_CODE_200:
        raise common_function.CustomError(set_resp)

    print('Success: successfully completed request')
    return set_resp
