import enum
from typing import Optional

from asynctnt.iproto.protocol import IProtoError


class TarantoolError(Exception):
    """
    Base Tarantool Exception class
    """

    pass


class TarantoolSchemaError(TarantoolError):
    """
    Exception is raised when any problems with schema occurred
    """

    pass


class TarantoolDatabaseError(TarantoolError):
    """
    Exception is raised when Tarantool responds with code != 0
    """

    def __init__(self, code: int, message: str, error: Optional[IProtoError]):
        super(TarantoolDatabaseError, self).__init__(message)
        self.code = code
        self.message = message
        self.error = error


class TarantoolNetworkError(TarantoolError):
    pass


class TarantoolNotConnectedError(TarantoolNetworkError):
    """
    Raised when asynctnt is not connected to Tarantool
    """

    pass


class ErrorCode(enum.IntEnum):
    """
    Tarantool default error codes
    """

    ER_UNKNOWN = 0
    ER_ILLEGAL_PARAMS = 1
    ER_MEMORY_ISSUE = 2
    ER_TUPLE_FOUND = 3
    ER_TUPLE_NOT_FOUND = 4
    ER_UNSUPPORTED = 5
    ER_NONMASTER = 6
    ER_READONLY = 7
    ER_INJECTION = 8
    ER_CREATE_SPACE = 9
    ER_SPACE_EXISTS = 10
    ER_DROP_SPACE = 11
    ER_ALTER_SPACE = 12
    ER_INDEX_TYPE = 13
    ER_MODIFY_INDEX = 14
    ER_LAST_DROP = 15
    ER_TUPLE_FORMAT_LIMIT = 16
    ER_DROP_PRIMARY_KEY = 17
    ER_KEY_PART_TYPE = 18
    ER_EXACT_MATCH = 19
    ER_INVALID_MSGPACK = 20
    ER_PROC_RET = 21
    ER_TUPLE_NOT_ARRAY = 22
    ER_FIELD_TYPE = 23
    ER_INDEX_PART_TYPE_MISMATCH = 24
    ER_UPDATE_SPLICE = 25
    ER_UPDATE_ARG_TYPE = 26
    ER_FORMAT_MISMATCH_INDEX_PART = 27
    ER_UNKNOWN_UPDATE_OP = 28
    ER_UPDATE_FIELD = 29
    ER_FUNCTION_TX_ACTIVE = 30
    ER_KEY_PART_COUNT = 31
    ER_PROC_LUA = 32
    ER_NO_SUCH_PROC = 33
    ER_NO_SUCH_TRIGGER = 34
    ER_NO_SUCH_INDEX_ID = 35
    ER_NO_SUCH_SPACE = 36
    ER_NO_SUCH_FIELD_NO = 37
    ER_EXACT_FIELD_COUNT = 38
    ER_FIELD_MISSING = 39
    ER_WAL_IO = 40
    ER_MORE_THAN_ONE_TUPLE = 41
    ER_ACCESS_DENIED = 42
    ER_CREATE_USER = 43
    ER_DROP_USER = 44
    ER_NO_SUCH_USER = 45
    ER_USER_EXISTS = 46
    ER_PASSWORD_MISMATCH = 47
    ER_UNKNOWN_REQUEST_TYPE = 48
    ER_UNKNOWN_SCHEMA_OBJECT = 49
    ER_CREATE_FUNCTION = 50
    ER_NO_SUCH_FUNCTION = 51
    ER_FUNCTION_EXISTS = 52
    ER_BEFORE_REPLACE_RET = 53
    ER_MULTISTATEMENT_TRANSACTION = 54
    ER_TRIGGER_EXISTS = 55
    ER_USER_MAX = 56
    ER_NO_SUCH_ENGINE = 57
    ER_RELOAD_CFG = 58
    ER_CFG = 59
    ER_SAVEPOINT_EMPTY_TX = 60
    ER_NO_SUCH_SAVEPOINT = 61
    ER_UNKNOWN_REPLICA = 62
    ER_REPLICASET_UUID_MISMATCH = 63
    ER_INVALID_UUID = 64
    ER_REPLICASET_UUID_IS_RO = 65
    ER_INSTANCE_UUID_MISMATCH = 66
    ER_REPLICA_ID_IS_RESERVED = 67
    ER_INVALID_ORDER = 68
    ER_MISSING_REQUEST_FIELD = 69
    ER_IDENTIFIER = 70
    ER_DROP_FUNCTION = 71
    ER_ITERATOR_TYPE = 72
    ER_REPLICA_MAX = 73
    ER_INVALID_XLOG = 74
    ER_INVALID_XLOG_NAME = 75
    ER_INVALID_XLOG_ORDER = 76
    ER_NO_CONNECTION = 77
    ER_TIMEOUT = 78
    ER_ACTIVE_TRANSACTION = 79
    ER_CURSOR_NO_TRANSACTION = 80
    ER_CROSS_ENGINE_TRANSACTION = 81
    ER_NO_SUCH_ROLE = 82
    ER_ROLE_EXISTS = 83
    ER_CREATE_ROLE = 84
    ER_INDEX_EXISTS = 85
    ER_SESSION_CLOSED = 86
    ER_ROLE_LOOP = 87
    ER_GRANT = 88
    ER_PRIV_GRANTED = 89
    ER_ROLE_GRANTED = 90
    ER_PRIV_NOT_GRANTED = 91
    ER_ROLE_NOT_GRANTED = 92
    ER_MISSING_SNAPSHOT = 93
    ER_CANT_UPDATE_PRIMARY_KEY = 94
    ER_UPDATE_INTEGER_OVERFLOW = 95
    ER_GUEST_USER_PASSWORD = 96
    ER_TRANSACTION_CONFLICT = 97
    ER_UNSUPPORTED_PRIV = 98
    ER_LOAD_FUNCTION = 99
    ER_FUNCTION_LANGUAGE = 100
    ER_RTREE_RECT = 101
    ER_PROC_C = 102
    ER_UNKNOWN_RTREE_INDEX_DISTANCE_TYPE = 103
    ER_PROTOCOL = 104
    ER_UPSERT_UNIQUE_SECONDARY_KEY = 105
    ER_WRONG_INDEX_RECORD = 106
    ER_WRONG_INDEX_PARTS = 107
    ER_WRONG_INDEX_OPTIONS = 108
    ER_WRONG_SCHEMA_VERSION = 109
    ER_MEMTX_MAX_TUPLE_SIZE = 110
    ER_WRONG_SPACE_OPTIONS = 111
    ER_UNSUPPORTED_INDEX_FEATURE = 112
    ER_VIEW_IS_RO = 113
    ER_NO_TRANSACTION = 114
    ER_SYSTEM = 115
    ER_LOADING = 116
    ER_CONNECTION_TO_SELF = 117
    ER_KEY_PART_IS_TOO_LONG = 118
    ER_COMPRESSION = 119
    ER_CHECKPOINT_IN_PROGRESS = 120
    ER_SUB_STMT_MAX = 121
    ER_COMMIT_IN_SUB_STMT = 122
    ER_ROLLBACK_IN_SUB_STMT = 123
    ER_DECOMPRESSION = 124
    ER_INVALID_XLOG_TYPE = 125
    ER_ALREADY_RUNNING = 126
    ER_INDEX_FIELD_COUNT_LIMIT = 127
    ER_LOCAL_INSTANCE_ID_IS_READ_ONLY = 128
    ER_BACKUP_IN_PROGRESS = 129
    ER_READ_VIEW_ABORTED = 130
    ER_INVALID_INDEX_FILE = 131
    ER_INVALID_RUN_FILE = 132
    ER_INVALID_VYLOG_FILE = 133
    ER_CASCADE_ROLLBACK = 134
    ER_VY_QUOTA_TIMEOUT = 135
    ER_PARTIAL_KEY = 136
    ER_TRUNCATE_SYSTEM_SPACE = 137
    ER_LOAD_MODULE = 138
    ER_VINYL_MAX_TUPLE_SIZE = 139
    ER_WRONG_DD_VERSION = 140
    ER_WRONG_SPACE_FORMAT = 141
    ER_CREATE_SEQUENCE = 142
    ER_ALTER_SEQUENCE = 143
    ER_DROP_SEQUENCE = 144
    ER_NO_SUCH_SEQUENCE = 145
    ER_SEQUENCE_EXISTS = 146
    ER_SEQUENCE_OVERFLOW = 147
    ER_NO_SUCH_INDEX_NAME = 148
    ER_SPACE_FIELD_IS_DUPLICATE = 149
    ER_CANT_CREATE_COLLATION = 150
    ER_WRONG_COLLATION_OPTIONS = 151
    ER_NULLABLE_PRIMARY = 152
    ER_NO_SUCH_FIELD_NAME_IN_SPACE = 153
    ER_TRANSACTION_YIELD = 154
    ER_NO_SUCH_GROUP = 155
    ER_SQL_BIND_VALUE = 156
    ER_SQL_BIND_TYPE = 157
    ER_SQL_BIND_PARAMETER_MAX = 158
    ER_SQL_EXECUTE = 159
    ER_UPDATE_DECIMAL_OVERFLOW = 160
    ER_SQL_BIND_NOT_FOUND = 161
    ER_ACTION_MISMATCH = 162
    ER_VIEW_MISSING_SQL = 163
    ER_FOREIGN_KEY_CONSTRAINT = 164
    ER_NO_SUCH_MODULE = 165
    ER_NO_SUCH_COLLATION = 166
    ER_CREATE_FK_CONSTRAINT = 167
    ER_DROP_FK_CONSTRAINT = 168
    ER_NO_SUCH_CONSTRAINT = 169
    ER_CONSTRAINT_EXISTS = 170
    ER_SQL_TYPE_MISMATCH = 171
    ER_ROWID_OVERFLOW = 172
    ER_DROP_COLLATION = 173
    ER_ILLEGAL_COLLATION_MIX = 174
    ER_SQL_NO_SUCH_PRAGMA = 175
    ER_SQL_CANT_RESOLVE_FIELD = 176
    ER_INDEX_EXISTS_IN_SPACE = 177
    ER_INCONSISTENT_TYPES = 178
    ER_SQL_SYNTAX_WITH_POS = 179
    ER_SQL_STACK_OVERFLOW = 180
    ER_SQL_SELECT_WILDCARD = 181
    ER_SQL_STATEMENT_EMPTY = 182
    ER_SQL_KEYWORD_IS_RESERVED = 183
    ER_SQL_SYNTAX_NEAR_TOKEN = 184
    ER_SQL_UNKNOWN_TOKEN = 185
    ER_SQL_PARSER_GENERIC = 186
    ER_SQL_ANALYZE_ARGUMENT = 187
    ER_SQL_COLUMN_COUNT_MAX = 188
    ER_HEX_LITERAL_MAX = 189
    ER_INT_LITERAL_MAX = 190
    ER_SQL_PARSER_LIMIT = 191
    ER_INDEX_DEF_UNSUPPORTED = 192
    ER_CK_DEF_UNSUPPORTED = 193
    ER_MULTIKEY_INDEX_MISMATCH = 194
    ER_CREATE_CK_CONSTRAINT = 195
    ER_CK_CONSTRAINT_FAILED = 196
    ER_SQL_COLUMN_COUNT = 197
    ER_FUNC_INDEX_FUNC = 198
    ER_FUNC_INDEX_FORMAT = 199
    ER_FUNC_INDEX_PARTS = 200
    ER_NO_SUCH_FIELD_NAME = 201
    ER_FUNC_WRONG_ARG_COUNT = 202
    ER_BOOTSTRAP_READONLY = 203
    ER_SQL_FUNC_WRONG_RET_COUNT = 204
    ER_FUNC_INVALID_RETURN_TYPE = 205
    ER_SQL_PARSER_GENERIC_WITH_POS = 206
    ER_REPLICA_NOT_ANON = 207
    ER_CANNOT_REGISTER = 208
    ER_SESSION_SETTING_INVALID_VALUE = 209
    ER_SQL_PREPARE = 210
    ER_WRONG_QUERY_ID = 211
    ER_SEQUENCE_NOT_STARTED = 212
    ER_NO_SUCH_SESSION_SETTING = 213
    ER_UNCOMMITTED_FOREIGN_SYNC_TXNS = 214
    ER_SYNC_MASTER_MISMATCH = 215
    ER_SYNC_QUORUM_TIMEOUT = 216
    ER_SYNC_ROLLBACK = 217
    ER_TUPLE_METADATA_IS_TOO_BIG = 218
    ER_XLOG_GAP = 219
    ER_TOO_EARLY_SUBSCRIBE = 220
    ER_SQL_CANT_ADD_AUTOINC = 221
    ER_QUORUM_WAIT = 222
    ER_INTERFERING_PROMOTE = 223
    ER_ELECTION_DISABLED = 224
    ER_TXN_ROLLBACK = 225
    ER_NOT_LEADER = 226
    ER_SYNC_QUEUE_UNCLAIMED = 227
    ER_SYNC_QUEUE_FOREIGN = 228
    ER_UNABLE_TO_PROCESS_IN_STREAM = 229
    ER_UNABLE_TO_PROCESS_OUT_OF_STREAM = 230
    ER_TRANSACTION_TIMEOUT = 231
    ER_ACTIVE_TIMER = 232
    ER_TUPLE_FIELD_COUNT_LIMIT = 233
    ER_CREATE_CONSTRAINT = 234
    ER_FIELD_CONSTRAINT_FAILED = 235
    ER_TUPLE_CONSTRAINT_FAILED = 236
    ER_CREATE_FOREIGN_KEY = 237
    ER_FOREIGN_KEY_INTEGRITY = 238
    ER_FIELD_FOREIGN_KEY_FAILED = 239
    ER_COMPLEX_FOREIGN_KEY_FAILED = 240
    ER_WRONG_SPACE_UPGRADE_OPTIONS = 241
    ER_NO_ELECTION_QUORUM = 242
    ER_SSL = 243
    ER_SPLIT_BRAIN = 244
