#include "mms_m_errstr.h"
#include <string>


static const char *str_IED_ERROR_OK = "IED_ERROR_OK";








static struct _mms_m_err_str
{
    IedClientError err;
    const char *str;
}g_mms_m_err_str[] = 
{
    // general errors
    {IED_ERROR_OK,                                      "IED_ERROR_OK"},
    {IED_ERROR_NOT_CONNECTED,                           "IED_ERROR_NOT_CONNECTED"},
    {IED_ERROR_ALREADY_CONNECTED,                       "IED_ERROR_ALREADY_CONNECTED"},
    {IED_ERROR_CONNECTION_LOST,                         "IED_ERROR_CONNECTION_LOST"},
    {IED_ERROR_SERVICE_NOT_SUPPORTED,                   "IED_ERROR_SERVICE_NOT_SUPPORTED"},
    {IED_ERROR_CONNECTION_REJECTED,                     "IED_ERROR_CONNECTION_REJECTED"},
    {IED_ERROR_OUTSTANDING_CALL_LIMIT_REACHED,          "IED_ERROR_OUTSTANDING_CALL_LIMIT_REACHED"},

    // client side errors
    {IED_ERROR_USER_PROVIDED_INVALID_ARGUMENT,          "IED_ERROR_USER_PROVIDED_INVALID_ARGUMENT"},
    {IED_ERROR_ENABLE_REPORT_FAILED_DATASET_MISMATCH,   "IED_ERROR_ENABLE_REPORT_FAILED_DATASET_MISMATCH"},
    {IED_ERROR_OBJECT_REFERENCE_INVALID,                "IED_ERROR_OBJECT_REFERENCE_INVALID"},
    {IED_ERROR_UNEXPECTED_VALUE_RECEIVED,               "IED_ERROR_UNEXPECTED_VALUE_RECEIVED"},

    // server side errors
    {IED_ERROR_TIMEOUT,                                "IED_ERROR_TIMEOUT"},
    {IED_ERROR_ACCESS_DENIED,                          "IED_ERROR_ACCESS_DENIED"},
    {IED_ERROR_OBJECT_DOES_NOT_EXIST,                   "IED_ERROR_OBJECT_DOES_NOT_EXIST"},
    {IED_ERROR_OBJECT_EXISTS,                          "IED_ERROR_OBJECT_EXISTS"},
    {IED_ERROR_OBJECT_ACCESS_UNSUPPORTED,              "IED_ERROR_OBJECT_ACCESS_UNSUPPORTED"},
    {IED_ERROR_TYPE_INCONSISTENT,                      "IED_ERROR_TYPE_INCONSISTENT"},
    {IED_ERROR_TEMPORARILY_UNAVAILABLE,                "IED_ERROR_TEMPORARILY_UNAVAILABLE"},
    {IED_ERROR_OBJECT_UNDEFINED,                       "IED_ERROR_OBJECT_UNDEFINED"},
    {IED_ERROR_INVALID_ADDRESS,                        "IED_ERROR_INVALID_ADDRESS"},
    {IED_ERROR_HARDWARE_FAULT,                         "IED_ERROR_HARDWARE_FAULT"},
    {IED_ERROR_TYPE_UNSUPPORTED,                       "IED_ERROR_TYPE_UNSUPPORTED"},
    {IED_ERROR_OBJECT_ATTRIBUTE_INCONSISTENT,           "IED_ERROR_OBJECT_ATTRIBUTE_INCONSISTENT"},
    {IED_ERROR_OBJECT_VALUE_INVALID,                   "IED_ERROR_OBJECT_VALUE_INVALID"},
    {IED_ERROR_OBJECT_INVALIDATED,                     "IED_ERROR_OBJECT_INVALIDATED"},
    {IED_ERROR_MALFORMED_MESSAGE,                      "IED_ERROR_MALFORMED_MESSAGE"},
    {IED_ERROR_SERVICE_NOT_IMPLEMENTED,                "IED_ERROR_SERVICE_NOT_IMPLEMENTED"},

    {IED_ERROR_UNKNOWN,                                 "IED_ERROR_UNKNOWN"},
};

const char *mms_m_err_str(IedClientError err)
{
    for(int i = 0; i < sizeof(g_mms_m_err_str) / sizeof(struct _mms_m_err_str); i++)
    {
        if(g_mms_m_err_str[i].err == err)
        {
            return g_mms_m_err_str[i].str;
        }
    }

    return "IED_ERROR_UNKNOWN";
}



static struct _mms_m_control_err_str
{
    ControlLastApplError err;
    const char *str;
}g_mms_m_control_err_str[] = 
{
    // general errors
    {CONTROL_ERROR_NO_ERROR,        "CONTROL_ERROR_NO_ERROR"},
    {CONTROL_ERROR_UNKNOWN,         "CONTROL_ERROR_UNKNOWN"},
    {CONTROL_ERROR_TIMEOUT_TEST,    "CONTROL_ERROR_TIMEOUT_TEST"},
    {CONTROL_ERROR_OPERATOR_TEST,   "CONTROL_ERROR_OPERATOR_TEST"}
};

const char *mms_m_control_err_str(ControlLastApplError err)
{
    for(int i = 0; i < sizeof(g_mms_m_control_err_str) / sizeof(struct _mms_m_control_err_str); i++)
    {
        if(g_mms_m_control_err_str[i].err == err)
        {
            return g_mms_m_control_err_str[i].str;
        }
    }

    return "MMS_M_CONTROL_ERR_UNKNOWN";
}

