#include "kdtreefindstartcellvis.h"
#include "kdtreelog.h"
#include <GL/gl.h>
#include <stdio.h>

KDTreeFindStartCellVis::KDTreeFindStartCellVis (const Vis *parent, const LogEntry *someevent) : Vis (parent, someevent) {
  strcpy (_type, "KDTreeFindStartCell");
  _start = VisManager::FindVisStart (this, someevent);
}

void KDTreeFindStartCellVis::Display (const LogEntry *upto) const {
  GLfloat oldWidth;
  glGetFloatv(GL_LINE_WIDTH, &oldWidth);
  const LogEntry * se = upto;

  switch (upto->_event) {
  case KDTreeFindStartCell_End:
    KDTreeFindStartCell_End_Entry *e = (KDTreeFindStartCell_End_Entry*)upto->_data;
    glLineWidth(oldWidth+2);
    glColor3f(1.0,0.6,0.0);  // orange
    e->_start->_bounds.drawWireFrame();
    glLineWidth(oldWidth);
    se = _start;

  case KDTreeFindStartCell_Start:
    KDTreeFindStartCell_Start_Entry *s = (KDTreeFindStartCell_Start_Entry*)se->_data;

    float epsilon = 0.1;
    float rx = s->_R.x();
    float ry = s->_R.y();
    float rz = s->_R.z();
    float dx = s->_D.x();
    float dy = s->_D.y();
    float dz = s->_D.z();

    //draw point
    glColor3f(0.0,0.0,0.0);  // black
    glBegin(GL_LINES);
    glVertex3f(rx-epsilon, ry, rz);
    glVertex3f(rx+epsilon, ry, rz);
    glVertex3f(rx, ry-epsilon, rz);
    glVertex3f(rx, ry+epsilon, rz);
    glVertex3f(rx, ry, rz-epsilon);
    glVertex3f(rx, ry, rz+epsilon);
    glEnd();

    //draw direction
    float length = 10.0;
    glColor3f(1.0,0.0,0.0); // red
    glBegin(GL_LINES);
    glVertex3f(rx, ry, rz);
    glVertex3f(rx+length*dx, ry+length*dy, rz+length*dz);
    glEnd();
    break;
  default:
    break;
  }
}

char * KDTreeFindStartCellVis::StatusLine (const LogEntry *ours) const {
  char *newline = new char [150];
  switch (ours->_event) {
  case KDTreeFindStartCell_End:
    //KDTreeFindStartCell_End_Entry *e = (KDTreeFindStartCell_End_Entry*)ours->_data;
    sprintf(newline, "%s: found start cell", ours->_name);
    break;
  case KDTreeFindStartCell_Start:
    //KDTreeFindStartCell_Start_Entry *s = (KDTreeFindStartCell_Start_Entry*)ours->_data;
    sprintf(newline, "%s: looking for start cell", ours->_name);
    break;
  default:
    break;
  }
  return newline;
}


