# -*- coding:utf-8 -*-
"""
Function: del_logicaldrive.py moudle. This moudle mainly involves the
 deleting a Logical Disk function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2020-2021
"""
import sys
from scripts.common_function import UREST_STATUS_CODE_2


def dellogicaldrive_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    deleting a Logical Disk.
    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('delvdisk',
                                   help='''delete virtual disk''')

    sub_parser.add_argument('-CI', dest='controllerid',
                            type=int, required=True, help='''controller ID''')

    sub_parser.add_argument('-LI', dest='logicaldriveid',
                            type=int, required=True,
                            help='''virtual disk ID''')

    parser_dict['delvdisk'] = sub_parser

    return 'delvdisk'


def get_taskinfo(client, task_uri):
    """
    #=====================================================================
    #   @Method:  Delete logical disks and query the task progress.
    #   @Param:   client, RedfishClient object
    #             task_url:Task task_url:Task resource uri
    #   @Return:
    #   @author:
    #=====================================================================
    """
    get_resp = client.get_resource(task_uri)
    if get_resp is None:
        return None

    # Query the progress.
    task_resp = client.print_task_prog(get_resp)
    if task_resp is None:
        return None

    get_resp = client.get_resource(task_uri)
    if get_resp is None:
        return None

    if get_resp['status_code'] != 200:
        print('failure: ' + get_resp['resource']['Messages']['Message'])
        return get_resp

    if task_resp == 'Exception':
        print('failure: ' + get_resp['resource']['Messages']['Message'])
        sys.exit(144)
    return get_resp


def dellogicaldrive(client, args):
    """
    #=====================================================================
    #   @Method:  Logical disk deletion subcommand processing functions
    #   @Param:   client, RedfishClient object
                  parser, subcommand argparser.
                          Export error messages when parameters are incorrect.
                  args, parameter list
    #   @Return:
    #   @author:
    #=====================================================================
    """
    slotid = client.get_slotid()
    if slotid is None:
        return None

    systems = "/redfish/v1/Systems/" + slotid
    url = systems + "/Storages"
    resp = client.get_resource(url)
    if resp is None:
        return None

    if resp['status_code'] != 200:
        if resp['status_code'] == 404:
            print('Failure: resource was not found')
        return resp
    storage = "/Storages/RAIDStorage" + str(args.controllerid)
    # Check the controller URL validity.
    url = systems + storage
    resp = client.get_resource(url)
    if resp is None:
        return None

    if resp['status_code'] == 404:
        print('Failure: the value of -CI parameter is invalid')
        sys.exit(UREST_STATUS_CODE_2)

    elif resp['status_code'] == 200:
        logical = "/Volumes/LogicalDrive" + str(args.logicaldriveid)
        url = systems + storage + logical

        resp = client.delete_resource(url)
        if resp is None:
            return None

        if resp['status_code'] == 202:
            task_uri = resp['message']['@odata.id']
            resp = get_taskinfo(client, task_uri)

        elif resp['status_code'] == 404:
            print('Failure: the value of -LI parameter is invalid')
            sys.exit(UREST_STATUS_CODE_2)
    return resp
