#ifndef FIREWORKSSYSTEM_H
#define FIREWORKSSYSTEM_H

#include "ParticleSystem.h"
#include <cmath>

static const double pi = 3.14159265358979323846;

class FireworksSystem : public ParticleSystem {
protected:
	//This variable stores the last time that the fireworks erupted.
	float lasteruption;
public:
	FireworksSystem(const unsigned int maxpart) : ParticleSystem(maxpart),
	lasteruption(0.0f) {}

	virtual void updateBehavior() {

		//Different types of fireworks.
		int fireworktype = rand() % 4;

		static float randdir=(-100 + (rand() % 201)) / 50.0f;
		static float lasttrail = 0.0f;
		static unsigned int nexttime = (rand() % 2501) + 500;
		float randrho = 1.0f;

		if (lastupdate - lasteruption < nexttime &&
		lastupdate-lasttrail > 25) {
			lasttrail = lastupdate;
			
			unsigned int numparticles = 1;
			Particle* newpart = emit(numparticles);

			for (unsigned int pindex = 0; pindex < numparticles;
			pindex++) {
				newpart[pindex].color.r=1.0f;
				newpart[pindex].color.g=1.0f;
				newpart[pindex].color.b=1.0f;
				newpart[pindex].color.a = 1.0f;

				newpart[pindex].timetolive = 1.0f;
				newpart[pindex].size = 0.3f;
				newpart[pindex].velocity.y = 0.0f;
				newpart[pindex].velocity.z = 0.0f;
				newpart[pindex].velocity.x = randdir;
				newpart[pindex].acceleration.y=-2.03;
				newpart[pindex].pos =
				ParticleVector(0.0f, -1.0f, 0.0f);
				newpart[pindex].pos.y +=
				(lastupdate - lasteruption) / 1200.0f;

				newpart[pindex].Start();
			}
		}
		else if (lastupdate - lasteruption >= nexttime) {
			unsigned int numparticles = 1000;
			Particle* newpart = emit(numparticles);

			randrho = (rand() % 120 / 10.0f) + 0.2f;
			for (unsigned int pindex = 0; pindex < numparticles;
			pindex++) {
				unsigned char randlevel = rand() % 255;
				newpart[pindex].color.r=(randlevel) / 255.0f;
				randlevel = rand() % 255;
				newpart[pindex].color.g=(randlevel) / 255.0f;
				randlevel = rand() % 255;
				newpart[pindex].color.b=(randlevel) / 255.0f;
				newpart[pindex].color.a = 1.0f;

				newpart[pindex].timetolive = 3.0f;
				newpart[pindex].size = 0.3f;
				
				//float rho = (-1000+(rand() % 2001))/1000.0f;
				float theta = (rand() % 360) / 180.0f * pi;
				float phi = ((rand() % 360) / 180.0f * pi) - pi;
				float rho = randrho;

				if (fireworktype <= 1) {
					//Sphere.
					if (fireworktype == 1)
						rho = (rand() % 20 / 10.0f) + 0.2f;
				
					newpart[pindex].velocity.x = rho * sin(theta) * cos(phi);
					newpart[pindex].velocity.y = rho * sin(theta) * sin(phi);
					newpart[pindex].velocity.z = rho * cos(theta);
				}
				else if (fireworktype == 2) {
					//Torus.
					phi += pi;	//Phi is from 0 to 2pi for a torus.
					newpart[pindex].velocity.x = rho * cos(theta*2) * cos(phi);
					newpart[pindex].velocity.y = rho * cos(theta/2) * sin(phi);
					newpart[pindex].velocity.z = rho * sin(theta);
				}
				else {
					//Starr Rose (or my derivation of one, anyway).
					phi += pi;
					newpart[pindex].velocity.x = .1 * (2+.5*sin(theta*8)) * .5 * cos(theta+sin(16*theta)/16) * phi * phi;
					newpart[pindex].velocity.y = .4 * (2+.5*sin(theta*8)) * sin(theta+sin(16*theta)/16) * phi;
					newpart[pindex].velocity.z = .1 * (2+.5*sin(theta*8)) * .5 * phi * phi;
				}

				newpart[pindex].acceleration.y=-0.81;
				
				newpart[pindex].pos =
				ParticleVector(0.0f, -1.0f, 0.0f);
				newpart[pindex].pos.y +=
				(lastupdate - lasteruption) / 1200.0f;

				newpart[pindex].Start();
		
			}
			
			lasteruption = lastupdate;
			randdir=(-100 + (rand() % 201)) / 50.0f;
			nexttime = (rand() % 2501) + 500;
		}
	}
};

#endif

