//
// Created by Иван_Архипов on 07.11.2017.
//
#include "SubDirectory.h"

#include "DatFile.h"
#include "Common/DatException.h"
#include "Subfile.h"
#include "BinaryData.h"

#include "Subfiles/TextSubfile.h"
#include "Subfiles/DdsSubfile.h"
#include "Subfiles/FontSubFile.h"
#include "Subfiles/JpgSubfile.h"
#include "Subfiles/OggSubfile.h"
#include "Subfiles/WavSubfile.h"
#include "Subfiles/UnknownSubfile.h"

namespace LOTRO_DAT {
    SubDirectory::SubDirectory() {
        offset_ = 0;
    }

    SubDirectory::~SubDirectory() {
        for (Subfile* file : subfiles_)
            delete file;
        for (SubDirectory *directory : subdirs_)
            delete directory;
    }

    SubDirectory::SubDirectory(long long offset, DatFile *dat, long long max_subdirs) :
            dat_(dat), offset_(offset), max_subdirs_(max_subdirs) {
        try {
            MakeSubDirectories();
        } catch (std::exception &e) {
            fprintf(stderr, "Caught %s exception.", e.what());
            
            fprintf(stderr, "Unable to initialize directory at offset %lld. Initializing it as empty directory...\n",
                    offset);
            subdirs_.clear();
            subfiles_.clear();
            return;
        }

        try {
            MakeSubFiles();
        } catch (std::exception &e) {
            fprintf(stderr, "Caught %s exception.", e.what());
            
            fprintf(stderr, "Unable to initialize directory at offset %lld. Initializing it as empty directory...\n",
                    offset);
            subdirs_.clear();
            subfiles_.clear();
            return;
        }
    }

    void SubDirectory::MakeSubDirectories() {
        BinaryData data(1024);
        dat_->ReadData(data, 63 * 8, offset_);
        if (data.ToNumber<4>(0) != 0 || data.ToNumber<4>(4) != 0) {
            std::string err =
                    std::string("Bad SubDirectory::MakeSubDirectories - first 8 bytes are not equal to 0 at offset ")
                    + std::to_string(offset_);
            throw DatException(err.c_str(), SUBDIR_EXCEPTION);
        }

        for (unsigned int i = 8; i < 63 * 8; i += 8) {
            if (data.ToNumber<4>(i) == 0 || data.ToNumber<4>(i + 4) == 0)
                break;

            try {
                SubDirectory *subdir = new SubDirectory(data.ToNumber<4>(i + 4), dat_);
                subdirs_.push_back(subdir);
            } catch (std::exception &e) {
                fprintf(stderr, "Caught %s exception.", e.what());
            
                fprintf(stderr, "Making SubDirectory at offset %lld failed, continuing\n", data.ToNumber<4>(i + 4));
            }
        }
    }

    void SubDirectory::MakeSubFiles() {
        BinaryData data(64 * 32);
        dat_->ReadData(data, 64 * 32, offset_ + 63 * 8);

        for (unsigned int i = 0; i < 61 * 32; i += 32) {
            if (data.ToNumber<4>(i + 8) < 0x32 || data.ToNumber<4>(i + 12) < 0x32)
                continue;

            BinaryData mfile_id(4);
            dat_->ReadData(mfile_id, 4, data.ToNumber<4>(i + 12) + 8);

            if (data.ToNumber<4>(i + 8) != mfile_id.ToNumber<4>(0)) {
                continue;
            }
            subfiles_.push_back(
                    MakeSubfile(
                            offset_ + 63 * 8 + i,
                            data.ToNumber<4>(i), // fragments_count
                            data.ToNumber<4>(i + 4), // unknown1
                            data.ToNumber<4>(i + 8), // file_id
                            data.ToNumber<4>(i + 12), // file_offset
                            data.ToNumber<4>(i + 16), // block_size
                            data.ToNumber<4>(i + 20), // timestamp
                            data.ToNumber<4>(i + 24), // version
                            data.ToNumber<4>(i + 28) // block_size
                    )
            );
        }
    }

    void SubDirectory::MakeDictionary(std::unordered_map<long long, Subfile *> &dict) {
        for (Subfile *i : subfiles_) {
            if (dict.count(i->file_id()) == 0 || dict[i->file_id()]->timestamp() < i->timestamp())
                dict[i->file_id()] = i;
        }

        for (SubDirectory *i : subdirs_)
            i->MakeDictionary(dict);
    }

