#include "control.h"
#include "input.h"



/*

Constructor

****************************/

bool Control::bRunning=false;

vector <Node *> Control::nodeList;              //all Nodes
vector<Node *> Control::nodeTree;               //currently active nodes

Control::Control(){

state=MOVECURSORSTATE;
//TODO: remove after saturday...
state=AUTOCAMERASTATE;
viewParent=this;
viewTarget=Vector3f(0,1,2);
viewLocation=Vector3f(0,1,3);
moveMode=FLYINGCAMERA;                        //as opposed to walking

destLocation=Vector3f(0,10,0);
destTarget=Vector3f(-20.0f,-1.7f,39.8f);
velocityLoc=Vector3f(0,0,0);
velocityView=Vector3f(0,0,0);
travelTime=Vector3f(0,0,0);

lookDir=Vector3f(0,0,0);
}

void Control::update(double deltaTime){

switch (state)
       {
       case MOVECURSORSTATE:
       break;

       case MOVECAMERASTATE:
         confineMouse();
         processMove(deltaTime);
       break;
       case AUTOCAMERASTATE:
         moveAutoCamera(deltaTime);
       }
if (bRunning)
  {
  updateNodes();
  }
}

void Control::updateNodes(){
}

void Control::switchMode(){

bRunning=!bRunning;

if (bRunning)
  start();
else
  stop();
}


void Control::start(){

}

void Control::stop(){
}

void Control::processMove(double deltaTime){

float lookSpeed=0.0025;
float moveSpeed=0.025;

Vector3f up(0,1,0);
Vector3f left(1,0,0);
Vector3f forward(0,0,1);

Vector3f move, move2, direction;

//calculate x rotation
direction.x=sin((float)lookDir.x*lookSpeed);
direction.z=-cos((float)lookDir.x*lookSpeed);

//calculate y rotation
direction.y=sin((float)lookDir.y*lookSpeed);
direction.z*=cos((float)lookDir.y*lookSpeed);
direction.x*=cos((float)lookDir.y*lookSpeed);

//fwd/bkwd
move.x= direction.dotProduct(left);

if (moveMode==FLYINGCAMERA)
  move.y= direction.dotProduct(up);        //only if flying

move.z= direction.dotProduct(forward);

move *= Input::keyVector.z;

//strafing
move += (direction.crossProduct(up)) * Input::keyVector.x;

//up/down
move+= up * Input::keyVector.y;

viewLocation+=move*moveSpeed*deltaTime/10;

//set viewTarget to adjust to new Location!
viewTarget=direction+viewLocation;
}

void Control::confineMouse(){

int deltaX;
int deltaY;

deltaX=(int)Input::mouseVector.x;
deltaY=(int)Input::mouseVector.y;

lookDir.x+=deltaX;
lookDir.y+=deltaY;

int centerX=Input::windowX+Input::screenX/2;
int centerY=Input::windowY+Input::screenY/2;

//this is windows specific!
Input::mouseX=Input::screenX/2;
Input::mouseY=Input::screenY/2;
SetCursorPos(centerX,centerY);
}


void Control::toggleFlyMode(){

if (state==MOVECURSORSTATE)
  {
  state=MOVECAMERASTATE;
  std::cout << "switching to navigation mode! \n";
  glutSetCursor(GLUT_CURSOR_NONE);
  }
else if (state==MOVECAMERASTATE)
  {
  state=MOVECURSORSTATE;
  std::cout << "switching to mouse mode! \n";
  glutSetCursor(GLUT_CURSOR_INHERIT);
  }
}


void Control::switchCamToOne(){
}

void Control::switchCamToTwo(){
}

void Control::moveAutoCamera(float deltaTime){
float xMid=6.4;
float yMid=4.8;

viewTarget=Vector3f(xMid,yMid,0.0);
viewLocation.x=5*cos(Renderer::currentTime*0.0001+Input::mouseY*0.01) + xMid;
viewLocation.z=7*sin(Renderer::currentTime*0.0001+Input::mouseY*0.01) + yMid;
viewLocation.y=3*cos(Input::mouseX*0.001*Renderer::currentTime*0.0001);

int oldDrawingSize=Input::savedDrawings.size();
Input::getAllDrawings();
//if (oldDrawingSize<Input::savedDrawings.size())
//  {
//    loadNewScan();
//  }
}


