//
// Created by kikab on 04.06.2018.
//

#ifndef LOTRO_DAT_LIBRARY_DATIO_H
#define LOTRO_DAT_LIBRARY_DATIO_H

#include <utility>
#include "DatOperationResult.h"

extern "C++" {
namespace LOTRO_DAT {
    class BinaryData;

    class DatFile;

    class SubDirectory;

    class SubFile;


    class DatIO {
    public:
        DatIO() = delete;
        DatIO(const DatIO &other) = delete;
        DatIO& operator=(const DatIO &other) = delete;
        ~DatIO() = default;

        explicit DatIO(DatFile *datFilePtr);

        DatOperationResult<> Init(const std::string &filename);

        DatOperationResult<> ReadData(BinaryData &data, long long size, long long offset = 0, long long data_offset = 0) const;

        DatOperationResult<> WriteData(const BinaryData &data, long long size, long long offset = 0, long long data_offset = 0);

        DatOperationResult<long long> GetActualDatSize();

        DatOperationResult<std::string> GetFilename();

        void PrintInformaion(FILE* file);

        DatOperationResult<> DeInit();

    private:

        //------------------------------------------------//
        // PRIVATE INIT SECTION
        //------------------------------------------------//

        DatOperationResult<> OpenDatFile();

        DatOperationResult<> ReadSuperBlock();

        //------------------------------------------------//
        // PRIVATE DEINIT SECTION
        //------------------------------------------------//

        DatOperationResult<> ModifyFragmentationJournal();

        DatOperationResult<> UpdateHeader();

    private:
        DatFile *dat;
        FILE *file_handler_;
        std::string filename_;

    public:
        long long constant1;
        long long constant2;
        long long file_size;
        long long version1;
        long long version2;
        long long fragmentation_journal_size;
        long long fragmentation_journal_end;
        long long root_directory_offset;
        long long fragmentation_journal_offset;
        long long free_dat_size;

    private:
        long long actual_dat_size_;
    };
}
};
#endif //LOTRO_DAT_LIBRARY_DATIO_H

