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

#include "Subfiles/WavSubfile.h"

#include "BinaryData.h"
#include "DatFile.h"
#include "DatException.h"
#include "SubfileData.h"

namespace LOTRO_DAT {
    WavSubfile::WavSubfile() = default;

    WavSubfile::WavSubfile(DatFile *dat, 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)
            : Subfile(dat, dictionary_offset, unknown1, file_id, file_offset, file_size,
                      timestamp, version, block_size, unknown2) {
    }

    FILE_TYPE WavSubfile::FileType() const {
        return WAV;
    }

    std::string WavSubfile::Extension() const {
        return ".wav";
    }

    SubfileData WavSubfile::PrepareForExport(const BinaryData &file_data) {
        SubfileData result;
        result.binary_data = file_data.CutData(8);
        result.options["fid"] = file_id();
        result.options["ext"] = Extension();
        return result;
    }

    BinaryData WavSubfile::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()) {
            throw DatException("Bad DdsSubfile::MakeForImport() - invalid options data!", IMPORT_EXCEPTION);
        }
        return old_data.CutData(0, 24) + data.binary_data;
    }
};