// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
/*
This test does a read write cycle of a known good hypertree grid file,
comparing the shape of read and written trees, to exercise the reader
and writer and ensure that they work.
*/

#include "vtkDataArray.h"
#include "vtkHyperTreeGrid.h"
#include "vtkNew.h"
#include "vtkStringFormatter.h"
#include "vtkTestUtilities.h"
#include "vtkXMLHyperTreeGridReader.h"
#include "vtkXMLHyperTreeGridWriter.h"

#include <vtksys/SystemTools.hxx>

#include <string>

#include <iostream>

#define VTK_SUCCESS 0
#define VTK_FAILURE 1
int TestXMLHyperTreeGridIO(int argc, char* argv[])
{
  char* temp_dir =
    vtkTestUtilities::GetArgOrEnvOrDefault("-T", argc, argv, "VTK_TEMP_DIR", "Testing/Temporary");
  if (!temp_dir)
  {
    std::cerr << "Could not determine temporary directory." << std::endl;
    return VTK_FAILURE;
  }

  char* data_dir = vtkTestUtilities::GetDataRoot(argc, argv);
  if (!data_dir)
  {
    delete[] temp_dir;
    std::cerr << "Could not determine data directory." << std::endl;
    return VTK_FAILURE;
  }

  std::string fname = "HyperTreeGrid.1.htg";

  std::string ifname = std::string(data_dir) + std::string("/Data/") + fname;

  std::cout << "- READ INPUT --------------------------------" << std::endl;
  vtkNew<vtkXMLHyperTreeGridReader> reader;
  reader->SetFileName(ifname.c_str());
  reader->Update();

  vtkHyperTreeGrid* read_in = reader->GetOutput();
  int size[3];
  vtkDataArray* coords;
  int ntp;

  std::string read1;

  read_in->GetDimensions(size);
  read1 += "SIZE " + vtk::to_string(size[0]) + "," + vtk::to_string(size[1]) + "," +
    vtk::to_string(size[2]) + "\n";
  read1 += "DIMS " + vtk::to_string(read_in->GetDimension()) + "\n";
  read1 += "#TREES " + vtk::to_string(read_in->GetMaxNumberOfTrees()) + "\n";
  read1 += "ORIENTATION " + vtk::to_string(read_in->GetOrientation()) + "\n";
  read1 += "BRANCHFACTOR " + vtk::to_string(read_in->GetBranchFactor()) + "\n";
  coords = read_in->GetXCoordinates();
  ntp = coords->GetNumberOfTuples();
  read1 += "XCOORDS " + vtk::to_string(ntp) + ":" + vtk::to_string(coords->GetTuple1(0)) + "..." +
    vtk::to_string(coords->GetTuple1(ntp - 1)) + "\n";
  coords = read_in->GetYCoordinates();
  ntp = coords->GetNumberOfTuples();
  read1 += "YCOORDS " + vtk::to_string(ntp) + ":" + vtk::to_string(coords->GetTuple1(0)) + "..." +
    vtk::to_string(coords->GetTuple1(ntp - 1)) + "\n";
  coords = read_in->GetZCoordinates();
  ntp = coords->GetNumberOfTuples();
  read1 += "ZCOORDS " + vtk::to_string(ntp) + ":" + vtk::to_string(coords->GetTuple1(0)) + "..." +
    vtk::to_string(coords->GetTuple1(ntp - 1)) + "\n";
  read1 += "TRANSPOSED " + vtk::to_string(read_in->GetTransposedRootIndexing()) + "\n";
  read1 += "#CHILDREN " + vtk::to_string(read_in->GetNumberOfChildren()) + "\n";
  read1 += "#LEVELS " + vtk::to_string(read_in->GetNumberOfLevels()) + "\n";
  read1 += "#VERTS " + vtk::to_string(read_in->GetNumberOfCells()) + "\n";
  read1 += "#LEAVES " + vtk::to_string(read_in->GetNumberOfLeaves()) + "\n";
  std::cout << read1 << std::endl;

  std::string output_dir = temp_dir;
  output_dir += "/HTG";

  vtksys::SystemTools::MakeDirectory(output_dir);

  std::string ofname = output_dir + "/" + fname;

  std::cout << "- WRITE BINARY --------------------------------" << std::endl;

  vtkNew<vtkXMLHyperTreeGridWriter> writer;
  writer->SetFileName(ofname.c_str());
  writer->SetInputData(read_in);
  writer->SetDataModeToBinary();
  writer->Write();

  vtkNew<vtkXMLHyperTreeGridReader> reader2;
  reader2->SetFileName(ofname.c_str());
  reader2->Update();
  vtkHyperTreeGrid* wrote_out = reader2->GetOutput();

  std::string read2;
  wrote_out->GetDimensions(size);
  read2 += "SIZE " + vtk::to_string(size[0]) + "," + vtk::to_string(size[1]) + "," +
    vtk::to_string(size[2]) + "\n";
  read2 += "DIMS " + vtk::to_string(wrote_out->GetDimension()) + "\n";
  read2 += "#TREES " + vtk::to_string(wrote_out->GetMaxNumberOfTrees()) + "\n";
  read2 += "ORIENTATION " + vtk::to_string(wrote_out->GetOrientation()) + "\n";
  read2 += "BRANCHFACTOR " + vtk::to_string(wrote_out->GetBranchFactor()) + "\n";
  coords = wrote_out->GetXCoordinates();
  ntp = coords->GetNumberOfTuples();
  read2 += "XCOORDS " + vtk::to_string(ntp) + ":" + vtk::to_string(coords->GetTuple1(0)) + "..." +
    vtk::to_string(coords->GetTuple1(ntp - 1)) + "\n";
  coords = wrote_out->GetYCoordinates();
  ntp = coords->GetNumberOfTuples();
  read2 += "YCOORDS " + vtk::to_string(ntp) + ":" + vtk::to_string(coords->GetTuple1(0)) + "..." +
    vtk::to_string(coords->GetTuple1(ntp - 1)) + "\n";
  coords = wrote_out->GetZCoordinates();
  ntp = coords->GetNumberOfTuples();
  read2 += "ZCOORDS " + vtk::to_string(ntp) + ":" + vtk::to_string(coords->GetTuple1(0)) + "..." +
    vtk::to_string(coords->GetTuple1(ntp - 1)) + "\n";
  read2 += "TRANSPOSED " + vtk::to_string(wrote_out->GetTransposedRootIndexing()) + "\n";
  read2 += "#CHILDREN " + vtk::to_string(wrote_out->GetNumberOfChildren()) + "\n";
  read2 += "#LEVELS " + vtk::to_string(wrote_out->GetNumberOfLevels()) + "\n";
  read2 += "#VERTS " + vtk::to_string(wrote_out->GetNumberOfCells()) + "\n";
  read2 += "#LEAVES " + vtk::to_string(wrote_out->GetNumberOfLeaves()) + "\n";
  std::cout << read2 << std::endl;

  std::cout << "- WRITE APPENDED --------------------------------" << std::endl;

  writer->SetDataModeToAppended();
  writer->Write();

  reader2->Update();
  wrote_out = reader2->GetOutput();

  std::string read3;
  wrote_out->GetDimensions(size);
  read3 += "SIZE " + vtk::to_string(size[0]) + "," + vtk::to_string(size[1]) + "," +
    vtk::to_string(size[2]) + "\n";
  read3 += "DIMS " + vtk::to_string(wrote_out->GetDimension()) + "\n";
  read3 += "#TREES " + vtk::to_string(wrote_out->GetMaxNumberOfTrees()) + "\n";
  read3 += "ORIENTATION " + vtk::to_string(wrote_out->GetOrientation()) + "\n";
  read3 += "BRANCHFACTOR " + vtk::to_string(wrote_out->GetBranchFactor()) + "\n";
  coords = wrote_out->GetXCoordinates();
  ntp = coords->GetNumberOfTuples();
  read3 += "XCOORDS " + vtk::to_string(ntp) + ":" + vtk::to_string(coords->GetTuple1(0)) + "..." +
    vtk::to_string(coords->GetTuple1(ntp - 1)) + "\n";
  coords = wrote_out->GetYCoordinates();
  ntp = coords->GetNumberOfTuples();
  read3 += "YCOORDS " + vtk::to_string(ntp) + ":" + vtk::to_string(coords->GetTuple1(0)) + "..." +
    vtk::to_string(coords->GetTuple1(ntp - 1)) + "\n";
  coords = wrote_out->GetZCoordinates();
  ntp = coords->GetNumberOfTuples();
  read3 += "ZCOORDS " + vtk::to_string(ntp) + ":" + vtk::to_string(coords->GetTuple1(0)) + "..." +
    vtk::to_string(coords->GetTuple1(ntp - 1)) + "\n";
  read3 += "TRANSPOSED " + vtk::to_string(wrote_out->GetTransposedRootIndexing()) + "\n";
  read3 += "#CHILDREN " + vtk::to_string(wrote_out->GetNumberOfChildren()) + "\n";
  read3 += "#LEVELS " + vtk::to_string(wrote_out->GetNumberOfLevels()) + "\n";
  read3 += "#VERTS " + vtk::to_string(wrote_out->GetNumberOfCells()) + "\n";
  read3 += "#LEAVES " + vtk::to_string(wrote_out->GetNumberOfLeaves()) + "\n";
  std::cout << read3 << std::endl;

  bool ret = VTK_FAILURE;
  if (read1 == read2 && read1 == read3)
  {
    ret = VTK_SUCCESS;
    vtksys::SystemTools::RemoveADirectory(output_dir);
  }
  else
  {
    std::cerr << "Problem: Written file does not match read in file." << std::endl;
  }

  delete[] temp_dir;
  delete[] data_dir;

  return ret;
}
