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


def getstateless_init(parser, parser_list):
    """
    #=========================================================================
    #   @Description: get stateless info subcommand init
    #   @Method:  getstateless_init
    #   @Param:
    #   @Return:
    #=========================================================================
    """
    sub_parser = parser.add_parser('getstatless',
                                   help='get stateless '
                                        'computation information')
    parser_list['getstatless'] = sub_parser

    return 'getstatless'


def getstateless(client, _):
    """
    #=========================================================================
    #   @Description: get stateless
    #   @Method:  getstateless
    #   @Param:
    #   @Return:
    #=========================================================================
    """
    slotid = client.get_slotid()

    if slotid is None:
        return None

    length = 26
    mformat = '%-*s:%s'

    url = "/redfish/v1/Managers/%s" % slotid
    resp = client.get_resource(url)

    if resp is None:
        return None

    if resp['status_code'] == 200:
        resp_inner_dic = common_function.get_vendor_value(resp)
        stateless = resp_inner_dic.get('Stateless', {})

        sysmanagerid = stateless.get('SysManagerId', {})
        sysmanagerip = stateless.get('SysManagerIP', {})
        sysmanagerport = stateless.get('SysManagerPort', {})
        controlpoweronenabled = stateless.get('ControlPowerOnEnabled', {})
        autosearchenabled = stateless.get('AutoSearchEnabled', {})

        print(mformat % (length, 'SysManagerId', sysmanagerid))
        print(mformat % (length, 'SysManagerIP', sysmanagerip))
        print(mformat % (length, 'SysManagerPort', sysmanagerport))
        print(mformat %
              (length, 'ControlPowerOnEnabled', controlpoweronenabled))
        print(mformat % (length, 'AutoSearchEnabled', autosearchenabled))

    return resp
