#ifndef _GRAPHICS_H
#define _GRAPHICS_H

#include		<windows.h>
#include		<d3dx9.h>
#include		<stdio.h>
#include		<vector>
#include		<string>
#include		"output.h"
#include		"camera.h"
#include		"manifest.h"
#include		"resource.h"
#include		"sprite.h"
#include		"utils.h"
#include		"visualisation.h"

#define WIN32_LEAN_AND_MEAN

// Enum to reference the different display buffers.
typedef enum _BUFFER_TYPE {

		TRANSITION_BUFFER		= -1, // Transitioning.
		FRONT_BUFFER			= 0, // Front buffer.
		BACK_BUFFER				= 1 // Back buffer.

} BUFFER_TYPE;

// The vertex definition for the viewframe.
struct Vertex
{
	Vertex() { }
	Vertex(float x, float y, float z, float u, float v)
	{
		_x = x;   _y = y;   _z = z;
		_u = u;   _v = v;
	}

	float _x, _y, _z, _u, _v;
	static const DWORD FVF = D3DFVF_XYZ | D3DFVF_TEX1;
};

// Structure that defines the various transition properties.
typedef struct {

		float					Increment; // Increment value, between 0.0f and 1.0f.
		float					Smoothness; // The smoothness of the radial effect.
		BOOL					Active; // If the transition is active.
		BOOL					Direction; // The direction of the transition, (e.g. going from front to back or vice versa)
		int						Count; // The number of transitions so far.
		int						Interval; // The interval ticks between transitions.
		float					Speed; // The speed of the transition effect.

} TransitionAttributes;

class GraphicsManager
{
	private: 
	
		IDirect3DDevice9*       Device; // The main interface to the Direct3D Device.
		ID3DXEffect* 			Effect;	// The post-processing effect.

		OutputManager*			Output; // Link to output manager.
		Camera					ViewFrameCamera; // The camera that looks at the viewframe.
		Camera					WorldCamera; // The camera inside the viewframe that looks at the visualisation.

		D3DXMATRIX				World, View, Projection; // The World, View and Projection matrices.

		IDirect3DSurface9*		CoreBuffer; // Where the back buffer is saved before the rendering process.

		Visualisation*			FrontVisualisation; // The front visualisation.
		Visualisation*			BackVisualisation; // The back visualisation.
		std::string				GetVisualisationName(Visualisation* visualisation); // Gets the visualisation name.
		Visualisation*			GetActive(); // Gets the active visualisation.

		ID3DXMesh*				ViewFrame; // The main viewframe.
		void					CreateViewFrame(); // Creates the viewframe mesh.
		void					RenderViewFrame(); // Renders the viewframe.

		float					StartTime; // The tick time that the transition started.
		float					CurrentTime; // The current transition time.

	public: 

		GraphicsManager();
		~GraphicsManager();

		D3DPRESENT_PARAMETERS	Parameters; // Direct3D presentation parameters and properties.
		BOOL					Init(HWND hWnd, int width, int height, bool fullscreen); // Initialise the Direct3D Device.
		BOOL					Setup(); // Setup the graphics objects.

		VOID					BeginRender(); // Begin the rendering process.	

		VOID					SetVisualisation(BUFFER_TYPE Buffer, Visualisation* visualisation); // Apply a visualisation to a buffer.
		VOID					SetSurface(BUFFER_TYPE Buffer); // Set the active buffer.

		VOID					EndRender(); // End the rendering process.

		VOID					Update(); // Update the graphics.
		VOID					Release(); // Release all objects.						
		IDirect3DDevice9*		GetDevice(); // Return the Direct3D Device.	

		VOID					SetOutput(OutputManager* output); // Set the output manager.

		VOID					BeginTransition(); // Begin a transition at a random position.
		VOID					BeginTransition(D3DXVECTOR4* position); // Begin a transition at a specific position.

		IDirect3DSurface9*		GetBuffer(BUFFER_TYPE Buffer); // Get the surface of a buffer.
		BUFFER_TYPE				ActiveBuffer; // The currently active buffer.

		TransitionAttributes	Transition; // The transition properties structure.
};

#endif




