"""
    Flash Programmer service error information module

Copyright 2025 NXP.

SPDX-License-Identifier: BSD-3-Clause

"""

import gdb

def get_error_info(error_code):
    errors = {
        600: "COMMAND_ERROR.",
        601: "ADD_DEVICE_ERROR",
        603: "COMMAND_NOT_SUPPORTED",
        604: "FAIL_TO_EXECUTE",
        607: "FP_NO_DEVICE",
        608: "FP_NO_SERVICE",
        609: "FP_NO_SUPPORT_FOR_DEVICE",
        610: "FP_NO_SUPPORT_FOR_ARCHITECTURE",
        611: "NOT_IMPLEMENTED",
        612: "NO_DEVICE_FOR_CURRENT_CONTEXT",
        613: "SECTOR_ID_NOT_NUMBER",
        614: "INVALID_PARAMETERS_NUMBER",
        615: "ADDRESS_NOT_NUMBER",
        616: "SIZE_NOT_NUMBER",
        617: "INVALID_COMMAND_PARAMETERS",
        620: "INVALID_GEOMETRY",
        621: "DEVICE_NOT_CONFIGURED",
        622: "ADDRESS_NOT_SET",
        624: "FP_DEVICE_ALREADY_EXISTS",
        625: "FP_XML_ERR",
        626: "FP_TIMED_OUT_ERR",
        627: "FP_UNSUPPORTED_ERR",
        628: "VERIFY_ERR",
        630: "SECTOR_PROTECTED_ERR",
        632: "ERASE_ERR",
        633: "PROG_ERR",
        634: "DEV_PROT_ERR",
        635: "ALIGN_32_ERR",
        636: "ALIGN_64_ERR",
        637: "RANGE_ERR",
        638: "FP_INIT_ERR",
        643: "ALGORITHM_FIT_ERR",
        644: "RESUME_CALLBACK",
        649: "WAIT_FOR_TARGET_CALLBACK",
        651: "GEOMETRY_NOT_SELECTED_ERR",
        652: "ARCHITECTURE_NOT_SELECTED_ERR",
        653: "CONTROLLER_NOT_SELECTED_ERR",
        654: "PARAMETER_BLOCK_NOT_SET_ERR",
        655: "WORKSPACE_NOT_SET_ERR",
        656: "BASE_ADDRESS_NOT_SET_ERR",
        657: "ALGORITHM_NOT_FOUND_ERR",
        658: "ALGORITHM_SIZE_NOT_KNOWN_ERR",
        659: "ALGORITHM_ENTRY_POINT_NOT_KNOWN_ERR",
        660: "INVALID_SECTOR_INDEX_ERR",
        661: "OPERATION_CANCEL_ERR",
        662: "OPERATION_UNKNOWN_ERR",
        664: "DATA_BUFFER_NOT_ALIGNED_ERR",
        667: "OPERATION_NOT_SUPPORTED_ERR",
        668: "NOT_CONNECTED_ERR",
        669: "NO_DEVICE_SELECTED_ERR",
        672: "INVALID_PATH_ERR",
        673: "SIZE_NEGATIVE",
        674: "SIZE_NOT_ZERO",
        675: "OFFSET_NEGATIVE",
        677: "OFFSET_EXCEEDS_CAPACITY",
        678: "SIZE_EXCEEDS_CAPACITY",
        679: "INVALID_HEXADECIMAL_VALUE",
    }
    
    if(error_code in errors):
        return "Error code: {}\nError name: {}\n".format(error_code, errors[error_code])
    else:
        return "Unknown error code or internal error: {}\n".format(error_code)


def fperr_pretty_print(error_msg):
    lines = error_msg.splitlines()
    first_line = lines[0] if lines else ""
    err_num = 0
    if first_line != "":
        err_num = int(first_line)
    info_str = get_error_info(err_num)
    rest = lines[1:] if len(lines) > 1 else ""
    gdb.write("==========================================\n")
    gdb.write("F-ERROR: GTA RETURNED ERROR\n")
    gdb.write("==========================================\n")
    gdb.write("ERROR:\n")
    gdb.write("%s" % info_str)
    if rest != "":
        gdb.write("------------------------------------------\n")
        gdb.write("GTA TEXT:\n")
        for line in rest:
            gdb.write("%s\n" % line)
    gdb.write("==========================================\n")
    return 0