#include <SDL/SDL.h>
#include <math.h>
#include "vector.h"
#include "render.h"
#include "main.h"
#include "panels.h"
#include "things.h"

extern SDL_Surface *panel1, *panel2;
extern SDL_Surface *screen;
extern image bin[3];
extern image selectball[2][3];

extern float ballgravity;

selection sbin[3];
selection sball[3][2];
int wasteballnum[3];

thing wasteball[MAXWASTEBALL];
int wasteball_num = 0;

void CreateWasteball(int x, int y, int col)
{
	wasteball[wasteball_num].exists = true;
	wasteball[wasteball_num].pos.x = x;
	wasteball[wasteball_num].pos.y = y;
	wasteball[wasteball_num].vel.x = 0;
	wasteball[wasteball_num].vel.y = 0;
	wasteball[wasteball_num].color = col;
	wasteball[wasteball_num].mass = 1.0;
	wasteball_num++;
}

void mouse_panel_check(int x, int y, bool clicked)
{
	int i, j;
	int x1, y1, x2, y2;
	int w, h;

	y-=PanelScreenHeight;

	for (i=0; i<3; i++)
	{
		for (j=0; j<2; j++)
		{
			w = selectball[j][i].width;
			h = selectball[j][i].height;
			x1 = sball[i][j].pos.x - w/2;
			y1 = sball[i][j].pos.y - h/2;
			x2 = sball[i][j].pos.x + w/2 - 1;
			y2 = sball[i][j].pos.y + h/2 - 1;

			if (x>x1 && x<x2 && y>y1 && y<y2)
			{
				sball[i][j].chosen = true;
				if (clicked && (sball[i][j].condition==1))
				{
					CreateWasteball(sball[i][j].pos.x, sball[i][j].pos.y+PanelScreenWidth, i);
					sball[i][j].condition = 0;
					wasteballnum[i]--;
				}
			}
			else
			{
			    sball[i][j].chosen = false;
			}
		}
	}
}

void UpdateWasteballs()
{
	thing *b;
	int i;
	for (i=0; i<wasteball_num; i++)
	{
		b = &wasteball[i];
		if (b->exists)
		{
			b->vel.x = 16 * ((sbin[b->color].pos.x - b->pos.x) / Vdist(&b->pos, &sbin[b->color].pos));
			b->vel.y = 16 * ((sbin[b->color].pos.y - b->pos.y) / Vdist(&b->pos, &sbin[b->color].pos));

			b->pos.x += b->vel.x;
			b->pos.y += b->vel.y;

			b->mass = Vdist(&b->pos, &sbin[b->color].pos) / 256.0;
			if (b->mass>1.0) b->mass = 1.0;
			if (b->mass<0.25) b->mass = 0.25;

			if (Vdist(&b->pos, &sbin[b->color].pos)<=8)
			{
				b->exists = false;
			}
		}
	}
}


void InitSelections()
{
	int i, j;
	for (i=0; i<3; i++)
	{
		sbin[i].pos.x = PanelScreenWidth + GameScreenWidth/4 + i*(GameScreenWidth/4);
		sbin[i].pos.y = PanelScreenHeight/2;
	}

	for (i=0; i<3; i++)
	{
		for (j=0; j<2; j++)
		{
			sball[i][j].pos.x = PanelScreenWidth/4 + j*(PanelScreenWidth/2);
			sball[i][j].pos.y = GameScreenHeight/4 + i*(GameScreenHeight/4);
			sball[i][j].condition = 0;
			sball[i][j].chosen = false;
		}
		wasteballnum[i] = 0;
	}
}

void InitPanelGfx()
{
	int x, y, c;
	unsigned int *vram = (unsigned int*)panel1->pixels;
	for (y=0; y<GameScreenHeight; y++)
	{
	    for (x=0; x<PanelScreenWidth; x++)
	    {
			c = 56 + sin(x/64.0)*32 + cos(y/128.0) * 32;
			if (c<0) c = 0;
			if (c>255) c = 255;
			*vram++ = (63<<16) | (c<<8) | 127;
		}
	}

	vram = (unsigned int*)panel2->pixels;
	for (y=0; y<PanelScreenHeight; y++)
	{
	    for (x=0; x<ScreenWidth; x++)
	    {
			c = y;
			*vram++ = (63<<16) | (c<<8) | 127;
		}
	}
}

void ShowBinsPanel(unsigned int *vram)
{
	int i;
	for (i=0; i<3; i++)
	{
	    RenderSprite(sbin[i].pos.x, sbin[i].pos.y, &bin[i], vram, ScreenWidth, PanelScreenHeight);
	}
}

void ShowBallsPanel(unsigned int *vram)
{
	int i, j;
	for (i=0; i<3; i++)
	{
		for (j=0; j<2; j++)
		{
		    RenderSprite(sball[i][j].pos.x, sball[i][j].pos.y, &selectball[sball[i][j].condition][i], vram, PanelScreenWidth, GameScreenHeight);
		}
	}
}

void ShowWasteBalls()
{
	unsigned int *vram = (unsigned int*)screen->pixels;
	int i;
	for (i=0; i<wasteball_num; i++)
	{
		if (wasteball[i].exists)
			RenderZoomSprite(wasteball[i].pos.x, wasteball[i].pos.y, wasteball[i].mass, wasteball[i].mass, &selectball[1][wasteball[i].color], vram, ScreenWidth, ScreenHeight, true);
	}
}

void ShowBallSelections()
{
	unsigned int *vram = (unsigned int*)screen->pixels;
	int i, j;
	int x1, y1, x2, y2;
	int w, h;
	int c;

	c = 192 + sin(SDL_GetTicks()/128.0)*63;
	c = (c<<16) | (c<<8) | c;

	for (i=0; i<3; i++)
	{
		for (j=0; j<2; j++)
		{
			if (sball[i][j].chosen)
			{
				w = selectball[j][i].width;
				h = selectball[j][i].height;
				x1 = sball[i][j].pos.x - w/2;
				y1 = sball[i][j].pos.y - h/2 + PanelScreenHeight;
				x2 = sball[i][j].pos.x + w/2 - 1;
				y2 = sball[i][j].pos.y + h/2 - 1 + PanelScreenHeight;
				RenderBoxLine(x1, y1, x2, y2, c, vram, ScreenWidth);
			}
		}
	}
}


void ShowPanels()
{
	unsigned int *vram = (unsigned int*)screen->pixels;
	unsigned int *vpanel1 = (unsigned int*)panel1->pixels;
	unsigned int *vpanel2 = (unsigned int*)panel2->pixels;
	ShowBallsPanel(vpanel1);
	ShowBinsPanel(vpanel2);
}

void CollectWasteball(char color)
{
	int i;
	for (i=0; i<2; i++)
        if (sball[color][i].condition==0)
        	break;
	if (i==2) printf("error\n");
	else
		sball[color][i].condition = 1;
}

void InitPanels()
{
	InitPanelGfx();
	InitSelections();
}

void RunPanelThings()
{
    UpdateWasteballs();
}
