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

PRINT_STYLE = "%-20s: %s"


def getosinfo_init(parser, parser_list):
    """
    #==========================================================================
    # @Method: Obtain system information command initialization functions.
    # @Param: client, RedfishClient object
    parser, subcommand argparser.
    Export error messages when parameters are incorrect.
    args, parameter list
    # @Return:
    # @author:
    # @date: 2017.7.21
    #==========================================================================
    """
    sub_parser = parser.add_parser('getosinfo',
                                   help='''get operating system information''')
    parser_list['getosinfo'] = sub_parser

    return 'getosinfo'


def getosinfo(client, _):
    """
    #==========================================================================
    # @Method: Obtain system information command processing functions.
    # @Param: client, RedfishClient object
    parser, subcommand argparser.
    Export error messages when parameters are incorrect.
    args, parameter list
    # @Return:
    # @author:
    # @date: 2017.7.21
    #==========================================================================
    """
    # Modify: 2017.8.29 Pylint alarm clearance
    slotid = client.get_slotid()
    if slotid is None:
        return None
    url = "/redfish/v1/systems/%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)
        print(PRINT_STYLE % ("OSVersion", resp_inner_dic.get("OSVersion", "")))
        print(PRINT_STYLE % ("KernalVersion", resp_inner_dic.get("KernalVersion", "")))
        print(PRINT_STYLE % ("MajorOSVersion",
                             resp_inner_dic.get("MajorOSVersion", "")))
        print(PRINT_STYLE % ("MinorOSVersion",
                             resp_inner_dic.get("MinorOSVersion", "")))
        print(PRINT_STYLE % ("MajorPatchVersion",
                             resp_inner_dic.get("MajorPatchVersion", "")))
        print(PRINT_STYLE % ("MinorPatchVersion",
                             resp_inner_dic.get("MinorPatchVersion", "")))
        print(PRINT_STYLE % ("DomainName", resp_inner_dic.get("DomainName", "")))
        print(PRINT_STYLE % ("HostDescription",
                             resp_inner_dic.get("HostDescription", "")))

    elif resp['status_code'] == 404:
        print('Failure: Resource was not found')
    else:
        return resp
    return resp
