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

#include "DatFile.h"
#include "SubFile.h"
#include "BinaryData.h"
#include "SubfileData.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::unordered_set<long long> SubDirectory::visited_subdirectories_ = std::unordered_set<long long>();
    std::unordered_set<long long> SubDirectory::visited_subfiles_ = std::unordered_set<long long>();
    std::set<std::shared_ptr<SubDirectory>, SubDirectory::SubDirectoryOffsetComp> SubDirectory::subdir_init_queue_
           = std::set<std::shared_ptr<SubDirectory>, SubDirectory::SubDirectoryOffsetComp>();
    std::set<std::shared_ptr<SubDirectory>, SubDirectory::SubDirectoryOffsetComp> SubDirectory::subfile_init_queue_
            = std::set<std::shared_ptr<SubDirectory>, SubDirectory::SubDirectoryOffsetComp>();


    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) {
    }

    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));

            std::shared_ptr<SubDirectory> subdir = std::make_shared<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);
                subdir_init_queue_.insert(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;
        }

        subfiles_.resize(unsigned(subfiles_number), nullptr);
        BinaryData headers(32 * unsigned(subfiles_number));
        dat_.ReadData(headers, 32 * subfiles_number, offset_ + 63 * 8 + 4);

        for (int i = 0; i < subfiles_number; i++) {
            BinaryData header = headers.CutData(32 * i, 32 * (i + 1));

            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;

            if (visited_subfiles_.count(header.ToNumber<4>(4))) {
                LOG(ERROR) << "Found duplicate file with id " << header.ToNumber<4>(4);
                break;
            }

            subfiles_[i] = 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(subfiles_[i])) {
                visited_subfiles_.insert(subfiles_[i]->file_id());
            } else {
                LOG(WARNING) << "Incorrect SubFile in directory at offset " << offset_ + 63 * 8 + 4 + 32 * i << " (id = " << subfiles_[i]->file_id() << ");";
                subfiles_[i] = nullptr;
                break;
            }
        }
        return true;
    }

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

        for (const auto &dir : subdirs_) {
            dir->MakeDictionary(dict);
        }
    }

    std::shared_ptr<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 std::dynamic_pointer_cast<SubFile>(std::make_shared<TextSubFile>(dat_, dictionary_offset, unknown1, file_id, file_offset, file_size, timestamp, version, block_size, unknown2));
            case JPG:
                return std::dynamic_pointer_cast<SubFile>(std::make_shared<JpgSubFile>(dat_, dictionary_offset, unknown1, file_id, file_offset, file_size, timestamp, version, block_size, unknown2));
            case DDS:
                return std::dynamic_pointer_cast<SubFile>(std::make_shared<DdsSubFile>(dat_, dictionary_offset, unknown1, file_id, file_offset, file_size, timestamp, version, block_size, unknown2));
            case WAV:
                return std::dynamic_pointer_cast<SubFile>(std::make_shared<WavSubFile>(dat_, dictionary_offset, unknown1, file_id, file_offset, file_size, timestamp, version, block_size, unknown2));
            case OGG:
                return std::dynamic_pointer_cast<SubFile>(std::make_shared<OggSubFile>(dat_, dictionary_offset, unknown1, file_id, file_offset, file_size, timestamp, version, block_size, unknown2));
            case FONT:
                return std::dynamic_pointer_cast<SubFile>(std::make_shared<FontSubFile>(dat_, dictionary_offset, unknown1, file_id, file_offset, file_size, timestamp, version, block_size, unknown2));
            case UNKNOWN:
                return std::dynamic_pointer_cast<SubFile>(std::make_shared<UnknownSubFile>(dat_, dictionary_offset, unknown1, file_id, file_offset, file_size, timestamp, version, block_size, unknown2));
        }
        LOG(ERROR) << "Incorrect file type..";
        return std::dynamic_pointer_cast<SubFile>(std::make_shared<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;
    }

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