# -*- coding:utf-8 -*-
"""
Function: add_event_subscription.py moudle. This moudle mainly involves the
Creating an Event Subscription function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2017-2020
"""
import sys

from scripts import common_function


def addeventsubscription_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    Creating an Event Subscription
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    """
    sub_parser = parser.add_parser('addeventsubs',
                                   help='''create event subscription''')

    sub_parser.add_argument('-B', dest='requestbodyfile',
                            required=True,
                            help='local file used to save the request body in '
                                 'JSON format')
    parser_dict['addeventsubs'] = sub_parser
    return 'addeventsubs'


def addeventsubscription(client, args):
    """
    Function Description:Creating an Event Subscription
    Parameter:client refishClient: class object
    args object:CLI command
    """
    try:
        payload = common_function.payload_file(args.requestbodyfile,
                                               file_des='request body')
    except common_function.CustomError as e:
        print(e)
        sys.exit(common_function.UREST_STATUS_CODE_2)

    # Perform creation operations.
    url = '/redfish/v1/EventService/Subscriptions'
    resp = client.create_resource(url, payload)

    if resp is None:
        return None
    if resp['status_code'] == common_function.REDFISH_STATUS_CODE_201:
        print('Success: successfully completed request')
    elif resp['status_code'] == common_function.REDFISH_STATUS_CODE_400 \
            or resp['status_code'] == common_function.REDFISH_STATUS_CODE_409:
            common_function.get_error_info(resp)
    return resp
