/*
 * SPDX-FileCopyrightText: 2019-2026 Mattia Basaglia <dev@dragon.best>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include "glaxnimate/module/extraformats/aep/aep_format.hpp"
#include "glaxnimate/module/extraformats/aep/aep_parser.hpp"
#include "glaxnimate/module/extraformats/aep/aep_loader.hpp"
#include "glaxnimate/module/extraformats/aep/aepx.hpp"

bool glaxnimate::io::aep::AepFormat::riff_to_document(const RiffChunk& chunk, model::Document* document, const QString& filename)
{
    AepParser parser(this);
    try {
        Project project = parser.parse(chunk);
        QFileInfo finfo(filename);
        AepLoader loader(document, project, finfo.dir(), this);
        loader.load_project();
        return true;
    } catch ( const AepError& err ) {
        return false;
    }
}

bool glaxnimate::io::aep::AepFormat::on_open(QIODevice& file, const QString& filename, model::Document* document, const QVariantMap&)
{
    AepRiff riff_parser;
    try {
        RiffChunk chunk = riff_parser.parse(&file);
        return riff_to_document(chunk, document, filename);
    } catch ( const RiffError& r ) {
        error(i18n("Could not load file: %1", r.message));
        return false;
    }
}

bool glaxnimate::io::aep::AepxFormat::on_open(QIODevice& file, const QString& filename, model::Document* document, const QVariantMap&)
{
    QDomDocument dom;
    dom.setContent(file.readAll());
    AepxConverter aepx;
    return riff_to_document(aepx.aepx_to_chunk(dom.documentElement()), document, filename);
}
