#include <cassert>
#include <iostream>
#include <vector>
#include <string>
using namespace std;

#include <commandline.h>
#include <graphmisc.h>
#include <vrmltest.h>
#include <vrmlshaperaw.h>
#include <vrmlconvert.h>
#include <zpr.h>

vrmlshapeparse vrmltest::vsp;
vector< vrmlshape > vrmltest::vs;
bool vrmltest::winding(false);
vrmllines vrmltest::vL;


void vrmltest::keyboard(unsigned char key, int x, int y)
{
  switch (key)
  {
    case 27: exit(0); break;
  }
}

void vrmltest::display()
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  myglPushMatrix temp;

  unsigned int const imax = vs.size();
  if (winding)
    for (unsigned int i=0; i<imax; ++i)
      vs[i].writePwind();
  else
    for (unsigned int i=0; i<imax; ++i)
      vs[i].writePN();

  vL.writePC();

  glerrordisplay();
  
  glutSwapBuffers();
}


void vrmltest::getfilename(string & filename)
{
  cout << "Enter filename to parse." << endl;
  cout << "  eg p3.wrl" << endl;

  cin >> filename;
}

void vrmltest::test01(int argc, char** argv)
{
  vrmlshapeparse p;
 
//  string filename;
//  getfilename(filename);

  string filename("head.wrl");
  assert( p.readfile(filename.c_str()) );  

  vector< vrmlshape > s;
  vrmlconvert vc;

  assert(vc.eval(s,p));

  cout << endl << endl;
  cout << "Printing vrmlshape" << endl;
  cout << endl;
  cout << "s.size()=" << s.size() << endl;
  cout << endl;


  for (unsigned int i=0; i<s.size(); ++i)
    s[i].print(cout);
}

void vrmltest::test02(int argc, char** argv)
{
  cout << endl;
  cout << "VRML reader" << endl;
  cout << endl;
  cout << "$./main filename=<file>.wrl" << endl;
  cout << "  seenormals=true   - to see the normals." << endl;
  cout << "  winding=true      - to see the triangles winding." << endl;
  cout << endl;

  commandline cmd(argc,argv);
  string vrmlfile("head.wrl");
  cmd.mapvar(vrmlfile,"vrmlfile");
  if (vrmlfile.empty())
  {
    cout << "error: file must be assigned." << endl;
    cout << "  eg vrmlfile=head.wrl" << endl;
    return;
  }

  if (! vsp.readfile(vrmlfile) )
  {
    cout << "error: file not read in." << endl;
    return;
  }

  vrmlconvert().eval(vs,vsp);

  cmd.mapvar(winding,"winding");

  bool seenormals(false);
  cmd.mapvar(seenormals,"seenormals");
  if (seenormals)
  {

    float ci[3];
    ci[0] = 1.0;
    ci[1] = 0.0;
    ci[2] = 0.0;

    for (unsigned int i=0; i<vs.size(); ++i)
      vL.addnormals(vs[i],(float*)ci,1.8);
  }

  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  glutInitWindowSize(800,800);
  glutCreateWindow("");
  glutDisplayFunc(display);
  glutKeyboardFunc(keyboard);

  zpr zz;

  OpenGLinitialisation();

  glEnable(GL_DEPTH_TEST);
  glEnable(GL_CULL_FACE);
  glEnable(GL_NORMALIZE);

  zz.update();

  camera::lookat(cmd,0.0,0.0,4.0);

  GLdouble s=0.25;
  glScalef(s,s,s);

  glutMainLoop();
}

void vrmltest::test03(int argc, char** argv)
{
  vrmlshapeparse p;
 
  string filename;
  getfilename(filename);

  p.readfile(filename.c_str());

  cout << "Reading " << filename << endl; 

  if (p.vshp.empty()==false)
  {
    for (uint i=0; i<p.vshp.size(); ++i)
      p.vshp[i].print(cout);
  }
}


