//
#include "MainGUI.h"

#include "CompositeView.h"
#include <QHBoxLayout>
#include <QVTKGraphicsItem.h>
#include <QTimer>
#include <vtkGenericOpenGLRenderWindow.h>
#include <vtkRendererCollection.h>
#include <vtkRenderer.h>
#include <vtkOpenGLRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleSwitch.h>
#include <vtkCamera.h>
#include <vtkCameraPass.h>
#include <vtkOpaquePass.h>
#include <vtkDepthPeelingPass.h>
#include <vtkTranslucentPass.h>
#include <vtkVolumetricPass.h>
#include <vtkOverlayPass.h>
#include <vtkLightsPass.h>
#include <vtkSequencePass.h>
#include <vtkRenderPassCollection.h>
#include <vtkShadowMapBakerPass.h>
#include <vtkShadowMapPass.h>
#include <vtkAppendPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkInformation.h>
#include <vtkProperty.h>
#include <vtkPlaneSource.h>
#include <vtkSphereSource.h>
#include <vtkLight.h>
#include <vtkAssembly.h>
#include <vtkLightCollection.h>
#include <vtkLightActor.h>
#include <vtkSmartPointer.h>
#include <vtkCubeSource.h>
#include <vtkConeSource.h>
#include <vtkFrameBufferObject.h>
#include <vtkPolyDataNormals.h>
#include <vtkPointData.h>

MainGUI::MainGUI(QWidget *parent) :
  QMainWindow(parent)
{
  double background[3] = {1, 1, 1}; // white
  m_View = new CompositeView(background);
  this->setWindowTitle("test shadows");

  QWidget *cwidget = new QWidget();
  QHBoxLayout *layout = new QHBoxLayout();
  layout->setMargin(0);
  layout->addWidget(m_View);
  cwidget->setLayout(layout);
  this->setCentralWidget(cwidget);  

  m_Timer = new QTimer();
  m_Timer->stop();
  connect(m_Timer, SIGNAL(timeout()), this, SLOT(OnTimerTimeout()));
  m_Timer->start(1000);
  m_TimerCount = 0;
}

MainGUI::~MainGUI()
{
  delete m_VTKGI;
  delete m_View;
  delete m_Timer;
}

void MainGUI::InitializeSimpleScene()
{
  m_VTKGI = new QVTKGraphicsItem(m_View->GetGLContext(), NULL);

  m_VTKGI->setGeometry(0, 0, m_View->width(), m_View->height());

  vtkRendererCollection *rens = m_VTKGI->GetRenderWindow()->GetRenderers();
  vtkCollectionSimpleIterator rsit;
  vtkRenderer *ren;
  for (rens->InitTraversal(rsit); (ren = rens->GetNextRenderer(rsit)); )
    m_VTKGI->GetRenderWindow()->RemoveRenderer(ren);

  ren = vtkRenderer::New();
  m_VTKGI->GetRenderWindow()->AddRenderer(ren);

  vtkSmartPointer<vtkRenderWindowInteractor> rwi = m_VTKGI->GetRenderWindow()->GetInteractor();
  vtkSmartPointer<vtkInteractorStyleSwitch> style =
      vtkSmartPointer<vtkInteractorStyleSwitch>::New();
  style->SetCurrentStyleToTrackballActor();
  rwi->SetInteractorStyle(style);

  vtkSphereSource *sphere = vtkSphereSource::New();
  sphere->SetCenter(0, 0, 0);
  sphere->SetRadius(5);
  sphere->SetThetaResolution(20);
  sphere->SetPhiResolution(20);
  sphere->Update();
  vtkPolyDataMapper *sphereMapper = vtkPolyDataMapper::New();
  sphereMapper->SetInputConnection(sphere->GetOutputPort());
  vtkActor *sphereActor = vtkActor::New();
  sphereActor->SetMapper(sphereMapper);
  sphereActor->GetProperty()->SetColor(1, 1, 0);
  ren->AddActor(sphereActor);

  ren->ResetCamera();
  ren->SetBackground(.4, .4, .4);
  ren->Delete();
  m_VTKGI->GetRenderWindow()->Render();

  // finally add to scene
  m_View->GetScene()->addItem(m_VTKGI);
}

