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

#include "Subfiles/JpgSubFile.h"
#include "EasyLogging++/easylogging++.h"
#include "BinaryData.h"
#include "DatFile.h"
#include "SubfileData.h"

namespace LOTRO_DAT {
    JpgSubFile::JpgSubFile(SubFile preinit_file) : SubFile(preinit_file) {
    }

    FILE_TYPE JpgSubFile::FileType() const {
        return JPG;
    }

    std::string JpgSubFile::Extension() const {
        return std::string(".jpg");
    }

    SubfileData JpgSubFile::PrepareForExport(const BinaryData &file_data) {
        if (file_data.Empty()) {
            return SubfileData();
        }

        SubfileData result;
        result.binary_data = file_data.CutData(24);
        result.options["fid"] = file_id();
        result.options["ext"] = Extension();
        return result;
    }

    BinaryData JpgSubFile::MakeForImport(const BinaryData &old_data, const SubfileData &data) {
        if (!data.options["ext"] || data.options["ext"].as<std::string>() != Extension() ||
            !data.options["fid"] || data.options["fid"].as<long long>() != file_id()) {
            LOG(ERROR) << "invalid options data!";
            return BinaryData(0);
        }
        BinaryData file_size = BinaryData::FromNumber<4>(data.binary_data.size());
        return old_data.CutData(0, 28) + file_size + data.binary_data;
    }
};