# -*- coding:utf-8 -*-
"""
Function: operate_vmm.py moudle. This moudle mainly involves the
 performing Operations on Virtual Media function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2020-2021
"""
import sys
from scripts import common_function

HELP = "path of the virtual media image file that can be stored only on" \
       " remote file servers. The path format is server" \
       " IP address/directory/file name for interactive command, " \
       "protocol://username:password@IP[:port]/directory/filename for non-interactive command"


def operatevmm_init(parser, parser_list):
    """
    #==========================================================================
    # @Method: Register commands.
    # @Param: parser, parser_list
    # @Return:
    # @author:
    #==========================================================================
    """
    sub_parser = parser.add_parser('connectvmm',
                                   help='connect or disconnect virtual media')
    sub_parser.add_argument('-T', dest='Type',
                            type=str, required=True,
                            choices=['Disconnect', 'Connect'],
                            help='''operation type''')

    image_uri = {common_function.REQUIRED_FLAG: False,
                 common_function.HELP_INFO: HELP}
    args_pro = {common_function.REQUIRED_FLAG: False,
                "protocol": ['nfs', 'cifs']}
    args_acc = {common_function.REQUIRED_FLAG: False}
    common_function.unpack_remote_server_path(sub_parser, image_uri,
                                              args_pro, args_acc)

    parser_list['connectvmm'] = sub_parser

    return 'connectvmm'


def check_parameter(parser, args):
    """
    Function Description:check CLI command
    Parameter:args object:CLI command
    parser object:subcommand ArgumentParser object
    """
    if args.Type == "Connect":
        if args.imageuri is None:
            parser.error('-i are required.')
        if args.imageuri is not None and common_function.verify_remote_server_info(args.imageuri) \
                and (args.protocol is not None or args.accessuser is not None):
            print("Failure: -i contains all of the information of the remote server."
                  " you do not need -PRO -U any more.")
            sys.exit(common_function.UREST_STATUS_CODE_2)

    if args.Type == "Disconnect":
        if args.imageuri is not None \
                or args.protocol is not None \
                or args.accessuser is not None:
            parser.error('-i, -PRO, or -U is redundant.')


def is_interactive(args):
    """
    Function Description:check CLI command
    Parameter:args object:CLI command
    parser object:subcommand ArgumentParser object
    """
    if args.protocol is not None or args.accessuser is not None:
        return True
    return False


def operatevmm(client, args):
    """
    #==========================================================================
    # @Method: Command processing function
    # @Param:client, parser, args
    # @Return:
    # @author:
    # @date: 2017.8.1 11:09
    #==========================================================================
    """
    # Analyze parameters.
    payload = {'VmmControlType': args.Type}
    if args.Type == "Connect":
        payload['Image'] = common_function.splice_remote_server_path(args)

    if payload is None:
        return None

    resp_vmm = get_response(payload, client)

    return resp_vmm


def get_response(payload, client):
    """
    #==========================================================================
    # @Method: Query response information.
    # @Param:payload,client,slotid
    # @Return:
    # @date: 2017.8.1 11:09
    #==========================================================================
    """
    slotid = client.get_slotid()
    if slotid is None:
        return None
    url_cd, resp = common_function.get_cd_info(client, slotid)
    if url_cd == "":
        return resp

    url = url_cd + "/Oem/%s/Actions/VirtualMedia.Vmm" \
                   "Control" % common_function.COMMON_KEY
    # Perform operations on virtual media.
    resp_vmm = client.create_resource(url, payload)
    if resp_vmm is None:
        return None

    ret = resp_vmm.get("status_code", "")
    if ret == 200:
        print('Success: successfully completed request')
    elif ret == 400:
        print('Failure: operation failed')
    elif ret == 404:
        print('Failure: resource was not found')
    elif ret == 202:
        # Query the progress.
        resp_vmm = query_progress(client, resp_vmm)

    return resp_vmm


def query_progress(client, resp_vmm):
    """
    #==========================================================================
    # @Method: Query the progress.
    # @Param:payload,client,slotid
    # @date: 2017.8.1 11:09
    # @Return:
    #==========================================================================
    """
    task_resp = client.print_task_prog(resp_vmm, 10)
    if task_resp == "Exception":
        uri = resp_vmm["resource"].get("@odata.id", "")
        get_rsp = client.get_resource(uri)
        if get_rsp is None:
            return None

        if get_rsp.get("status_code", "") == 200:
            message = get_rsp.get("resource", "")
            err_message = message.get("Messages", "").get("Message", "")
            err_message = ("%s%s" % (err_message[0].lower(),
                                     err_message[1:len(err_message) - 1]))
            print('Failure: %s' % err_message)
            sys.exit(144)
        elif get_rsp.get("status_code", "") == 404:
            print('Failure: resource was not found')
            return get_rsp
        else:
            return get_rsp

    return task_resp
