//
// Created by Иван_Архипов on 31.10.2017.
//
#ifndef LOTRO_DAT_PATCHER_DATEXCEPTION_H
#define LOTRO_DAT_PATCHER_DATEXCEPTION_H

//#define DAT_DEBUG

#include <string>
#include <iostream>
#include <cstring>
#include "EasyLogging++/easylogging++.h"

extern  "C++"
{
namespace LOTRO_DAT
{
    enum DAT_EXCEPTION_TYPE {
        READ_EXCEPTION,
        WRITE_EXCEPTION,
        SUBDIR_EXCEPTION,
        SUBFILE_EXCEPTION,
        IMPORT_EXCEPTION,
        EXPORT_EXCEPTION,
        DATA_EXCEPTION,
        DATABASE_EXCEPTION,
        UNKNOWN_EXCEPTION
    };

    class DatException : public std::exception {
    public:
        DatException()
                : msg_(const_cast<char *>("Unknown DatException has been raised!"))
                , type_(UNKNOWN_EXCEPTION)
        {
            LOG(ERROR) << "THROWN DatException " << std::string(msg_);
        }

        explicit DatException(const char *msg, DAT_EXCEPTION_TYPE type = UNKNOWN_EXCEPTION) noexcept
                : std::exception()
                , type_(type)
        {
            msg_ = new char[strlen(msg) + 1];
            memcpy(msg_, msg, strlen(msg) + 1);
            LOG(ERROR) << "THROWN DatException " << std::string(msg_);
            #ifdef DAT_DEBUG
                fprintf(stderr, "%s\n", msg_);
            #endif
        }

        ~DatException() override
        {
            delete[] msg_;
        }

        const char* what() const noexcept override
        {
            return (std::string("DatException ") + std::string(msg_)).c_str();
        }

        DAT_EXCEPTION_TYPE type() const
        {
            return type_;
        }
    private:
        char *msg_;
        const DAT_EXCEPTION_TYPE type_;
    };
}
}
#endif //LOTRO_DAT_PATCHER_DATEXCEPTION_H
