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

FORMAT = '%-30s: %s'
SESSION_FORMAT = '%-20s: %s'


def getvnc_init(parser, parser_list):
    """
    #===========================================================
    # @Method:Register VNC commands.
    # @Param: parser, parser_list
    # @Return:
    # @date: 2017.8.7
    #===========================================================
    """
    sub_parser = parser.add_parser('getvnc',
                                   help='''get VNC information''')
    parser_list['getvnc'] = sub_parser

    return 'getvnc'


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

    return resp


def get_vncrule_session(rule, session, client):
    """
    #===========================================================
    # @Method: Query login rules and sessions
    # @Param:client, parser, args
    # @Return:
    # @date: 2017.8.1
    #===========================================================
    """
    idx = 0
    rule_info = ""
    while idx < len(rule):
        url = rule[idx].get("@odata.id", "")
        id_info = url[len(url) - 1:]
        rule_info += "Rule%s," % id_info
        idx += 1
    print(FORMAT % ("LoginRule", rule_info[:len(rule_info) - 1]))

    # Query sessions.
    idx = 0
    len_session = len(session)
    if len_session != 0:
        print("\n[Sessions]")
        print("-" * 40)
        while idx < len(session):
            url = session[idx].get("@odata.id", "")
            resp = client.get_resource(url)
            if resp is None:
                return None
            if resp['status_code'] == 200:
                # The array starts from 0.
                vnc_session = common_function.get_vendor_value(resp)
                for key in vnc_session:
                    print(SESSION_FORMAT % (key, vnc_session.get(key, None)))
            idx += 1
            print("-" * 40)
    return None


def get_virtualmedia_info(client, slotid):
    """
    #===========================================================
    # @Method: VNC query information
    # @Param:client, parser, args
    # @Return:
    # @date: 2017.8.1
    #===========================================================
    """
    url = "/redfish/v1/Managers/%s/VncService" % slotid
    resp = client.get_resource(url)
    if resp is None:
        return None
    if resp['status_code'] == 200:
        message = resp.get("resource", None)
        del message["@odata.context"]
        del message["@odata.id"]
        del message["@odata.type"]
        print("")
        for key in message:
            if key == "LoginRule":
                rule = message[key]
                continue
            if key == "Links":
                session = message[key].get("Sessions", "")
                continue
            print(FORMAT % (key, message.get(key, None)))
        # Query rules and sessions.
        get_vncrule_session(rule, session, client)
    elif resp['status_code'] == 404:
        print('Failure: resource was not found')
    return resp
