// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include <vtkImageData.h>
#include <vtkImageViewer2.h>
#include <vtkNew.h>
#include <vtkOpenSlideReader.h>
#include <vtkPNGWriter.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

// VTK includes
#include <vtkTestUtilities.h>

// C++ includes
#include <sstream>

#include <iostream>

// Main program
int TestOpenSlideReader(int argc, char** argv)
{
  // This test is known to fail with openslide library libopenslide-dev shipped
  // with ubuntu 14.04 as of March 31'2016. It does pass on fedora23, or if the
  // openslide library is built from source
  const char* rasterFileName =
    vtkTestUtilities::ExpandDataFileName(argc, argv, "Data/Microscopy/small2.ndpi");

  // std::cout << "Got Filename: " << rasterFileName << std::endl;

  // Create reader to read shape file.
  vtkNew<vtkOpenSlideReader> reader;
  reader->SetFileName(rasterFileName);
  reader->UpdateInformation();
  delete[] rasterFileName;

  // For debug
  // reader->SetUpdateExtent(extent);
  // vtkNew<vtkPNGWriter> writer;
  // writer->SetInputConnection(reader->GetOutputPort());
  // writer->SetFileName("this.png");
  // writer->SetUpdateExtent(extent);
  // writer->Update();
  // writer->Write();

  // Visualize
  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> window;
  window->AddRenderer(renderer);

  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(window);

  vtkNew<vtkImageViewer2> imageViewer;
  imageViewer->SetInputConnection(reader->GetOutputPort());
  // imageViewer->SetExtent(1000,1500,1000,1500,0,0);
  imageViewer->SetupInteractor(renderWindowInteractor);
  // imageViewer->SetSlice(0);
  imageViewer->Render();
  imageViewer->GetRenderer()->ResetCamera();
  renderWindowInteractor->Initialize();
  imageViewer->Render();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}
