/*
  Copyright 2025 Matthias-Sebastian Schröder <matthias.schroeder@dolanghub.de>

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

#pragma once

#include "dobase/vmstack.hpp"
#include "dobase/string.hpp"

__NAMESPACE_DO_START

#define ERR_FILE_NOT_FOUND               1
#define ERR_FILE_IN_USE                  3
#define ERR_EOF                          4
#define ERR_RECORD_OUT_OF_RANGE          5
#define ERR_FILE_EXISTS                  7
#define ERR_DATA_TYPE_MISSMATCH          9
#define ERR_SYNTAX_ERROR                10
#define ERR_ARG_VAL_TYPE                11
#define ERR_VAR_NOT_FOUND               12
#define ERR_ALIAS_NOT_FOUND             13
#define ERR_NOT_A_TABLE                 15
#define ERR_INVALID_TABLE_NUM           17
#define ERR_ALIAS_IN_USE                24
#define ERR_NO_INDEX_ORDER_SET          26
#define ERR_OP_INVALID_MEMO_BLOB_PIC    34
#define ERR_FILTER_MUST_BE_LOGICAL      37
#define ERR_BOF                         38
#define ERR_NUMERIC_OVERFLOW            39
#define ERR_MEMO_FILE_INV_OR_MISSING    41
#define ERR_NO_TABLE_IN_CURRENT_WA      52
#define ERR_SQRT_ARG_NEGATIVE           61
#define ERR_INV_POW_DOMAIN              78
#define ERR_MUST_SPEC_ADDI_PARAMS       94
#define ERR_NESTING_ERROR               96
#define ERR_MAX_DO_NEST_LEVEL          103
#define ERR_OP_TYPE_MISSMATCH          107
#define ERR_MUST_OPEN_EXCLUSIVE        110
#define ERR_CURSOR_READONLY            111
#define ERR_INVALID_KEY_LEN            112
#define ERR_INVALID_PATH_OR_FNAME      202
#define ERR_ARRAY_DIMS_INVALID         230
#define ERR_INV_ARG_SET_FUNC           231
#define ERR_NOT_AN_ARRAY               232

#define ERR_FEATURE_NOT_AVAILABLE     1001
#define ERR_USER_DEFINED              1098

#define ERR_CREATE_TEMP_FILE          1101
#define ERR_CREATE_FILE               1102
#define ERR_READING_FILE              1104
#define ERR_WRITING_TO_FILE           1105
#define ERR_FOR_WHILE_MUST_BE_LOG     1127
#define ERR_DUP_FIELD_NAMES           1156

#define ERR_MACRO_SUBST_RECURSIVE     1206
#define ERR_IF_ELSE_ENDIF_MISSING     1211
#define ERR_TOO_FEW_ARGUMENTS         1229
#define ERR_SUBSCRIPT_OUT_OF_RANGE    1234
#define ERR_OLE_IDISPATCH_EXCEPTION   1429
#define ERR_INVALID_HANDLE_CONN       1466
#define ERR_SQL_NEED_PREPARE          1472
#define ERR_ODBC_DRIVER_ERROR         1526
#define ERR_INV_TABLE_CHANGE_INDEX    1531

#define ERR_PROPERTY_IS_READONLY      1533
#define ERR_DS_NUMBER_INVALID         1540
#define ERR_FIELD_CANT_BE_NULL        1581
#define ERR_UPDATE_CONFLICT           1585
#define ERR_TRANS_NEST_LEVEL          1590
#define ERR_END_TRANS_NO_TRANS        1591
#define ERR_ROLLBACK_NO_TRANS         1592

#define ERR_INDEX_TAG_NOT_FOUND       1683

#define ERR_FILE_ACCESS_DENIED        1705
#define ERR_CDX_FILE_NOT_FOUND        1707
#define ERR_FIELD_DUP_OR_INV          1712
#define ERR_FIELD_W_O_ANZ_D_INV       1713
#define ERR_FILE_IS_READONLY          1718
#define ERR_WIDTH_OR_NUM_DEC_INV      1731
#define ERR_CLASS_DEF_NOT_FOUND       1733
#define ERR_PROP_NOT_FOUND            1734
#define ERR_PROP_IS_READONLY          1743
#define ERR_PROP_IS_CLASS_MEMBER      1760

#define ERR_CANT_DROP_ALL_COLUMNS     1871
#define ERR_UNIQUE_KEY_VIOLATION      1884
#define ERR_INDEX_CANT_STORE_NULL     1886
#define ERR_RECURSIVE_RULE_EVAL       1887
#define ERR_TAG_NAME_TOO_LONG         1888
#define ERR_TAG_NAME_DUPLICATE        1898 // DO
#define ERR_TAG_NAME_INV_CHARS        1899 // DO

#define ERR_NOT_AN_OBJECT             1924
#define ERR_INVALID_STMT_CLASS_MEMBER 1927
#define ERR_INVALID_OUTSIDE_WITH      1940
#define ERR_INVALID_REDEFINE          1960
#define ERR_CURRENCY_OVERFLOW         1988
#define ERR_DECLARE_DLL_EXCEPTION     2027
#define ERR_INVALID_DATE_OR_TIME      2034
#define ERR_TEXTMERGE_IS_RECURSIVE    2044
#define ERR_UNHANDLED_STRUCT_EXECPT   2059
#define ERR_NO_COLL_MEMBER            2061
#define ERR_COLL_DUP_KEY              2062
#define ERR_INDEX_FILE_CORRUPTED      2066
#define ERR_FIELD_READONLY            2088
#define ERR_TABLE_BECAME_CORRUPTED    2091
#define ERR_INV_EXPR_FOR_BINARY_IDX   2156
#define ERR_UNKNOWN_TYPE_INDEX_EXPR   2201

#define ERR_NO_DEBUGGER_AVAIL         9001
#define ERR_CHARSET_NOT_SUPPORTED     9003
#define ERR_CANT_PASS_TMP_R_BY_REF    9004
#define ERR_CLASS_IS_ABSTRACT         9005
#define ERR_NATIVE_TABLE_ENGINE       9006
#define ERR_PATTERN_SYNTAX            9007
#define ERR_CANT_GET_POINTER          9008
#define ERR_CHAR_BUF_INVALID          9009
#define ERR_STACK_OVERFLOW            9010
#define ERR_CPTRANS_CHAR_LOST         9011
#define ERR_PARSER_SYNTAX_AMBIGOUS    9012
#define ERR_PARSER_STACK_EXHAUSTED    9013
#define ERR_CANT_PASS_UNSAFE_POINTER  9014
#define ERR_INTEGER_OVERFLOW          9015
#define ERR_FILE_BOM_INVALID          9016

#define ERR_GENERIC_FILESYSTEM_ERROR  9901
#define ERR_GENERIC_TABLE_ERROR       9902

enum class InterruptType : int
{
  RT_ERROR, RT_CANCEL, RT_THROW
};

class assert_failed_exception
{
};

// Export vtable
class DOBASE_EXPORT RuntimeInterrupt
{
public:
  RuntimeInterrupt(InterruptType type);
  virtual ~RuntimeInterrupt() {};
  InterruptType type;
};

class DOBASE_EXPORT RuntimeError : RuntimeInterrupt
{
public:
  RuntimeError(const RuntimeError& error);
  RuntimeError(int code, String detail);
  RuntimeError(int code);
  int code;
  String message;
  String detail;
public_internal:
  RuntimeError();
  bool handled = false;
};

DOBASE_EXPORT size_t GetLastError(VmStack* stack);

DOBASE_EXPORT int GetLastErrorMessage(VmStack* stack, char *buf, size_t bufSize);

DOBASE_EXPORT int TranslateError(int code, char* buf, size_t bufSize);

DOBASE_EXPORT int TranslateErrorEx(int code, const String& extra, char* buf, size_t bufSize);

__NAMESPACE_END
