#include <SDL/SDL.h>
#include <math.h>

#include "demo_run.h"
#include "demo_script.h"

#include "part_bump.h"
#include "part_water.h"
#include "part_multieffects.h"

SDL_Event event;

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

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

void MouseActions(int x, int y)
{
    if (LMB_pressed)
    {
		multieffects_position_x -= (x - previous_mouse_x) * (0.0001 * multieffects_zoom);
		multieffects_position_y -= (previous_mouse_y - y) * (0.0001 * multieffects_zoom);
	}

    if (RMB_pressed)
    {
		multieffects_zoom -= (previous_mouse_y - y) * (0.005 * multieffects_zoom);
		multieffects_rotate += (x - previous_mouse_x) * 0.0025;

		if (multieffects_zoom > 128.0) multieffects_zoom = 128.0;
		//if (multieffects_zoom < 0.000001) multieffects_zoom = 0.000001;
    }
}

void Controls()
{
	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_c)
				{
					if (GetPart()==MULTIEFFECTS)
					{
						printf("%f %f\n", multieffects_position_x, multieffects_position_y);
						multieffects_position_x = floor(multieffects_position_x * 16.0) / 16.0 + (1.0 / 32.0);
						multieffects_position_y = floor(multieffects_position_y * 16.0) / 16.0 + (1.0 / 32.0);
						printf("%f %f\n", multieffects_position_x, multieffects_position_y);
					}
				}
				if (event.key.keysym.sym==SDLK_f)
				{
					if (GetPart()==MULTIEFFECTS)
					{
						if (multieffects_freeze_time==1.0)
							multieffects_freeze_time = 0.0;
						else
							multieffects_freeze_time = 1.0;
					}
				}
				if (event.key.keysym.sym==SDLK_r)
				{
					if (GetPart()==BUMP) bump_precalcs();

					if (GetPart()==MULTIEFFECTS)
					{
						if (multieffects_enable_rotate==1.0)
							multieffects_enable_rotate = 0.0;
						else
							multieffects_enable_rotate = 1.0;
					}
				}
				if (event.key.keysym.sym==SDLK_v)
				{
					limit_speed = !limit_speed;
				}
				if (event.key.keysym.sym==SDLK_b)
				{
					if (GetPart()==WATER) water_bump_state = !water_bump_state;
				}
				if (event.key.keysym.sym==SDLK_KP_PLUS)
				{
					if (GetPart()==WATER)
					{
						if (water_drops_show >= 256)
						{
							water_drops_show += 256;
							if (water_drops_show > 16384) water_drops_show = 16384;
						}
						else
						{
							water_drops_show += 16;
						}
							
						printf("drops: %d\n",water_drops_show);
					}
				}
				if (event.key.keysym.sym==SDLK_KP_MINUS)
				{
					if (GetPart()==WATER)
					{
						if (water_drops_show > 256)
						{
							water_drops_show -= 256;
						}
						else
						{
							water_drops_show -= 16;
							if (water_drops_show < 16) water_drops_show = 16;
						}

						printf("drops: %d\n",water_drops_show);
					}
				}
			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)
				{
					//multieffects_rotate += 0.25;
				}
				if (event.button.button == SDL_BUTTON_WHEELDOWN)
				{
					//multieffects_rotate -= 0.25;
				}
			break;
		}
	}
}
