# -*- coding:utf-8 -*-
"""
Function: get_logicaldrive.py. This file mainly involves the
 getting Logical Disk information function.
Copyright: xFusion Digital Technology Co., Ltd. All Rights Reserved (C) 2022-2022
"""
from scripts import common_function
from scripts.common_function import REDFISH_STATUS_CODE_404, \
    REDFISH_STATUS_CODE_200, UREST_STATUS_CODE_148, RESOURCE, OEM

FORMAT = '%-30s: %s'


def getlogicaldrive_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    get Logical Disk.
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    Return Value: subcommand
    """
    sub_parser = parser.add_parser('getvolumes',
                                   help='''get logical drive information''')

    parser_dict['getvolumes'] = sub_parser
    return 'getvolumes'


def getlogicaldrive(client, _):
    """
    #==========================================================================
    #   @Method:  get logical disk processing function.
    #   @Param:   client, RedfishClient object
                  parser, subcommand argparser.
                          Export error messages when parameters are incorrect.
                  args, parameter list
    #   @Return:
    #   @author:
    #==========================================================================
    """
    slotid = client.get_slotid()
    if slotid is None:
        return UREST_STATUS_CODE_148
    storages_url = "/redfish/v1/Systems/%s/Storages" % slotid
    storages_resp = client.get_resource(storages_url)
    if storages_resp is None:
        return UREST_STATUS_CODE_148
    if storages_resp.get("status_code") == REDFISH_STATUS_CODE_404:
        print('Failure: resource was not found')
        return storages_resp
    if storages_resp.get("status_code") != REDFISH_STATUS_CODE_200:
        print('Failure: get storages error')
        return storages_resp
    result = []
    for member in storages_resp.get(RESOURCE).get("Members", []):
        volume_url = "%s/Volumes" % member.get("@odata.id")
        get_logical_driver_info(client, result, volume_url)
    print_result(result)
    return storages_resp


def get_logical_driver_info(client, result, volume_url):
    """
    Function Description:Querying Processors Information
    Parameter: client refishClient: class object
    Parameter: result
    Parameter: volume_url
    """
    volume_resp = client.get_resource(volume_url)
    if volume_resp.get("status_code") != REDFISH_STATUS_CODE_200:
        return
    for member in volume_resp.get(RESOURCE).get("Members"):
        drive_url = member.get("@odata.id")
        drive_resp = client.get_resource(drive_url)
        if drive_resp.get("status_code") != REDFISH_STATUS_CODE_200:
            continue
        result.append(drive_resp.get(RESOURCE))


def print_result(result):
    """
    Function Description:print result
    Parameter: result: redfish interface Value
    """
    if len(result) == 0:
        print("no data available for the resource")
        return
    print("-" * 50)
    for drive_resp in result:
        print(FORMAT % ("Name", drive_resp.get("Name", '')))
        print(FORMAT % ("CapacityBytes", drive_resp.get("CapacityBytes", '')))
        print(FORMAT % ("VolumeType", drive_resp.get("VolumeType", '')))
        print(FORMAT % ("OptimumIOSizeBytes", drive_resp.get("OptimumIOSizeBytes", '')))
        status = drive_resp.get("Status", {})
        print(FORMAT % ("State", status.get("State", '')))
        print(FORMAT % ("Health", status.get("Health, ''")))
        print(FORMAT % ("Severity", status.get("Oem", {})
                        .get(common_function.COMMON_KEY).get("Severity", '')))
        oem_info = drive_resp.get(OEM).get(common_function.COMMON_KEY)
        print(FORMAT % ("RaidControllerID", oem_info.get("RaidControllerID", '')))
        print(FORMAT % ("VolumeRaidLevel", oem_info.get("VolumeRaidLevel", '')))
        print(FORMAT % ("DefaultReadPolicy", oem_info.get("DefaultReadPolicy", '')))
        print(FORMAT % ("DefaultWritePolicy", oem_info.get("DefaultWritePolicy", '')))
        print(FORMAT % ("DefaultCachePolicy", oem_info.get("DefaultCachePolicy", '')))
        print(FORMAT % ("ConsistencyCheck", oem_info.get("ConsistencyCheck", '')))
        print(FORMAT % ("CurrentReadPolicy", oem_info.get("CurrentReadPolicy", '')))
        print(FORMAT % ("CurrentWritePolicy", oem_info.get("CurrentWritePolicy", '')))
        print(FORMAT % ("CurrentCachePolicy", oem_info.get("CurrentCachePolicy", '')))
        print(FORMAT % ("AccessPolicy", oem_info.get("AccessPolicy", '')))
        print(FORMAT % ("BootEnable", oem_info.get("BootEnable", '')))
        print(FORMAT % ("BGIEnable", oem_info.get("BGIEnable", '')))
        print(FORMAT % ("SSDCachecadeVolume", oem_info.get("SSDCachecadeVolume", '')))
        print(FORMAT % ("SSDCachingEnable", oem_info.get("SSDCachingEnable", '')))
        print(FORMAT % ("DriveCachePolicy", oem_info.get("DriveCachePolicy", '')))
        print(FORMAT % ("OSDriveName", oem_info.get("OSDriveName", '')))
        print(FORMAT % ("InitializationMode", oem_info.get("InitializationMode", '')))
        print("-" * 50)
