/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | www.openfoam.com
     \\/     M anipulation  |
-------------------------------------------------------------------------------
    Copyright (C) 2018-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

Application
    Test-vtmWriter

Description
    Basic functionality tests for vtk::vtmWriter

\*---------------------------------------------------------------------------*/

#include "foamVtmWriter.H"

using namespace Foam;

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

int main(int argc, char *argv[])
{
    vtk::vtmWriter writer1;
    {
        fileName base = "region1_0001";

        writer1.beginBlock("internal");
        writer1.append_ugrid
        (
            base/"internal"
        );
        writer1.endBlock("internal");

        {
            writer1.beginBlock("boundary");
            writer1.append_poly
            (
                base/"patch0"
            );
            writer1.append("");  // bad entry
            writer1.append_poly
            (
                base/"patch1"
            );
            writer1.append_poly
            (
                base/"patch2"
            );
        }

        writer1.endBlock("boundary");

        {
            writer1.beginBlock("empty");
            writer1.endBlock("empty");
        }
        {
            writer1.beginBlock("dangling1");
            writer1.beginBlock("dangling2");
        }
    }

    Info<< nl << "vtm information" << nl;
    writer1.dump(Info),
    Info<< nl;

//    writer1.repair();
//
//   Info<< nl << "vtm information - after repair" << nl;
//    writer1.dump(Info),
//    Info<< nl;

    writer1.repair(true);

//    Info<< nl << "vtm information - after repair(collapse)" << nl;
//    writer1.dump(Info),
//    Info<< nl;
//
//    Info<< nl << "vtm information - after repair(collapse)" << nl;
//    writer1.dump(Info),
//    Info<< nl;

    Info<< nl << "Write to file" << nl;
    writer1.write("vtmWriter1.vtm");


    vtk::vtmWriter writer2;
    {
        fileName base = "region2_0001";

        writer2.beginBlock("internal");
        writer2.append_ugrid
        (
            base/"internal"
        );
        writer2.endBlock("internal");

        {
            writer2.beginBlock("boundary");
            writer2.append_poly
            (
                base/"patch0"
            );
            writer2.append("");  // bad entry
            writer2.append_poly
            (
                base/"patch1"
            );
            writer2.append_poly
            (
                base/"patch2"
            );
        }

        writer2.endBlock("boundary");

        // These should be automatically skipped
        writer2.endBlock();
        writer2.endBlock();
        writer2.endBlock();
        writer2.endBlock();
    }

    Info<< nl << "vtm information" << nl;
    writer2.dump(Info);

    writer2.repair(true);


    vtk::vtmWriter writer3;

    writer3.add("some-region1", writer1);
    writer3.add("some-region2", writer2);

    Info<< nl << "Combined:" << nl;
    writer3.dump(Info);

    Info<< nl << "XML:" << nl;
    writer3.write_xml(Info);

    // With hdf names
    {
        vtk::vtmWriter vtmMultiRegion;

        for (const word& regionName : { "fluid", "solid" })
        {
            // Combined internal + boundary in a vtm file
            vtk::vtmWriter vtmWriter;

            // No sub-block for internal
            vtmWriter.append_hdf<vtk::fileTag::UNSTRUCTURED_GRID>
            (
                "internal",
                regionName/"internal"
            );

            // Collect individual boundaries into a vtm file
            vtk::vtmWriter vtmBoundaries;

            vtmWriter.beginBlock("boundary");
            vtmBoundaries.beginBlock("boundary");

            vtmWriter.append_hdf<vtk::fileTag::POLY_DATA>
            (
                "patch0",
                regionName/"boundary"/"patch0",
            );
            vtmBoundaries.append_hdf<vtk::fileTag::POLY_DATA>
            (
                "patch0",
                "boundary/patch0"
            );

            vtmWriter.append_hdf<vtk::fileTag::POLY_DATA>
            (
                "patch1",
                regionName/"boundary"/"patch1"
            );
            vtmBoundaries.append_hdf<vtk::fileTag::POLY_DATA>
            (
                "patch1",
                "boundary/patch1"
            );

            vtmWriter.endBlock("boundary");
            vtmBoundaries.endBlock("boundary");

            // Add to multi-region vtm
            vtmMultiRegion.add(regionName, regionName, vtmWriter);

            Info<< nl << "region = " << regionName << nl;
            vtmWriter.write_xml(Info);

            Info<< nl << "region = " << regionName << " boundary" << nl;
            vtmBoundaries.write_xml(Info);
        }

        // Info<< nl << "multi-region:" << nl;
        // vtmMultiRegion.dump(Info);

        Info<< nl << "XML:" << nl;
        vtmMultiRegion.write_xml(Info);
    }

    return 0;
}


// ************************************************************************* //
