#include <SDL/SDL.h>

#include "project_run.h"
#include "project_script.h"
#include "part_volume1.h"
#include "part_volume3.h"

#include "fx_plasma.h"
#include "fx_blobs.h"
#include "fx_fire.h"
#include "fx_water.h"


SDL_Event event;

// ==================
// ==== Controls ====
// ==================

int previous_mouse_x = 0;
int previous_mouse_y = 0;
int LMB_pressed = false;
int RMB_pressed = false;

const float iso_add = 0.025f;

void MouseActions(int x, int y)
{
    if (LMB_pressed)
    {
        rot_vecs.x += -(y - previous_mouse_y) / 64.0f;
        rot_vecs.y += -(x - previous_mouse_x) / 64.0f;
	}

    if (RMB_pressed)
    {
        cam_zoom += (y - previous_mouse_y) / 1.0f;
        //if (cam_zoom < -256.0f) cam_zoom = -64.0f;
        //if (cam_zoom > 256.0f) cam_zoom = 64.0f;
    }
}

void ControlsHandling()
{
	while (SDL_PollEvent(&event))
	{
		switch (event.type)
		{
			case SDL_QUIT:
				quit=true;
			break;

			case SDL_KEYDOWN:
				if (event.key.keysym.sym==SDLK_ESCAPE)
				{
					quit=true;
				}

				if (event.key.keysym.sym==SDLK_SPACE)
				{
					NewPart((GetPart() + 1) % ENDPART);
				}

				if (event.key.keysym.sym==SDLK_a)
				{
					alpha_on = !alpha_on;
				}

				if (event.key.keysym.sym==SDLK_v)
				{
					limit_speed = !limit_speed;
				}

				if (event.key.keysym.sym==SDLK_f)
				{
					fx_on = !fx_on;
				}

				if (event.key.keysym.sym==SDLK_p)
				{
					curr_fx = (curr_fx + 1) % fx_num;

					switch(curr_fx)
					{
						case 0:
							iso_surf = 0.8f;
							InitFxPlasmaColors();
						break;

						case 1:
							iso_surf = 0.3f;
							InitFxBlobsColors();
						break;

						case 2:
							iso_surf = 0.125f;
							InitFxFireColors();
						break;

						case 3:
							iso_surf = 0.25f;
							InitFxWaterColors();
						break;
					}
				}
			break;

			case SDL_MOUSEMOTION:
				MouseActions(event.motion.x, event.motion.y);
				previous_mouse_x = event.motion.x;
				previous_mouse_y = event.motion.y;
			break;

			case SDL_MOUSEBUTTONUP:
				if (event.button.button == SDL_BUTTON_LEFT)
					LMB_pressed = false;
				if (event.button.button == SDL_BUTTON_RIGHT)
					RMB_pressed = false;
			break;

			case SDL_MOUSEBUTTONDOWN:
				if (event.button.button == SDL_BUTTON_LEFT)
					LMB_pressed = true;
				if (event.button.button == SDL_BUTTON_RIGHT)
					RMB_pressed = true;

				if (event.button.button == SDL_BUTTON_WHEELUP)
				{
					iso_surf += iso_add;
					if (iso_surf > 1.0f) iso_surf = 1.0f;
					printf("%f\n",iso_surf);
				}
				if (event.button.button == SDL_BUTTON_WHEELDOWN)
				{
					iso_surf -= iso_add;
					if (iso_surf < 0.0f) iso_surf = 0.0f;
					printf("%f\n",iso_surf);
				}
			break;
		}
	}
}

void Controls()
{
	ControlsHandling();
}