void AddLightActors(vtkRenderer *r)
{
  assert("pre: r_exists" && r!=0);

  vtkLightCollection *lights=r->GetLights();

  lights->InitTraversal();
  vtkLight *l=lights->GetNextItem();
  while(l!=0)
    {
    double angle=l->GetConeAngle();
    if(l->LightTypeIsSceneLight() && l->GetPositional()
       && angle<180.0) // spotlight
      {
      vtkLightActor *la=vtkLightActor::New();
      la->SetLight(l);
      r->AddViewProp(la);
      la->Delete();
      }
    l=lights->GetNextItem();
    }
}

void MainGUI::InitializeShadowScene()
{
  vtkRenderWindow *renderWindow = m_VTKGI->GetRenderWindow();

  vtkRenderer *cren = NULL;
  renderWindow->GetRenderers()->InitTraversal();
  do
  {
    cren = renderWindow->GetRenderers()->GetNextItem();
    if (cren)
      renderWindow->RemoveRenderer(cren);
  } while (cren);

  vtkRenderWindowInteractor *interactor = renderWindow->GetInteractor();

  renderWindow->SetMultiSamples(0);

  renderWindow->SetAlphaBitPlanes(1);
  interactor->SetRenderWindow(renderWindow);

#if VTK_MAJOR_VERSION <= 5
  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
#else
  vtkSmartPointer<vtkOpenGLRenderer> renderer =
    vtkSmartPointer<vtkOpenGLRenderer>::New();
#endif
  renderWindow->AddRenderer(renderer);

  bool supported = vtkFrameBufferObject::IsSupported(renderWindow); // adapted from line 182 of vtkShadowMapPass.cxx

  if(!supported)
    {
    std::cerr << "Shadow rendering is not supported by the current video"
              << " driver!" << std::endl;
    return;
    }

  vtkSmartPointer<vtkCameraPass> cameraP =
    vtkSmartPointer<vtkCameraPass>::New();

  vtkSmartPointer<vtkOpaquePass> opaque =
    vtkSmartPointer<vtkOpaquePass>::New();

  vtkSmartPointer<vtkDepthPeelingPass> peeling =
    vtkSmartPointer<vtkDepthPeelingPass>::New();
  peeling->SetMaximumNumberOfPeels(200);
  peeling->SetOcclusionRatio(0.1);

  vtkSmartPointer<vtkTranslucentPass> translucent =
    vtkSmartPointer<vtkTranslucentPass>::New();
  peeling->SetTranslucentPass(translucent);

  vtkSmartPointer<vtkVolumetricPass> volume =
    vtkSmartPointer<vtkVolumetricPass>::New();
  vtkSmartPointer<vtkOverlayPass> overlay =
    vtkSmartPointer<vtkOverlayPass>::New();

  vtkSmartPointer<vtkLightsPass> lights =
    vtkSmartPointer<vtkLightsPass>::New();

  vtkSmartPointer<vtkSequencePass> opaqueSequence =
    vtkSmartPointer<vtkSequencePass>::New();

  vtkSmartPointer<vtkRenderPassCollection> passes2 =
    vtkSmartPointer<vtkRenderPassCollection>::New();
  passes2->AddItem(lights);
  passes2->AddItem(opaque);
  opaqueSequence->SetPasses(passes2);

  vtkSmartPointer<vtkCameraPass> opaqueCameraPass =
    vtkSmartPointer<vtkCameraPass>::New();
  opaqueCameraPass->SetDelegatePass(opaqueSequence);

  vtkSmartPointer<vtkShadowMapBakerPass> shadowsBaker =
    vtkSmartPointer<vtkShadowMapBakerPass>::New();
  shadowsBaker->SetOpaquePass(opaqueCameraPass);
  shadowsBaker->SetResolution(1024);
  // To cancel self-shadowing.
  shadowsBaker->SetPolygonOffsetFactor(3.1f);
  shadowsBaker->SetPolygonOffsetUnits(10.0f);

  vtkSmartPointer<vtkShadowMapPass> shadows =
    vtkSmartPointer<vtkShadowMapPass>::New();
  shadows->SetShadowMapBakerPass(shadowsBaker);
  shadows->SetOpaquePass(opaqueSequence);

  vtkSmartPointer<vtkSequencePass> seq =
    vtkSmartPointer<vtkSequencePass>::New();
  vtkSmartPointer<vtkRenderPassCollection> passes =
    vtkSmartPointer<vtkRenderPassCollection>::New();
  passes->AddItem(shadowsBaker);
  passes->AddItem(shadows);
  passes->AddItem(lights);
  passes->AddItem(peeling);
  passes->AddItem(volume);
  passes->AddItem(overlay);
  seq->SetPasses(passes);
  cameraP->SetDelegatePass(seq);

  renderer->SetPass(cameraP);

  vtkSmartPointer<vtkPlaneSource> rectangleSource =
    vtkSmartPointer<vtkPlaneSource>::New();
  rectangleSource->SetOrigin(-5.0,0.0,5.0);
  rectangleSource->SetPoint1(5.0,0.0,5.0);
  rectangleSource->SetPoint2(-5.0,0.0,-5.0);
  rectangleSource->SetResolution(100,100);

  vtkSmartPointer<vtkPolyDataMapper> rectangleMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  rectangleMapper->SetInputConnection(rectangleSource->GetOutputPort());

  rectangleMapper->SetScalarVisibility(0);

  vtkSmartPointer<vtkActor> rectangleActor =
    vtkSmartPointer<vtkActor>::New();
  vtkSmartPointer<vtkInformation> rectangleKeyProperties =
    vtkSmartPointer<vtkInformation>::New();
  rectangleKeyProperties->Set(vtkShadowMapBakerPass::OCCLUDER(),0); // dummy val.
  rectangleKeyProperties->Set(vtkShadowMapBakerPass::RECEIVER(),0); // dummy val.
  rectangleActor->SetPropertyKeys(rectangleKeyProperties);

  rectangleActor->SetMapper(rectangleMapper);
  rectangleActor->SetVisibility(1);
  rectangleActor->GetProperty()->SetColor(1.0,1.0,1.0);

  vtkSmartPointer<vtkCubeSource> boxSource =
    vtkSmartPointer<vtkCubeSource>::New();
  boxSource->SetXLength(2.0);
  vtkSmartPointer<vtkPolyDataNormals> boxNormals =
    vtkSmartPointer<vtkPolyDataNormals>::New();
  boxNormals->SetInputConnection(boxSource->GetOutputPort());
  boxNormals->SetComputePointNormals(0);
  boxNormals->SetComputeCellNormals(1);
  boxNormals->Update();
  boxNormals->GetOutput()->GetPointData()->SetNormals(0);

  vtkSmartPointer<vtkPolyDataMapper> boxMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  boxMapper->SetInputConnection(boxNormals->GetOutputPort());
  boxMapper->SetScalarVisibility(0);

  vtkSmartPointer<vtkActor> boxActor =
    vtkSmartPointer<vtkActor>::New();
  vtkSmartPointer<vtkInformation> boxKeyProperties =
    vtkSmartPointer<vtkInformation>::New();
  boxKeyProperties->Set(vtkShadowMapBakerPass::OCCLUDER(),0); // dummy val.
  boxKeyProperties->Set(vtkShadowMapBakerPass::RECEIVER(),0); // dummy val.
  boxActor->SetPropertyKeys(boxKeyProperties);

  boxActor->SetMapper(boxMapper);
  boxActor->SetVisibility(1);
  boxActor->SetPosition(-2.0,2.0,0.0);
  boxActor->GetProperty()->SetColor(1.0,0.0,0.0);

  vtkSmartPointer<vtkConeSource> coneSource =
    vtkSmartPointer<vtkConeSource>::New();
  coneSource->SetResolution(24);
  coneSource->SetDirection(1.0,1.0,1.0);
  vtkSmartPointer<vtkPolyDataMapper> coneMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  coneMapper->SetInputConnection(coneSource->GetOutputPort());
  coneMapper->SetScalarVisibility(0);

  vtkSmartPointer<vtkActor> coneActor =
    vtkSmartPointer<vtkActor>::New();
  vtkSmartPointer<vtkInformation> coneKeyProperties=
    vtkSmartPointer<vtkInformation>::New();
  coneKeyProperties->Set(vtkShadowMapBakerPass::OCCLUDER(),0); // dummy val.
  coneKeyProperties->Set(vtkShadowMapBakerPass::RECEIVER(),0); // dummy val.
  coneActor->SetPropertyKeys(coneKeyProperties);

  coneActor->SetMapper(coneMapper);
  coneActor->SetVisibility(1);
  coneActor->SetPosition(0.0,1.0,1.0);
  coneActor->GetProperty()->SetColor(0.0,0.0,1.0);
//  coneActor->GetProperty()->SetLighting(false);

  vtkSmartPointer<vtkSphereSource> sphereSource =
    vtkSmartPointer<vtkSphereSource>::New();
  sphereSource->SetThetaResolution(32);
  sphereSource->SetPhiResolution(32);
  vtkSmartPointer<vtkPolyDataMapper> sphereMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
  sphereMapper->SetScalarVisibility(0);

  vtkSmartPointer<vtkActor> sphereActor =
    vtkSmartPointer<vtkActor>::New();
  vtkSmartPointer<vtkInformation> sphereKeyProperties =
    vtkSmartPointer<vtkInformation>::New();
  sphereKeyProperties->Set(vtkShadowMapBakerPass::OCCLUDER(),0); // dummy val.
  sphereKeyProperties->Set(vtkShadowMapBakerPass::RECEIVER(),0); // dummy val.
  sphereActor->SetPropertyKeys(sphereKeyProperties);
  sphereActor->SetMapper(sphereMapper);

  sphereActor->SetVisibility(1);
  sphereActor->SetPosition(2.0,2.0,-1.0);
  sphereActor->GetProperty()->SetColor(1.0,1.0,0.0);

  renderer->AddViewProp(rectangleActor);
  renderer->AddViewProp(boxActor);
  renderer->AddViewProp(coneActor);
  renderer->AddViewProp(sphereActor);

  // Spotlights.

  // lighting the box.
  vtkSmartPointer<vtkLight> l1 =
    vtkSmartPointer<vtkLight>::New();
  l1->SetPosition(-4.0,4.0,-1.0);
  l1->SetFocalPoint(boxActor->GetPosition());
  l1->SetColor(1.0,1.0,1.0);
  l1->SetPositional(1);
  renderer->AddLight(l1);
  l1->SetSwitch(1);

  // lighting the sphere
  vtkSmartPointer<vtkLight> l2 =
    vtkSmartPointer<vtkLight>::New();
  l2->SetPosition(4.0,5.0,1.0);
  l2->SetFocalPoint(sphereActor->GetPosition());
  l2->SetColor(1.0,0.0,1.0);
//  l2->SetColor(1.0,1.0,1.0);
  l2->SetPositional(1);
  renderer->AddLight(l2);
  l2->SetSwitch(1);

  AddLightActors(renderer);

  renderer->SetBackground(0.66,0.66,0.66);
  renderer->SetBackground2(157.0/255.0*0.66,186/255.0*0.66,192.0/255.0*0.66);
  renderer->SetGradientBackground(true);

  renderWindow->Render();
  if(peeling->GetLastRenderingUsedDepthPeeling())
    {
    std::cout << "depth peeling was used" << std::endl;
    }
  else
    {
    std::cout << "depth peeling was not used (alpha blending instead)" << std::endl;
    }

  renderer->ResetCamera();
  vtkSmartPointer<vtkCamera> camera = renderer->GetActiveCamera();
  camera->Azimuth(40.0);
  camera->Elevation(10.0);

  renderWindow->Render();
}

void MainGUI::OnTimerTimeout()
{
  m_Timer->stop();
  if (m_TimerCount == 0)
  {
    InitializeSimpleScene();
    m_TimerCount++;
    m_Timer->start(5000);
  }
  else
  {
    InitializeShadowScene();
  }
}

