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

FORMAT = '%-20s: %s'


def getvmm_init(parser, parser_list):
    """
    #===========================================================
    # @Method:Register VMM commands.
    # @Param: parser, parser_list
    # @Return:
    # @date: 2017.8.7
    #===========================================================
    """
    sub_parser = parser.add_parser('getvmm',
                                   help='''get virtual media information''')
    parser_list['getvmm'] = sub_parser

    return 'getvmm'


def getvmm(client, _):
    """
    #===========================================================
    # @Method: VMM command processing function
    # @Param:client
    # @Return:
    # date:2017.08.29 11:54
    #===========================================================
    """
    slotid = client.get_slotid()
    if slotid is None:
        return None
    # Query VMM information.
    resp = get_virtualmedia_info(client, slotid)

    return resp


def get_virtualmedia_info(client, slotid):
    """
    #===========================================================
    # @Method: VMM query information
    # @Param:client, parser, args
    # @Return:
    # @date: 2017.8.1
    #===========================================================
    """
    # Query collections.
    url, resp = common_function.get_cd_info(client, slotid)
    if url == "":
        return resp
    resp = client.get_resource(url)
    if resp is None:
        return None
    if resp['status_code'] == 200:
        print_result(resp)
    elif resp['status_code'] == 404:
        print('Failure: resource was not found')
    return resp


def print_result(resp):
    """
    功能描述：打印 请求结果
    参数：resp (dict):redfish 接口返回值
    返回值：None
    异常描述：None
    修改：None
    """
    mediatypes = resp["resource"].get("MediaTypes", "")
    # At most one type is allowed.
    len_med = len(mediatypes)
    if len_med != 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)))
    vendor_dict = common_function.get_vendor_value(resp)
    print(FORMAT % ("EncryptionEnabled", vendor_dict.get("EncryptionEnabled", None)))