    void SubDirectory::UpdateDirectories(std::unordered_map<long long, BinaryData *> &patched_files, std::unordered_map<long long, Subfile*> &dict) {
        for (unsigned i = 0; i < subfiles_.size(); i++) {
            long long file_id = subfiles_[i]->file_id();

            if (patched_files.count(file_id) != 0) {
                BinaryData data(32);
                dat_->ReadData(data, 32, subfiles_[i]->dictionary_offset());
                if (data.CutData(8, 16) != patched_files[file_id]->CutData(8, 16))
                    continue;

                BinaryData header = dict[subfiles_[i]->file_id()]->MakeHeaderData();
                BinaryData write_data(4);

                write_data.FromNumber<4>(dict[file_id]->file_offset());
                dat_->WriteData(write_data, 4, subfiles_[i]->dictionary_offset() + 12);

                write_data.FromNumber<4>(dict[file_id]->file_size());
                dat_->WriteData(write_data, 4, subfiles_[i]->dictionary_offset() + 16);

                write_data.FromNumber<4>(dict[file_id]->block_size());
                dat_->WriteData(write_data, 4, subfiles_[i]->dictionary_offset() + 28);
            }
        }

        for (SubDirectory *i : subdirs_)
            i->UpdateDirectories(patched_files, dict);
    }

    Subfile *SubDirectory::MakeSubfile(long long dictionary_offset, long long fragments_count, long long unknown1, long long file_id,
                                       long long file_offset, long long file_size, long long timestamp,
                                       long long version, long long block_size) {

        FILE_TYPE type = GetSubfileType(file_id, file_offset);

        switch (type) {
            case TEXT:
                return dynamic_cast<Subfile *>(new TextSubfile(dat_, dictionary_offset, fragments_count, unknown1, file_id, file_offset, file_size, timestamp, version, block_size));
            case JPG:
                return dynamic_cast<Subfile *>(new JpgSubfile(dat_, dictionary_offset, fragments_count, unknown1, file_id, file_offset, file_size, timestamp, version, block_size));
            case DDS:
                return dynamic_cast<Subfile *>(new DdsSubfile(dat_, dictionary_offset, fragments_count, unknown1, file_id, file_offset, file_size, timestamp, version, block_size));
            case WAV:
                return dynamic_cast<Subfile *>(new WavSubfile(dat_, dictionary_offset, fragments_count, unknown1, file_id, file_offset, file_size, timestamp, version, block_size));
            case OGG:
                return dynamic_cast<Subfile *>(new OggSubfile(dat_, dictionary_offset, fragments_count, unknown1, file_id, file_offset, file_size, timestamp, version, block_size));
            case FONT:
                return dynamic_cast<Subfile *>(new FontSubfile(dat_, dictionary_offset, fragments_count, unknown1, file_id, file_offset, file_size, timestamp, version, block_size));
            case UNKNOWN:
                return dynamic_cast<Subfile *>(new UnknownSubfile(dat_, dictionary_offset, fragments_count, unknown1, file_id, file_offset, file_size, timestamp, version, block_size));
        }
        throw DatException("Bad SubDirectory::MakeSubfile() - unable to recognize file_type!", SUBFILE_EXCEPTION);
    }

    FILE_TYPE SubDirectory::GetSubfileType(long long file_id, long long file_offset) const {
        // Text check based on file_id
        if ((file_id >> 24ll) == 0x25ll)
            return TEXT;

        // Font check based on file_id
        if ((file_id >> 24ll) == 0x42ll)
            return FONT;

        BinaryData header(64);
        try {
            dat_->ReadData(header, 64, (unsigned) file_offset + 8);
        }catch (DatException &e) {
            if (e.type() == READ_EXCEPTION) {
                std::string err =
                        "Bad Subfile::getExtension() - unable to read header of file with id = " +
                        std::to_string(file_id) +
                        " and offset = " + std::to_string(file_offset);
                throw DatException(err.c_str(), SUBFILE_EXCEPTION);
            } else
                throw e;
        }

        // jpeg / dds check
        if ((file_id >> 24ll) == 0x41ll) {
            long long soi = header.ToNumber<2>(24);
            long long marker = header.ToNumber<2>(26);

            //auto markerSize = header.ToNumber<short>(28);
            //auto four = header.ToNumber<int>(30);

            if ((soi == 0xD8FFll && marker == 0xE0FFll) || marker == 0xE1FFll)
                return JPG;
            return DDS;
        }

        // Ogg and Wav check
        if (header[8] == 0x4F && header[9] == 0x67 && header[10] == 0x67 && header[11] == 0x53)
            return OGG;
        if (header[8] == 0x52 && header[9] == 0x49 && header[10] == 0x46 && header[11] == 0x46)
            return WAV;

        return UNKNOWN;
    }
};