# -*- coding:utf-8 -*-
"""
Function: set_cdev_channel.py moudle. This moudle mainly involves the
Setting the Enabling Status of the Character Device Channel for
Communication Between the uREST and iBMC function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2020-2021
"""
from scripts import common_function


def setcdevchannel_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    Setting the Enabling Status of the Character Device Channel for
    Communication Between the uREST and iBMC
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    """
    sub_parser = parser.add_parser('setcdevchannel',
                                   help='set the channel type or enable status'
                                        ' of the communication between'
                                        ' the uREST and iBMC')
    sub_parser.add_argument('-E', dest='enable',
                            type=str, required=False,
                            action=common_function.UniqueStore,
                            help="enable or disable the devirtualization"
                                 " character device channel",
                            choices=['True', 'False'])
    sub_parser.add_argument('-T', dest='service_type',
                            action=common_function.UniqueStore,
                            choices=[common_function.USB_DEVICE,
                                     common_function.CDEV_DEVICE],
                            required=False,
                            help="the BOB channel is the devirtualized"
                                 " character device channel or USB channel"
                                 " (enabled by default)")
    parser_dict['setcdevchannel'] = sub_parser

    return 'setcdevchannel'


def check_parameter(parser, args):
    """
    Function Description:check CLI command
    Parameter:args object:CLI command
    parser object:subcommand ArgumentParser object
    """
    if not args.service_type and not args.enable:
        parser.error('specify at least one of -E and -T.')


def setcdevchannel(client, args):
    """
    Function Description: Setting the Enabling Status of the Character Device
    Channel for Communication Between the uREST and iBMC
    Parameter:client refishClient:class object
    args object:CLI command
    """
    slotid = client.get_slotid()
    if slotid is None:
        return None

    url = "/redfish/v1/Managers/%s/SmsService" % slotid
    resp = client.get_resource(url)
    if resp is None:
        return None
    if resp['status_code'] != common_function.REDFISH_STATUS_CODE_200:
        common_function.get_error_info(resp)
        return resp

    payload = {}
    if args.service_type == common_function.USB_DEVICE:
        payload["ChannelType"] = common_function.BOB_CHANNEL_TYPE
    elif args.service_type == common_function.CDEV_DEVICE:
        payload["ChannelType"] = "%s-%s" % (common_function.PCIE_DEVICE,
                                            common_function.CDEV_DEVICE)

    if args.enable:
        payload["CdevChannelEnabled"] = (False if args.enable == 'False'
                                         else True)

    resp = client.set_resource(url, payload)
    if resp is None:
        return None
    if resp['status_code'] == common_function.REDFISH_STATUS_CODE_200:
        success_message = 'Success: successfully completed request.' \
                          ' When using in-band commands,' \
                          ' set the communication service between the uREST' \
                          ' and iBMC based on the enabling status of the' \
                          ' character devices channel.'
        common_function.print_result(resp, success_message=success_message)
    else:
        common_function.print_error_message(resp)
    return resp
