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

#ifndef LOTRO_DAT_PATCHER_DATFILE_H
#define LOTRO_DAT_PATCHER_DATFILE_H

#ifdef LOTRO_DAT_EXPORTS
#define LOTRO_DAT_API __declspec(dllexport)
#else
#define LOTRO_DAT_API __declspec(dllimport)
#endif

#include <fstream>
#include <map>
#include <unordered_map>
#include <set>

extern  "C++"
{
namespace LOTRO_DAT
{
    class BinaryData;
    class DatException;
    class SubDirectory;
    class Subfile;

    enum STATE {
        CLOSED,
        SUCCESS_OPENED,
        SUCCESS_SUPERBLOCK,
        SUCCESS_DIRECTORIES,
        SUCCESS_DICTIONARY,
        READY
    };

    class DatFile
    {
    public:
        DatFile();
        explicit DatFile(const char* filename);
        explicit DatFile(const std::string &filename);
        ~DatFile();

        void ReadData(BinaryData &data, long long size, long long offset = 0, long long data_offset = 0);
        void WriteData(const BinaryData &data, long long size, long long offset = 0, long long data_offset = 0);

        long long constant1() const;
        long long constant2() const;
        long long file_size() const;
        long long version1() const;
        long long version2() const;

        const std::unordered_map<long long, Subfile*> &dictionary();

    private:
        FILE *file_handler_;
        SubDirectory *root_directory_;
        std::unordered_map<long long, Subfile*> dictionary_;

        void OpenDatFile(const char* dat_name);
        void ReadSuperBlock();
        void MakeDirectories();
        void MakeDictionary();

        long long constant1_;
        long long constant2_;
        long long file_size_;
        long long version1_;
        long long version2_;
        long long root_directory_offset_;

        STATE dat_state_;
    };
}
}

#endif //LOTRO_DAT_PATCHER_DATFILE_H
