#include "BATesting.h"
#include "Base/Axis/Frame.h"
#include "Base/Axis/MakeScale.h"
#include "Base/Axis/Scale.h"
#include "Device/Data/Datafield.h"
#include "Device/IO/IOFactory.h"
#include "Device/IO/ImportSettings.h"
#include "Device/IO/ReadWrite2DTable.h"
#include "Device/IO/ReadWriteINT.h"
#include "Device/IO/ReadWriteTiff.h"
#include "Tests/GTestWrapper/google_test.h"
#include <filesystem>

namespace fs = std::filesystem;

static const fs::path testdir =
    fs::path(BATesting::TestOutDir) / fs::path("Unit") / fs::path("Device");

class RW2DTest : public ::testing::Test {
protected:
    RW2DTest()
    {
        for (size_t i = 0, size = m_model_data.size(); i < size; ++i)
            m_model_data[i] = static_cast<double>(i);
    }

    Datafield m_model_data{Frame(EquiDivision("x", 5, 1.0, 5.0), EquiDivision("y", 10, 6.0, 7.0))};
};


TEST_F(RW2DTest, rwInt)
{
    std::stringstream ss;
    RW::writeBAInt(m_model_data, ss);

    Datafield result = RW::readBAInt(ss);

    auto compare_axis = [this, &result](size_t index) {
        EXPECT_EQ(m_model_data.axis(index).size(), result.axis(index).size());
        EXPECT_EQ(m_model_data.axis(index).min(), result.axis(index).min());
        EXPECT_EQ(m_model_data.axis(index).max(), result.axis(index).max());
    };

    EXPECT_EQ(m_model_data.rank(), result.rank());
    compare_axis(0);
    compare_axis(1);
    for (size_t i = 0, size = m_model_data.size(); i < size; ++i)
        EXPECT_EQ(m_model_data[i], result[i]);
}

TEST_F(RW2DTest, rwNumpyMatrix)
{
    std::stringstream ss;
    RW::write2DTable(m_model_data, ss);

    Datafield result = RW::read2DTable(ss);
    EXPECT_EQ(m_model_data.rank(), result.rank());
    for (size_t i = 0, size = m_model_data.size(); i < size; ++i)
        EXPECT_EQ(m_model_data[i], result[i]);
}

TEST_F(RW2DTest, ascii)
{
    const std::string fname = (testdir / fs::path("rw2dtestdata.csv")).string();
    IO::writeDatafield(m_model_data, fname);

    Datafield result = IO::readData2D(fname);
    EXPECT_EQ(m_model_data.rank(), result.rank());
    for (size_t i = 0, size = m_model_data.size(); i < size; ++i)
        EXPECT_EQ(m_model_data[i], result[i]);
}

TEST_F(RW2DTest, gz)
{
    const std::string fname = (testdir / fs::path("rw2dtestdata.csv.gz")).string();
    IO::writeDatafield(m_model_data, fname);

    Datafield result = IO::readData2D(fname);
    EXPECT_EQ(m_model_data.rank(), result.rank());
    for (size_t i = 0, size = m_model_data.size(); i < size; ++i)
        EXPECT_EQ(m_model_data[i], result[i]);
}

TEST_F(RW2DTest, bz2)
{
    const std::string fname = (testdir / fs::path("rw2dtestdata.csv.bz2")).string();
    IO::writeDatafield(m_model_data, fname);

    Datafield result = IO::readData2D(fname);
    EXPECT_EQ(m_model_data.rank(), result.rank());
    for (size_t i = 0, size = m_model_data.size(); i < size; ++i)
        EXPECT_EQ(m_model_data[i], result[i]);
}

TEST_F(RW2DTest, csv2DRequiresSettings)
{
    // Explicit csv2D format without ImportSettings2D must throw
    const std::string fname = (testdir / fs::path("rw2dtestdata.csv")).string();
    IO::writeDatafield(m_model_data, fname);

    EXPECT_THROW(IO::readData2D(fname, IO::csv2D), std::runtime_error);

    // With settings, it should work
    ImportSettings2D settings("x", "y");
    Datafield result = IO::readData2D(fname, IO::csv2D, &settings);
    EXPECT_EQ(m_model_data.rank(), result.rank());
    for (size_t i = 0, size = m_model_data.size(); i < size; ++i)
        EXPECT_EQ(m_model_data[i], result[i]);
}

TEST_F(RW2DTest, csv2DWithCoordinates)
{
    // Test that coordinate labels from ImportSettings2D are applied
    const std::string fname = (testdir / fs::path("rw2dtestdata_coord.csv")).string();
    IO::writeDatafield(m_model_data, fname);

    ImportSettings2D settings("q_x (1/nm)", "q_z (1/nm)");
    Datafield result = IO::readData2D(fname, IO::csv2D, &settings);

    // For bare intensity (no axis info in file), we get default bin axes
    // But the test verifies the settings parameter is accepted
    EXPECT_EQ(m_model_data.rank(), result.rank());
    EXPECT_EQ(m_model_data.size(), result.size());
}

TEST_F(RW2DTest, csv2DSwapAxes)
{
    // Test that swap_axes transposes the data even without embedded axis info
    const std::string fname = (testdir / fs::path("rw2dtestdata_swap.csv")).string();
    IO::writeDatafield(m_model_data, fname);

    // Read without swap
    ImportSettings2D settings("x", "y");
    Datafield normal = IO::readData2D(fname, IO::csv2D, &settings);

    // Read with swap (has_axes=false, swap_axes=true)
    ImportSettings2D swap_settings("x", "y", false, true);
    Datafield swapped = IO::readData2D(fname, IO::csv2D, &swap_settings);

    // Swapped should have transposed dimensions
    EXPECT_EQ(normal.axis(0).size(), swapped.axis(1).size());
    EXPECT_EQ(normal.axis(1).size(), swapped.axis(0).size());
}

#ifdef BA_TIFF_SUPPORT

TEST_F(RW2DTest, rwTiff)
{
    std::stringstream ss;
    RW::writeTiff(m_model_data, ss);

    Datafield result = RW::readTiff(ss);
    EXPECT_EQ(m_model_data.rank(), result.rank());
    for (size_t i = 0, size = m_model_data.size(); i < size; ++i)
        EXPECT_EQ(m_model_data[i], result[i]);
}

#endif // BA_TIFF_SUPPORT
