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

#include "DatFile.h"
#include "Subfile.h"
#include "BinaryData.h"
#include "EasyLogging++/easylogging++.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 {
    std::set<long long> SubDirectory::visited_subdirectories_ = std::set<long long>();

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

    SubDirectory::~SubDirectory() {
        subfiles_.clear();
        subdirs_.clear();
    }

    SubDirectory::SubDirectory(long long offset, DatFile *dat, long long max_subdirs) :
            dat_(dat), offset_(offset), max_subdirs_(max_subdirs) {
        LOG(DEBUG) << "Initialising " << offset_ << " SubDirectory";
        LOG(DEBUG) << "Processing SubDirectories";

        if (!MakeSubDirectories()) {
            subfiles_.clear();
            subdirs_.clear();
            return;
        }

        LOG(DEBUG) << "Processing SubFiles";
        if (!MakeSubFiles()) {
            subfiles_.clear();
            subdirs_.clear();
            return;
        }
        LOG(DEBUG) << "SubDirectory " << offset_ << " initialized successfully";
    }

    bool SubDirectory::MakeSubDirectories() {
        BinaryData data(1024);
        dat_->ReadData(data, 63 * 8, offset_);

        if (data.Empty()) {
            LOG(ERROR) << "(READ ERROR) Incorrect directory at offset " << offset_;
            return false;
        }

        if (data.ToNumber<4>(0) != 0 || data.ToNumber<4>(4) != 0) {
            LOG(DEBUG) << "first 8 bytes are not equal to 0 at offset " << offset_;
            return false;
        }

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

            if (visited_subdirectories_.count(data.ToNumber<4>(i + 4)) > 0) {
                LOG(DEBUG) << "Visiting subdirectory at offset " << data.ToNumber<4>(i + 4) << " more than one time. Passing.";
                continue;
            }
            visited_subdirectories_.insert(data.ToNumber<4>(i + 4));

            SubDirectory *subdir = new SubDirectory(data.ToNumber<4>(i + 4), dat_);

            if (subdir->subfiles_.empty() && subdir->subdirs_.empty()) {
                LOG(WARNING) << "Sub-subdirectory is empty or made empty... Dictionary offset = " << offset_ + i << "; Passing others";
                //break;
            } else
                subdirs_.push_back(subdir);
        }
        return true;
    }

    bool SubDirectory::MakeSubFiles() {
        BinaryData data = BinaryData(4);
        dat_->ReadData(data, 4, offset_ + 63 * 8);

        if (data.Empty()) {
            LOG(ERROR) << "(READ ERROR) Incorrect directory at offset " << offset_;
            return false;
        }

        auto subfiles_number = data.ToNumber<4>(0);
        if (subfiles_number >= 64) {
            LOG(ERROR) << "Incorrect directory (subfiles_num >= 64) at offset " << offset_;
            return false;
        }

        for (int i = 0; i < subfiles_number; i++) {
            BinaryData header(32);
            dat_->ReadData(header, 32, offset_ + 63 * 8 + 4 + 32 * i);

            if (header.Empty()) {
                LOG(ERROR) << "(READ ERROR) Incorrect directory (unable to read subfile data) at offset " << offset_;
                return false;
            }

            if (header.ToNumber<4>(20) == 0 || header.ToNumber<4>(28) != 0)
                continue;

            Subfile *subfile = MakeSubfile(
                    offset_ + 63 * 8 + 4 + 32 * i,
                    header.ToNumber<4>(0), // unknown1
                    header.ToNumber<4>(4), // file_id
                    header.ToNumber<4>(8), // file_offset
                    header.ToNumber<4>(12), // file_size
                    header.ToNumber<4>(16), // timestamp
                    header.ToNumber<4>(20), // version
                    header.ToNumber<4>(24), // block_size
                    header.ToNumber<4>(28) // unknown2 - must be zero??
            );

            if (dat_->CorrectSubfile(subfile)) {
                subfiles_.emplace_back(subfile);
            } else {
                LOG(WARNING) << "Incorrect Subfile in directory at offset " << offset_ + 63 * 8 + 4 + 32 * i << " (id = " << subfile->file_id() << ");";
                //break;
            }
        }
        return true;
    }

    void SubDirectory::MakeDictionary(std::map<long long, Subfile *> &dict) {
        for (Subfile *i : subfiles_) {
            if (dict.count(i->file_id() != 0)) {
                LOG(WARNING) << "Found multiple instances of file " << i->file_id() << " at dictionary offset "
                             << i->dictionary_offset() << ". Base offset = " << dict[i->file_id()]->dictionary_offset();
                continue;
            }
            dict[i->file_id()] = i;
        }

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

    Subfile *SubDirectory::MakeSubfile(long long dictionary_offset, 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, long long unknown2) {

        FILE_TYPE type = GetSubfileType(file_id, file_offset);

        switch (type) {
            case TEXT:
                return dynamic_cast<Subfile *>(new TextSubfile(dat_, dictionary_offset, unknown1, file_id, file_offset, file_size, timestamp, version, block_size, unknown2));
            case JPG:
                return dynamic_cast<Subfile *>(new JpgSubfile(dat_, dictionary_offset, unknown1, file_id, file_offset, file_size, timestamp, version, block_size, unknown2));
            case DDS:
                return dynamic_cast<Subfile *>(new DdsSubfile(dat_, dictionary_offset, unknown1, file_id, file_offset, file_size, timestamp, version, block_size, unknown2));
            case WAV:
                return dynamic_cast<Subfile *>(new WavSubfile(dat_, dictionary_offset, unknown1, file_id, file_offset, file_size, timestamp, version, block_size, unknown2));
            case OGG:
                return dynamic_cast<Subfile *>(new OggSubfile(dat_, dictionary_offset, unknown1, file_id, file_offset, file_size, timestamp, version, block_size, unknown2));
            case FONT:
                return dynamic_cast<Subfile *>(new FontSubfile(dat_, dictionary_offset, unknown1, file_id, file_offset, file_size, timestamp, version, block_size, unknown2));
            case UNKNOWN:
                return dynamic_cast<Subfile *>(new UnknownSubfile(dat_, dictionary_offset, unknown1, file_id, file_offset, file_size, timestamp, version, block_size, unknown2));
        }
        LOG(ERROR) << "Incorrect file type..";
        return dynamic_cast<Subfile *>(new UnknownSubfile(dat_, dictionary_offset, unknown1, file_id, file_offset, file_size, timestamp, version, block_size, unknown2));
    }

    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);
        dat_->ReadData(header, 64, (unsigned) file_offset + 8);

        if (header.Empty()) {
            LOG(ERROR) << "Unable to read file header. file_id = " << file_id << ", offset = " << offset_;
            return UNKNOWN;
        }


        // 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;
    }
};