# -*- utf-8 -*-
"""
Function: get_security_service.py moudle. This moudle mainly
 involves the querying 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 = 'getsecurityservice'
url = "/redfish/v1/Managers/{}/SecurityService"
title = False
error_message = "failed to obtain the information."


def get_security_service_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    querying Security Service Resource Collection Information server.
    Parameter:parser object: action object
              parser_dict dict: dictionary format of subcommand and
               ArgumentParser
    """
    sub_parser = parser.add_parser(sub_command,
                                   help="querying security service resource"
                                        " collection information")
    parser_dict[sub_command] = sub_parser
    return sub_command


def print_chain(chain_value):
    """
    Function Description:print hain
    Parameter:chain_value dict: redfish value
    """
    for chain_key, key_value in chain_value.items():
        if key_value and isinstance(key_value, dict):
            print("\n%s:" % chain_key)
            for server_key, server_value in key_value.items():
                print(common_function.PRO_FORMAT % (server_key,
                                                    server_value))
        else:
            print(common_function.PRO_FORMAT % (chain_key,
                                                key_value))


def print_value(resp):
    """
    Function Description:print value
    Parameter:resp dict: redfish value
    """

    for key, value in resp['resource'].items():
        if key not in ["Id",
                       "Name",
                       "SessionTokenLength",
                       "SecurityControlVersion",
                       "MasterKeyUpdateInterval",
                       "HttpsTransferCertVerification",
                       "SOLAutoOSLockEnabled",
                       "SOLAutoOSLockKey",
                       "RemoteHttpsServerCertChainInfo",
                       "RemoteHttpsServerRootCA"]:
            continue

        if value and isinstance(value, list):
            print("\n[%s]" % key)
            print('-' * 50)
            for chain_value in value:
                print_chain(chain_value)
                print('-' * 50)

        else:
            print(common_function.PRO_FORMAT % (key, value))


def get_security_service(client, _):
    """
    Function Description:get security service
    Parameter:client refishClient: class object
    """
    slot_id = client.get_slotid()
    if slot_id is None:
        sys.exit(common_function.UREST_STATUS_CODE_127)

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

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

    if 'resource' not in resp:
        raise common_function.CustomError(error_message)

    print_value(resp)
    return resp
