#include "plugin.h"

IDirect3DDevice9* Device;
LPD3DXLINE Line;
D3DXVECTOR3 D3DLine[500];

void Init(IDirect3DDevice9* device);
void Quit(void);
void Render(PluginData* Data);

Manifest MosaicPlugin = {
	PLUGIN_VER, 
	PLUGIN_NAME,
	PLUGIN_DESC,
	Init,        
	Quit,        
	Render,
	&Properties
};
 
void Init(IDirect3DDevice9* device) 
{
	Device = device;

	if (Device != NULL) 
	{
		D3DXCreateLine(Device, &Line);
		Line->SetAntialias(true);
	}
}

 
void Quit() 
{
	if (Line != NULL)
	{
		Line->Release();
		Line = NULL;
	}
}

void Render(PluginData* Data)
{
	if (Device != NULL) 
	{
		int spectrumDetail = 0;
		float spectrumSpacing = 0.0f;

		switch (Properties.Resolution)
		{
			case RES_SIMPLE:
					Line->SetWidth(8.0f);
					spectrumDetail = 100;
					spectrumSpacing = 0.3f;
				break;
			case RES_DETAILED:
					Line->SetWidth(1.0f);
					spectrumDetail = 500;
					spectrumSpacing = 2.0f;
				break;
			default:
					Line->SetWidth(2.5f);
					spectrumDetail = 250;
					spectrumSpacing = 0.7f;
				break;
		}

		D3DXMATRIX finalMatrix = Data->World;

		for(int i = 0; i < spectrumDetail; i++)
		{
			D3DLine[i].x = i/spectrumSpacing;
			D3DLine[i].y = (Data->Audio.Spectrum[i]*800.0f);
			D3DLine[i].z = 0.0f;
		}

		for(int i = 0; i < 2; i++)
		{
			D3DXMatrixRotationY(&finalMatrix, i*D3DX_PI);
			
			finalMatrix *= Data->World;
			Line->Begin();

				Line->DrawTransform(D3DLine, spectrumDetail, &finalMatrix, D3DXCOLOR(Properties.Color.R,
					Properties.Color.G,
					Properties.Color.B,
					Properties.Color.A));

				D3DXMATRIX finalZ;
				D3DXMatrixRotationZ(&finalZ, D3DX_PI);
				finalZ *= finalMatrix;

				Line->DrawTransform(D3DLine, spectrumDetail, &finalZ, D3DXCOLOR(Properties.Color.R,
					Properties.Color.G,
					Properties.Color.B,
					Properties.Color.A));

			Line->End();
		}
	}
}
 
extern "C" __declspec(dllexport) Manifest* Plugin() {
	return &MosaicPlugin;
}