# -*- coding:utf-8 -*-
"""
Function: restore_storage_controller.py moudle. This moudle mainly involves the
Restoring Default Controller Settings function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2020-2021
"""
from scripts.common_function import TIMEOUT_THIRTY
from scripts import common_function


def restorestoragecontroller_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    restoring Default Controller Settings.
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    Modify: 2019.4.28 The help information is optimized.
    Return Value: subcommand
    """
    sub_parser = parser.add_parser('restoreraid',
                                   help='''restore RAID controller''')
    sub_parser.add_argument('-I', dest='controllerid',
                            type=int, required=True,
                            help='''RAID controller ID''')
    parser_dict['restoreraid'] = sub_parser

    return 'restoreraid'


def restorestoragecontroller(client, args):
    """
    Function Description:restoring Default Controller Settings
    Parameter:client refishClient: class object
    args object:CLI command
    Modify: 2019.1.17 Default timeout parameter normalization
    Return Value: resp dict:result of the redfish interface
    """
    slotid = client.get_slotid()
    if slotid is None:
        return None

    # Determine whether a storage system exists.
    systems = "/redfish/v1/Systems/" + slotid
    storage = "/Storages/RAIDStorage" + str(args.controllerid)
    resp = client.check_storages(systems, storage, '-I')
    if resp is None:
        return None
    if resp['status_code'] != 200:
        return resp

    payload = {}
    act = "/Actions/Oem/%s/Storage.RestoreStorageController" \
          "DefaultSettings" % common_function.COMMON_KEY
    url = systems + storage + act
    resp = client.create_resource(url, payload, timeout=TIMEOUT_THIRTY)
    if resp is None:
        return resp
    if resp['status_code'] == 200:
        print('Success: successfully completed request')
    elif resp['status_code'] == 400:
        str1 = "Failure: failed to operate storage controller "
        str2 = "because the current status does not support it"
        print(str1 + str2)
    return resp
