// Copyright 2022, FOSS-VG Developers and Contributers
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// version 3 along with this program.
// If not, see https://www.gnu.org/licenses/agpl-3.0.en.html

#pragma once

#include <cstdint>

template <typename T>
struct ErrorOr {
    bool isError;
    uint8_t errorCode;
    T value;

    ErrorOr() {
        this->isError = false;
        this->errorCode = 0;
    }

    ErrorOr(T value) {
        this->isError = false;
        this->errorCode = 0;
        this->value = value;
    }

    ErrorOr(bool isError, uint8_t errorCode) {
        this->isError = isError;
        this->errorCode = errorCode;
    }

    ErrorOr(bool isError, uint8_t errorCode, T value) {
        this->isError = isError;
        this->errorCode = errorCode;
        this->value = value;
    }
};

struct ErrorOrVoid {
    bool isError;
    uint8_t errorCode;

    ErrorOrVoid() {
        this->isError = false;
        this->errorCode = 0;
    }

    ErrorOrVoid(bool isError, uint8_t errorCode) {
        this->isError = isError;
        this->errorCode = errorCode;
    }
};

namespace ErrorCodes {
    // These are all arbitrary values used to assign error codes to different
    // kinds of errors.
    // Using them is optional as ErrorOr<> accepts any uint8_t value as
    // error code, but they are useful for readability.

    // Ahh yes, very useful.
    const uint8_t SUCCESS = 0;

    const uint8_t OUT_OF_RANGE = 1;
    // like OUT_OF_RANGE but when going out of bounds in a non-predetermined way
    const uint8_t OVERRUN = 2;

    const uint8_t NOT_PRESENT = 3;

    const uint8_t WRONG_USAGE = 4;

    // when dealing with maps
    const uint8_t UNKNOWN_KEY = 5;

    //mismatched size in java strings
    const uint8_t MISMATCHEDSIZE = 6;

    const uint8_t NOT_YET_KNOWN = 7;

    const uint8_t INVALID_TYPE = 8;

    const uint8_t FILE_NOT_OPEN = 9;
    const uint8_t FILE_NOT_FOUND = 10;

    // when performing an operation that would technically be valid but must
    // never be performed (like deleting an end tag from an NBT compound)
    const uint8_t NOT_ALLOWED = 11;

    const uint8_t MIXED_TYPES = 12;

    // when too much data is available
    const uint8_t OVERFLOW = 13;

    const uint8_t UNIMPLEMENTED = 254;

    const uint8_t UNKNOWN = 255;
}