static struct _mms_m_ctl_add_cause_str
{
    ControlAddCause cause;
    const char *str;
}g_mms_m_ctl_add_cause_str[] = 
{
    {ADD_CAUSE_UNKNOWN,                             "ADD_CAUSE_UNKNOWN"},
    {ADD_CAUSE_NOT_SUPPORTED,                       "ADD_CAUSE_NOT_SUPPORTED"},
    {ADD_CAUSE_BLOCKED_BY_SWITCHING_HIERARCHY,      "ADD_CAUSE_BLOCKED_BY_SWITCHING_HIERARCHY"},
    {ADD_CAUSE_SELECT_FAILED,                       "ADD_CAUSE_SELECT_FAILED"},
    {ADD_CAUSE_INVALID_POSITION,                    "ADD_CAUSE_INVALID_POSITION"},
    {ADD_CAUSE_POSITION_REACHED,                    "ADD_CAUSE_POSITION_REACHED"},
    {ADD_CAUSE_PARAMETER_CHANGE_IN_EXECUTION,       "ADD_CAUSE_PARAMETER_CHANGE_IN_EXECUTION"},
    {ADD_CAUSE_STEP_LIMIT,                          "ADD_CAUSE_STEP_LIMIT"},
    {ADD_CAUSE_BLOCKED_BY_MODE,                     "ADD_CAUSE_BLOCKED_BY_MODE"},
    {ADD_CAUSE_BLOCKED_BY_PROCESS,                  "ADD_CAUSE_BLOCKED_BY_PROCESS"},
    {ADD_CAUSE_BLOCKED_BY_INTERLOCKING,             "ADD_CAUSE_BLOCKED_BY_INTERLOCKING"},
    {ADD_CAUSE_BLOCKED_BY_SYNCHROCHECK,             "ADD_CAUSE_BLOCKED_BY_SYNCHROCHECK"},
    {ADD_CAUSE_COMMAND_ALREADY_IN_EXECUTION,        "ADD_CAUSE_COMMAND_ALREADY_IN_EXECUTION"},
    {ADD_CAUSE_BLOCKED_BY_HEALTH,                   "ADD_CAUSE_BLOCKED_BY_HEALTH"},
    {ADD_CAUSE_1_OF_N_CONTROL,                      "ADD_CAUSE_1_OF_N_CONTROL"},
    {ADD_CAUSE_ABORTION_BY_CANCEL,                  "ADD_CAUSE_ABORTION_BY_CANCEL"},
    {ADD_CAUSE_TIME_LIMIT_OVER,                     "ADD_CAUSE_TIME_LIMIT_OVER"},
    {ADD_CAUSE_ABORTION_BY_TRIP,                    "ADD_CAUSE_ABORTION_BY_TRIP"},
    {ADD_CAUSE_OBJECT_NOT_SELECTED,                 "ADD_CAUSE_OBJECT_NOT_SELECTED"},
    {ADD_CAUSE_OBJECT_ALREADY_SELECTED,             "ADD_CAUSE_OBJECT_ALREADY_SELECTED"},
    {ADD_CAUSE_NO_ACCESS_AUTHORITY,                 "ADD_CAUSE_NO_ACCESS_AUTHORITY"},
    {ADD_CAUSE_ENDED_WITH_OVERSHOOT,                "ADD_CAUSE_ENDED_WITH_OVERSHOOT"},
    {ADD_CAUSE_ABORTION_DUE_TO_DEVIATION,           "ADD_CAUSE_ABORTION_DUE_TO_DEVIATION"},
    {ADD_CAUSE_ABORTION_BY_COMMUNICATION_LOSS,      "ADD_CAUSE_ABORTION_BY_COMMUNICATION_LOSS"},
    {ADD_CAUSE_ABORTION_BY_COMMAND,                 "ADD_CAUSE_ABORTION_BY_COMMAND"},
    {ADD_CAUSE_NONE,                                "ADD_CAUSE_NONE"},
    {ADD_CAUSE_INCONSISTENT_PARAMETERS,             "ADD_CAUSE_INCONSISTENT_PARAMETERS"},
    {ADD_CAUSE_LOCKED_BY_OTHER_CLIENT,              "ADD_CAUSE_LOCKED_BY_OTHER_CLIENT"},
};

const char *mms_m_ctl_add_cause_str(ControlAddCause cause)
{
    for(int i = 0; i < sizeof(g_mms_m_ctl_add_cause_str) / sizeof(struct _mms_m_ctl_add_cause_str); i++)
    {
        if(g_mms_m_ctl_add_cause_str[i].cause == cause)
        {
            return g_mms_m_ctl_add_cause_str[i].str;
        }
    }

    return "MMS_M_CTL_ADD_CAUSE_UNKNOWN";
}

static struct _mms_m_ctrl_model_str
{
    ControlModel model;
    const char *str;
}g_mms_m_ctrl_model_str[] = 
{
    {CONTROL_MODEL_STATUS_ONLY,             "status-only"},
    {CONTROL_MODEL_DIRECT_NORMAL,           "direct-with-normal-security"},
    {CONTROL_MODEL_SBO_NORMAL,              "sbo-with-normal-security"},
    {CONTROL_MODEL_DIRECT_ENHANCED,         "direct-with-enhanced-security"},
    {CONTROL_MODEL_SBO_ENHANCED,            "sbo-with-enhanced-security"},
};

const char *mms_m_ctrl_model_str(ControlModel model)
{
    for(int i = 0; i < sizeof(g_mms_m_ctrl_model_str) / sizeof(struct _mms_m_ctrl_model_str); i++)
    {
        if(g_mms_m_ctrl_model_str[i].model == model)
        {
            return g_mms_m_ctrl_model_str[i].str;
        }
    }

    return "MMS_M_CTL_MODEL_UNKNOWN";
}
