#include "qt/view_solution_geometry.hpp"

QtViewSolutionGeometry::QtViewSolutionGeometry(Kernel* _kernel){
  kernel=_kernel;

};

void
QtViewSolutionGeometry::initializeGL(){
  glClearColor(1,1,1,1);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_LIGHT0);
  glEnable(GL_LIGHTING);
  glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
  glEnable(GL_COLOR_MATERIAL);
}

void
QtViewSolutionGeometry::paintGL(){
  cout<<"PaintGL"<<endl;
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  if(step>kernel->step){
    cout<<"Pas encore calculé"<<endl;
  }
  else{
    double dX=kernel->geometry.dX;
    double lX=kernel->geometry.lX;
    double h=height();
    double w=width();
    glBegin(GL_TRIANGLES);
    for(size_t i=0;i<kernel->geometry.nX-1;++i){
      size_t max_left=kernel->nZ(i);
      size_t max_right=kernel->nZ(i+1);
      size_t left=0;
      size_t right=0;
      while(left<max_left-1 and right<max_right-1){
	if(kernel->Z(i,left+1)<=kernel->Z(i+1,right+1)){
	  drawTriangle(i,left,i+1,right,i,left+1);
	  ++left;
	}
	else{
	  drawTriangle(i,left,i+1,right,i+1,right+1);
	  ++right;
	}
      }
      if(left==max_left-1){
	while(right<max_right-1){
	  drawTriangle(i,left,i+1,right,i+1,right+1);
	  ++right;
	}
      }
      if(right==max_right-1){
	while(left<max_left-1){
	  drawTriangle(i,left,i+1,right,i,left+1);
	  ++left;
	}
      }
    }
    glEnd();
  }
}

void
QtViewSolutionGeometry::resizeGL(int w,int h){
  glViewport(0,0,w,h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(0,kernel->geometry.lX,0,1);
  //gluPerspective(45, (float)w/h, 0.01, 100.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  //gluLookAt(0,0,5,0,0,0,0,1,0);
}
/*void
QtViewSolutionGeometry::paintEvent(QPaintEvent* event){
  QPainter painter(this);
  painter.beginNativePainting();

  glClearColor(0, 0, 0, 1);
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_LIGHT0);
        glEnable(GL_LIGHTING);
        glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
        glEnable(GL_COLOR_MATERIAL);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glBegin(GL_TRIANGLES);
    glColor3f(1.0, 0.0, 0.0);
        glVertex3f(0, 10, 0);
        glColor3f(0.0, 1.0, 0.0);
        glVertex3f(20, -20, 0);
        glColor3f(0.0, 0.0, 1.0);
        glVertex3f(0.0, 50, 0);
        glEnd();


	/*if(step>kernel->step){
    cout<<"Pas encore calculé"<<endl;
  }
  else{
    double dX=kernel->geometry.dX;
    double lX=kernel->geometry.lX;
    double h=height();
    double w=width();
    for(size_t i=0;i<kernel->geometry.nX-1;++i){
      size_t max_left=kernel->nZ(i);
      size_t max_right=kernel->nZ(i+1);
      size_t left=0;
      size_t right=0;
      while(left<max_left-1 and right<max_right-1){
	if(kernel->Z(i,left+1)<=kernel->Z(i+1,right+1)){
	  drawTriangle(painter,i,left,i+1,right,i,left+1);
	  ++left;
	}
	else{
	  drawTriangle(painter,i,left,i+1,right,i+1,right+1);
	  ++right;
	}
      }
      if(left==max_left-1){
	while(right<max_right-1){
	  drawTriangle(painter,i,left,i+1,right,i+1,right+1);
	  ++right;
	}
      }
      if(right==max_right-1){
	while(left<max_left-1){
	  drawTriangle(painter,i,left,i+1,right,i,left+1);
	  ++left;
	}
      }
    }
    }
	painter.endNativePainting();
	painter.end();
}
*/

void
QtViewSolutionGeometry::drawTriangle(size_t ix1,size_t iz1,size_t ix2,size_t iz2,size_t ix3,size_t iz3){
  double dX=kernel->geometry.dX;
  double lX=kernel->geometry.lX;
  double h=height();
  double w=width();
  double x1=ix1*dX;
  double x2=ix2*dX;
  double x3=ix3*dX;
  double y1=kernel->Z(ix1,iz1);
  double y2=kernel->Z(ix2,iz2);
  double y3=kernel->Z(ix3,iz3);

  glColor3f(1,0,0);
  glVertex2f(x1,y1);
  glVertex2f(x2,y2);
  glVertex2f(x3,y3);

}

void
QtViewSolutionGeometry::draw(size_t _step){
  step=_step;
  update();
}

void
QtViewSolutionGeometry::mousePressEvent(QMouseEvent* event){
  double x=double(event->x())/width()*kernel->geometry.lX;
  double y=1-(double(event->y())/height());
  displayInfos(x,y);
}

void
QtViewSolutionGeometry::displayInfos(double x,double z){
  cout<<"---------------------------------"<<endl;
  cout<<"x = "<<x<<endl;
  cout<<"z = "<<z<<endl;
  size_t ix=floor(x/kernel->geometry.dX+0.5);
  if(ix==kernel->geometry.nX) --ix;
  cout<<"ix = "<<ix<<endl;
 
  double hbot=kernel->geometry.hbot[ix];
  cout<<"hbot = "<<hbot<<endl;
  double hsoil=kernel->geometry.hsoil[ix];
  cout<<"hsoil = "<<hsoil<<endl;
  if(z>hbot and z<hsoil){
    double dZ=kernel->geometry.dZ[ix];
    cout<<"dZ = "<<dZ<<endl;
    size_t nz=kernel->geometry.nZ[ix];
    cout<<"nZ = "<<nz<<endl;
    size_t iz=floor((z-hbot)/dZ+0.5);
    if(iz==nz) --iz;
    cout<<"iz = "<<iz<<endl;
  }
}
