# -*- coding:utf-8 -*-
"""
Function: set_ps_redundancy.py moudle. This moudle mainly involves the
Querying PCIe SSD Card Information function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2017-2020
"""
import sys
from scripts import common_function

PF = '{0:30}: {1}'


def getpciessdcard_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    Querying PCIe SSD Card Information
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    """
    sub_parser = parser.add_parser('getssdcard',
                                   help='''get PCIe SSD card information''')
    parser_dict['getssdcard'] = sub_parser

    return 'getssdcard'


def getpciessdcard(client, _):
    """
    Function Description:Querying PCIe SSD Card Information
    Parameter:client refishClient: class object
    """
    slotid = client.get_slotid()
    if slotid is None:
        return None

    chassis_url = "/redfish/v1/Chassis/" + slotid
    resp = client.get_resource(chassis_url)
    if resp is None:
        return None
    if resp['status_code'] != 200:
        if resp['status_code'] == 404:
            print('Failure: resource was not found')
        return resp

    print_result(client, resp)

    return resp


def print_result(client, resp):
    """
    Function Description:print result
    Parameter:client refishClient: class object
    resp (dict):redfish interface value
    """
    # In earlier versions, the Drives key does not exist.
    if resp['resource'].get('Links', {}).get('Drives', None) is None:
        print('Failure: resource was not found')
        sys.exit(148)
    # If no member exists
    drive_array = resp['resource']['Links']['Drives']
    if drive_array:
        flag = False
        for drive_info in drive_array:
            url = drive_info.get('@odata.id', '')
            if url.find('SSD') > 0:
                flag = True
                get_drive_info(client, url)
        if not flag:
            print('Failure: resource was not found')
            sys.exit(148)
    else:
        print('Failure: resource was not found')
        sys.exit(148)


def get_drive_info(client, drive_uri):
    """
    Function Description:Obtain PCIe card information.
    Parameter:client refishClient: class object
    drive_uri str:resource URL
    """
    drive_resp = client.get_resource(drive_uri)
    if drive_resp is None:
        return None
    if drive_resp['status_code'] != 200:
        return drive_resp

    drive_info = drive_resp['resource']
    oem_info = common_function.get_vendor_value(drive_resp)
    print(PF.format('Id', oem_info.get('DriveID', '')))
    print(PF.format('Name', drive_info.get('Location', {})[0].get('Info', '')))
    print(PF.format('Position', oem_info.get('Position', '')))
    print(PF.format('Health', drive_info.get('Status', {}).get('Health', '')))
    print(PF.format('State', drive_info.get('Status', {}).get('State', '')))
    print(PF.format('Manufacturer', drive_info.get('Manufacturer', {})))
    print(PF.format('Model', drive_info.get('Model', '')))
    print(PF.format('Protocol', drive_info.get('Protocol', '')))
    print(PF.format('FailurePredicted', drive_info.get('FailurePredicted', '')))
    print(PF.format('IndicatorLED', drive_info.get('IndicatorLED', '')))
    print(PF.format('PredictedMediaLifeLeftPercent',
                    drive_info.get('PredictedMediaLifeLeftPercent', '')))
    print(PF.format('MediaType', drive_info.get('MediaType', '')))
    key_list = ['Id', '@odata.context', '@odata.id', '@odata.type', 'Location',
                'MediaType', 'Name', 'Status', 'Manufacturer', 'CapacityBytes',
                'Protocol', 'FailurePredicted', 'HotspareType', 'IndicatorLED',
                'Model', 'PredictedMediaLifeLeftPercent', 'Links',
                'CapableSpeedGbs', 'NegotiatedSpeedGbs', 'StatusIndicator',
                'Revision', 'Oem']
    for key in drive_info:
        if key in key_list:
            continue
        print(PF.format(key, drive_info[key]))
    print('\n[Status]')
    print(PF.format('Health', drive_info.get('Status', {}).get('Health', '')))
    print(PF.format('State', drive_info.get('Status', {}).get('State', '')))
    print('-' * 50)

    return drive_resp
