#include "DiManager.h"
#include "Util.h"
#include "Geometry.h"
#include <d3dx10.h>

class Camera
{
	private:
	Camera(void);
	static Camera* instance;
	float yaw,pitch,roll,strafeLeftRightDir,moveForwardBackDir,moveUpDownDir,aspectR;
	float fov,zNear,zFar;
	float timeElapsed, curTime, timeStep, timeSinceControlling; 
	float recordInterval; 
	float forwardAccelerator, rightAccelerator; 
	bool isRecording, lastStateRKey, lastStatePKey, lastStateF6Key, lastStateF9Key, isCameraControlled, clearVectors;
	vector<vector<D3DXVECTOR3>> record_data;
	vector<float> record_time; 
	int record_index; 
    Util* util; 

	D3DXMATRIX viewMatrix;
	D3DXMATRIX projectionMatrix;
	D3DXMATRIX lightViewMatrix;
	D3DXMATRIX lightProjectionMatrix;
	D3DXMATRIX rotationMatrix;

	D3DXVECTOR3 defaultUp, defaultView;
	D3DXVECTOR3 position, lookAt, upVector;
	D3DXVECTOR3 forwardVector, rightVector;
	D3DXVECTOR2 mousePosition;
	D3DXVECTOR2 oldMousePosition;
	D3DXPLANE   frustum[6];
	
	DiManager* diManager;
	
	void buildFrustum();
	public:
	void update(float dt);
	void setLookAt(D3DXVECTOR3 look);
	void setPosition(D3DXVECTOR3 pos);
	void setUpVector(D3DXVECTOR3 pos);
	void strafeLeftRight(float x);
	void moveForwardBack(float z);
	void moveUpDown(float z);
	void setMousePosition(int x, int y);
	void checkCameraKeys();
	void stopRecording(); 
	void startRecording(); 
	void fetchCameraData();
	void controlCamera(float* time);
	void interpolateCatMullRom(int p0, int p1, int p2, int p3, float s); 
	D3DXVECTOR3* getEyePosition();
	D3DXMATRIX* getViewMatrix();
	D3DXMATRIX* getProjectionMatrix();
	D3DXMATRIX* getLightViewMatrix();
	D3DXMATRIX* getLightProjectionMatrix();
	void updateProjection(float aspect);
	void buildLightViewAndProjection();
	bool testSphereInFrustum(BoundingSphere bs);

	static Camera* getInstance(void)
	{
		if(!instance)
			instance = new Camera();
		return instance;
	}
	static void releaseCamera()
	{
		delete(instance);
		instance = NULL;
	}
	
};
