// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
/**
 * This tests reading a LAS file.
 */

#include "vtkCamera.h"
#include "vtkLookupTable.h"
#include "vtkNew.h"
#include "vtkPointData.h"
#include "vtkPolyDataMapper.h"
#include "vtkRegressionTestImage.h"
#include "vtkRenderer.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderWindow.h"
#include "vtkSmartPointer.h"
#include "vtkTestUtilities.h"
#include "vtkUnsignedCharArray.h"
#include "vtkUnsignedShortArray.h"


//#include "vtkXMLPolyDataWriter.h"

#include "vtkLASReader.h"

int TestLASReader_@VTK_LAS_READER_POSTFIX@(int argc, char **argv)
{
  const char* fileName = "Data/@VTK_LAS_READER_TEST_INPUT@";
  const char* path = vtkTestUtilities::ExpandDataFileName(argc, argv, fileName);
  vtkNew<vtkLASReader> reader;
  //Select source file
  reader->SetFileName(path);

  //Read the output
  reader->Update();

  delete [] path;

  vtkSmartPointer<vtkPolyData> outputData = reader->GetOutput();

  bool useClassification = false;
  bool useColor = false;
  vtkUnsignedShortArray* classification =
    vtkUnsignedShortArray::SafeDownCast(outputData->GetPointData()->GetArray("classification"));
  vtkUnsignedShortArray* intensity =
    vtkUnsignedShortArray::SafeDownCast(outputData->GetPointData()->GetArray("intensity"));
  vtkUnsignedShortArray* color =
    vtkUnsignedShortArray::SafeDownCast(outputData->GetPointData()->GetArray("color"));
  double range[2];
  if (classification)
  {
    classification->GetRange(range, 0);
    if (range[0] != range[1])
    {
      std::cout << "Color by classification\n";
      useClassification = true;
      outputData->GetPointData()->SetActiveScalars("classification");
    }
  }
  if (! useClassification)
  {
    if (color)
    {
      color->GetRange(range, 0);
      if (range[0] != range[1])
      {
        std::cout << "Color by color scalar\n";
        useColor = true;
        outputData->GetPointData()->SetActiveScalars("color");
      }
    }
    else
    {
      std::cout << "Color by intensity\n";
      intensity->GetRange(range, 0);
      outputData->GetPointData()->SetActiveScalars("intensity");
    }
  }


  // vtkNew<vtkXMLPolyDataWriter> writer;
  // writer->SetFileName("test.vtp");
  // writer->SetInputData(outputData);
  // writer->Write();

  //Visualise in a render window
  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputData(outputData);
  if (useColor)
  {
    mapper->SetColorModeToDirectScalars();
  }
  else
  {
    mapper->SetScalarRange(range);
  }

  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);

  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);

  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);
  renderer->AddActor(actor);
  renderer->ResetCamera();

  vtkCamera *camera=renderer->GetActiveCamera();
  camera->Elevation(@VTK_LAS_READER_ELEVATION@);

  int retVal = vtkRegressionTestImageThreshold (renderWindow, 0.2);
  if (retVal == vtkRegressionTester::DO_INTERACTOR)
  {
    renderWindowInteractor->Start ();
    retVal = vtkRegressionTester::PASSED;
  }
  return !((retVal == vtkTesting::PASSED) || (retVal == vtkTesting::DO_INTERACTOR));
}
