# -*- coding:utf-8 -*-
"""
Function: get_usbstick.py moudle. This moudle mainly involves the
 querying a Virtual Smart Provisioning USB Flash Drive function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2020-2021
"""
FORMAT = '%-20s: %s'


def getusbstick_init(parser, parser_list):
    """
    #===========================================================
    # @Method:Register commands.
    # @Param: parser, parser_list
    # @Return:
    # @date: 2017.8.7
    #===========================================================
    """
    sub_parser = parser.add_parser('getvusb',
                                   help='get virtual USB '
                                        'flash drive information')
    parser_list['getvusb'] = sub_parser
    return 'getvusb'


def getusbstick(client, _):
    """
    #===========================================================
    # @Method: Process functions.
    # @Param:client,
    # @Return:
    # @date: 2017.8.1
    #===========================================================
    """
    slotid = client.get_slotid()
    if slotid is None:
        return None
    # Display information.
    resp = get_virtualmedia_info(client, slotid)

    return resp


def get_virtualmedia_info(client, slotid):
    """
    #===========================================================
    # @Method: Query information.
    # @Param:client, parser, args
    # @Return:
    # @date: 2017.8.1
    #===========================================================
    """
    url = "/redfish/v1/Managers/%s/VirtualMedia/USBStick" % slotid
    resp = client.get_resource(url)
    if resp is None:
        return None
    if resp['status_code'] == 200:
        mediatypes = resp["resource"].get("MediaTypes", "")
        len_me = len(mediatypes)
        if len_me != 0:
            print(FORMAT % ("MediaTypes", mediatypes[0]))
        else:
            print(FORMAT % ("MediaTypes", ""))
        print(FORMAT % ("Image", resp["resource"].get("Image", None)))
        print(FORMAT % ("ImageName", resp["resource"].get("ImageName", None)))
        print(FORMAT % ("ConnectedVia",
                        resp["resource"].get("ConnectedVia", None)))
        print(FORMAT % ("Inserted", resp["resource"].get("Inserted", None)))
    elif resp['status_code'] == 404:
        print('Failure: resource was not found')
    return resp
