# -*- coding:utf-8 -*-
"""
Function: reset_bmc.py moudle. This moudle mainly involves the
Restarting the iBMC function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2018-2020
"""
from scripts.common_function import REDFISH_STATUS_CODE_200


def resetbmc_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    Restarting the iBMC
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    """
    sub_parser = parser.add_parser('resetbmc',
                                   help='''reset iBMC''')
    sub_parser.add_argument('-R', dest='ResetType', required=True,
                            help='Restart operation', choices=['ForceRestart'])
    parser_dict['resetbmc'] = sub_parser

    return 'resetbmc'


def resetbmc(client, args):
    """
    Function Description:Restarting the 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/Actions/Manager.Reset" % slotid

    payload = {"ResetType": args.ResetType}

    resp = client.create_resource(url, payload)
    if resp is None:
        return None

    if resp['status_code'] == REDFISH_STATUS_CODE_200:
        print('Success: successfully completed request')
    else:
        error_code = "0"
        if resp['status_code']:
            error_code = resp['status_code']
        print('Failure: error code ' + error_code)
    return resp
