# -*- coding:utf-8 -*-
"""
Function: get_memory_count.py moudle. This moudle mainly involves the
memory quantity information.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2023-2023
"""
from scripts import common_function
from scripts.common_function import TIMEOUT_THIRTY
from scripts.common_function import REDFISH_STATUS_CODE_404
from scripts.common_function import REDFISH_STATUS_CODE_200


INVALID_VALUE = None


def getmemorycount_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    memory quantity information.
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    """
    sub_parser = parser.add_parser('getmemorycount',
                                   help='get memory count information')
    parser_dict['getmemorycount'] = sub_parser

    return 'getmemorycount'


def getmemorycount(client, _):
    """
    Function Description:Get memory quantity information.
    Parameter:client refishClient: class object
    """
    # Obtain upgradeable driver collection resources.
    slotid = client.get_slotid()
    if slotid is None:
        return INVALID_VALUE
    url = "/redfish/v1/Systems/%s/Memory" % slotid
    resp = client.get_resource(url, timeout=TIMEOUT_THIRTY)
    if resp is None:
        return INVALID_VALUE
    if resp['status_code'] == REDFISH_STATUS_CODE_404:
        common_function.print_info('Failure: failed to get memories')
    elif resp['status_code'] == REDFISH_STATUS_CODE_200:
        memory_urls = resp['resource'].get("Members", '')
        if len(memory_urls) == 0:
            common_function.print_info('Failure: Memory not found')
            return INVALID_VALUE
        common_function.print_info("-" * 80)
        common_function.print_info("%-18s%-18s%-18s%-s" % ("Slot", "SerialNumber", "ErrorCount", "Description"))
        for memory_url in memory_urls:
            result = client.get_resource(memory_url['@odata.id'], timeout=TIMEOUT_THIRTY)
            if result['status_code'] == REDFISH_STATUS_CODE_404:
                common_function.print_info('Failure: failed to get memory count')
            elif result['status_code'] == REDFISH_STATUS_CODE_200:
                print_result(result)


def print_result(result):
    """
    Function Description:print result
    Parameter:client refishClient: class object
    resp dict: redfish interface result
    """
    serial_number = result.get("resource", {}).get("SerialNumber", '')
    slot = result.get("resource", {}).get("DeviceLocator", {})
    err_count = result.get("resource", {}).get("Oem", {}).get(common_function.COMMON_KEY, {}).get("EccCount", '')
    description = 'No memory failure detected' \
        if err_count == 0 else 'There are %s errors in the current memory' % err_count
    if len(str(err_count)) == 0:
        description = ''
    common_function.print_info("%-18s%-18s%-18s%-s" % (slot, serial_number, err_count, description))
