# -*- coding:utf-8 -*-
"""
Function: get_sp_diagnose.py moudle. This moudle mainly involves the
 querying the Diagnosis Configuration Resource of the
  Smart Provisioning Service function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2019-2021
"""
from scripts import common_function

FORMAT = '%-30s: %s'


def getspdiagnose_init(parser, parser_dict):
    """
    Function Description:initializing the Command for querying the
     diagnosis configuration resource information of the
      smart Provisioning service
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    Modify:  2019.4.28 The help information is optimized.
    Return Value: subcommand
    """
    sub_parser = parser.add_parser('getspdiagnose',
                                   help='''obtain SP diagnosis information.''')

    parser_dict['getspdiagnose'] = sub_parser

    return 'getspdiagnose'


def getspdiagnose(client, _):
    """
    #=====================================================================
    #   @Method: SP diagnose information query subcommand processing function
    #   @Param:  client, RedfishClient object
    #   @Return:
    #   @author:
    #   @date:
    #=====================================================================
    """
    slotid = client.get_slotid()
    if slotid is None:
        return None
    url = "/redfish/v1/Managers/%s/SPService/SPDiagnose/1" % slotid
    resp = client.get_resource(url)
    if resp is None:
        return None
    if resp['status_code'] == 200:
        print_result(resp)
    else:
        common_function.get_error_info(resp)
    return resp


def print_result(resp):
    """
    功能描述：打印 请求结果
    参数：resp (dict):redfish 接口返回值
    返回值：None
    异常描述：None
    修改：None
    """
    info = resp.get('resource', None)
    if info is None:
        print('No data is available for the resource.')
        return
    diagnose_list = info.get("Diagnose", None)
    if not diagnose_list:
        print('No data is available for the resource.')
        return
    for diagnose in diagnose_list:
        print(FORMAT % ("DeviceType", diagnose.get("DeviceType", None)))
        print(FORMAT % ("Mode", diagnose.get("Mode", None)))
        print(FORMAT %
              ("DurationMinutes", diagnose.get("DurationMinutes", None)))
        print("-" * 40)
