// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause

#include "vtkAOSDataArrayTemplate.h"
#include "vtkAffineArray.h"
#include "vtkDIYExplicitAssigner.h"
#include "vtkDIYUtilities.h"
#include "vtkDataArrayRange.h"
#include "vtkDoubleArray.h"
#include "vtkFieldData.h"
#include "vtkIntArray.h"
#include "vtkLogger.h"
#include "vtkNew.h"
#include "vtkSOADataArrayTemplate.h"
#include "vtkSmartPointer.h"

// clang-format off
#include "vtk_diy2.h"
#include VTK_DIY2(diy/master.hpp)
#include VTK_DIY2(diy/mpi.hpp)
// clang-format on

template <vtkIdType N, vtkIdType M, class ArrayT>
bool TestTemplatedArray(ArrayT* array, bool initialized = false)
{
  using ValueType = vtk::GetAPIType<ArrayT>;
  if (!initialized)
  {
    array->SetName("array-name");
    array->SetNumberOfComponents(N);
    array->SetNumberOfTuples(M);
    for (vtkIdType i = 0; i < M; ++i)
    {
      ValueType tuple[N];
      for (vtkIdType j = 0; j < N; ++j)
      {
        tuple[j] = static_cast<ValueType>(N * i + j);
      }
      array->SetTypedTuple(i, tuple);
    }
  }

  diy::MemoryBuffer bb;
  vtkDIYUtilities::Save(bb, array);
  bb.position = 0L;

  vtkDataArray* ptr(nullptr);
  vtkDIYUtilities::Load(bb, ptr);
  const auto loaded = vtkSmartPointer<vtkDataArray>::Take(ptr);

  if (!loaded || loaded->GetNumberOfComponents() != N || loaded->GetNumberOfTuples() != M)
  {
    vtkLog(ERROR, "Data array not loaded correctly");
    return false;
  }

  for (vtkIdType i = 0; i < M; ++i)
  {
    ValueType tuple[N], expected[N];
    for (vtkIdType j = 0; j < N; ++j)
    {
      expected[j] = static_cast<ValueType>(N * i + j);
    }

    array->GetTypedTuple(i, tuple);
    for (vtkIdType j = 0; j < N; ++j)
    {
      if (std::abs(expected[j] - tuple[j]) > 0)
      {
        vtkLog(ERROR, "Data not loaded correctly: " << expected[j] << " != " << tuple[j]);
        return false;
      }
    }
  }

  return true;
}

//------------------------------------------------------------------------------
int TestDIYDataArraySerialization(int vtkNotUsed(argc), char* vtkNotUsed(argv)[])
{
  bool rc(false);
  const vtkNew<vtkDoubleArray> dblArray;
  rc = TestTemplatedArray<3, 14>(dblArray.GetPointer());

  const vtkNew<vtkSOADataArrayTemplate<double>> soaArray;
  rc |= TestTemplatedArray<7, 2>(soaArray.GetPointer());

  const vtkNew<vtkAOSDataArrayTemplate<int>> intArray;
  rc |= TestTemplatedArray<13, 5>(intArray.GetPointer());

  const vtkNew<vtkAffineArray<double>> affineArray;
  affineArray->ConstructBackend(1, 0);
  affineArray->SetNumberOfComponents(3);
  affineArray->SetNumberOfTuples(14);
  rc |= TestTemplatedArray<3, 14>(affineArray.GetPointer(), true);

  return rc ? EXIT_SUCCESS : EXIT_FAILURE;
}
