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


def rollbackbmc_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    Switching the iBMC Images
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    """
    sub_parser = parser.add_parser('rollbackbmc',
                                   help='''roll back iBMC''')
    parser_dict['rollbackbmc'] = sub_parser

    return 'rollbackbmc'


def rollbackbmc(client, _):
    """
    Function Description:Switching the iBMC Images
    Parameter:client refishClient:class object
    """
    slotid = client.get_slotid()
    if slotid is None:
        return None

    url = "/redfish/v1/Managers/%s/" \
          "Actions/Oem/%s/Manager.RollBack" % (slotid,
                                               common_function.COMMON_KEY)

    payload = {}

    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
