//	World1.cpp - One of the worlds.
//  ----------------------------------------------------------------------------
//	This file is part of 'NiallsAVLib', base code for any kind of audiovisual
//	apps.
//	Copyright (C) 2012  Niall Moody
//	
//	This program is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.
//
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program.  If not, see <http://www.gnu.org/licenses/>.
//	----------------------------------------------------------------------------

#include "World1.h"
#include "Random.h"

//------------------------------------------------------------------------------
World1::World1():
World(ThreeFloats(50.0f/255.0f, 56.0f/255.0f, 180.0f/255.0f),
	  ThreeFloats(215.0f/255.0f, 84.0f/255.0f, 170.0f/255.0f),
	  ThreeFloats(185.0f/255.0f, 181.0f/255.0f, 255.0f/255.0f),
	  ThreeFloats(215.0f/255.0f, 84.0f/255.0f, 84.0f/255.0f),
	  ThreeFloats(1.0f, 0.6f, 0.38f),
	  TwoFloats(1920.0f, 0.0f)),
mousePos(-1.0f),
done(false)
{
	int i, j;
	int correctI, correctJ;

	correctI = (int)(Random::getGlobal().getRandomFloat() * 2.9999f);
	correctJ = (int)(Random::getGlobal().getRandomFloat() * 1.9999f);

	outerCols[correctJ][correctI] = ThreeFloats(215.0f/255.0f, 84.0f/255.0f, 170.0f/255.0f);
	middleCols[correctJ][correctI] = ThreeFloats(185.0f/255.0f, 181.0f/255.0f, 255.0f/255.0f);
	innerCols[correctJ][correctI] = ThreeFloats(215.0f/255.0f, 84.0f/255.0f, 84.0f/255.0f);

	for(j=0;j<2;++j)
	{
		for(i=0;i<3;++i)
		{
			if(!((i == correctI) && (j == correctJ)))
			{
				outerCols[j][i].x = Random::getGlobal().getRandomFloat();
				outerCols[j][i].y = Random::getGlobal().getRandomFloat();
				outerCols[j][i].z = Random::getGlobal().getRandomFloat();
				
				middleCols[j][i].x = Random::getGlobal().getRandomFloat();
				middleCols[j][i].y = Random::getGlobal().getRandomFloat();
				middleCols[j][i].z = Random::getGlobal().getRandomFloat();
				
				innerCols[j][i].x = Random::getGlobal().getRandomFloat();
				innerCols[j][i].y = Random::getGlobal().getRandomFloat();
				innerCols[j][i].z = Random::getGlobal().getRandomFloat();
			}
		}
	}

	//Make sure the mouse isn't right on the correct colour from the start.
	correctPos.x = 64.0f + ((float)correctI * ((1280.0f-128.0f)/2.0f));
	correctPos.y = 64.0f + ((float)correctJ * ((720.0f-128.0f)/2.0f));
	mousePos.x = Random::getGlobal().getRandomFloat() * 1280.0f;
	mousePos.y = Random::getGlobal().getRandomFloat() * 720.0f;
	while(mousePos.getDistance(correctPos) < 192.0f)
	{
		mousePos.x = Random::getGlobal().getRandomFloat() * 1280.0f;
		mousePos.y = Random::getGlobal().getRandomFloat() * 720.0f;
	}
	calcColours();
}

//------------------------------------------------------------------------------
World1::~World1()
{

}

//------------------------------------------------------------------------------
void World1::mouseMove(const TwoFloats& pos)
{
	if(!done)
	{
		mousePos = pos;
		calcColours();
	}
}

//------------------------------------------------------------------------------
void World1::subclassTick()
{
	if(mousePos.getDistance(correctPos) < 192.0f)
	{
		float tempf = getOverallScale();

		if(tempf < 0.66f)
		{
			tempf += 0.0025f;
			setOverallScale(tempf);
		}

		if(getOverallScale() >= 0.66f)
			done = true;
	}
}

//------------------------------------------------------------------------------
void World1::calcColours()
{
	TwoFloats tempPos;
	int mouseX, mouseY;
	ThreeFloats topRow[3];
	ThreeFloats bottomRow[3];
	ThreeFloats finalCols[3];

	if(mousePos.x < 64.0f)
		tempPos.x = 0.0f;
	else if(mousePos.x > (1280.0f-64.0f))
		tempPos.x = 2.0f;
	else
		tempPos.x = (2.0f*(mousePos.x-64.0f)/(1280.0f-128.0f));

	if(mousePos.y < 64.0f)
		tempPos.y = 0.0f;
	else if(mousePos.y > (720.0f-64.0f))
		tempPos.y = 1.0f;
	else
		tempPos.y = (mousePos.y-64.0f)/(720.0f-128.0f);

	mouseX = (int)tempPos.x;
	mouseY = (int)tempPos.y;

	tempPos.x -= (float)mouseX;
	tempPos.y -= (float)mouseY;

	if(tempPos.x == 0.0f)
	{
		topRow[0] = outerCols[mouseY][mouseX];
		topRow[1] = middleCols[mouseY][mouseX];
		topRow[2] = innerCols[mouseY][mouseX];
	}
	else
	{
		topRow[0] = outerCols[mouseY][mouseX] * (1.0f-tempPos.x);
		topRow[0] += outerCols[mouseY][mouseX+1] * tempPos.x;

		topRow[1] = middleCols[mouseY][mouseX] * (1.0f-tempPos.x);
		topRow[1] += middleCols[mouseY][mouseX+1] * tempPos.x;

		topRow[2] = innerCols[mouseY][mouseX] * (1.0f-tempPos.x);
		topRow[2] += innerCols[mouseY][mouseX+1] * tempPos.x;
	}

	if(tempPos.y == 0.0f)
		setRingColours(topRow);
	else
	{
		if(tempPos.x == 0.0f)
		{
			bottomRow[0] = outerCols[mouseY+1][mouseX];
			bottomRow[1] = middleCols[mouseY+1][mouseX];
			bottomRow[2] = innerCols[mouseY+1][mouseX];
		}
		else
		{
			bottomRow[0] = outerCols[mouseY+1][mouseX] * (1.0f-tempPos.x);
			bottomRow[0] += outerCols[mouseY+1][mouseX+1] * tempPos.x;

			bottomRow[1] = middleCols[mouseY+1][mouseX] * (1.0f-tempPos.x);
			bottomRow[1] += middleCols[mouseY+1][mouseX+1] * tempPos.x;

			bottomRow[2] = innerCols[mouseY+1][mouseX] * (1.0f-tempPos.x);
			bottomRow[2] += innerCols[mouseY+1][mouseX+1] * tempPos.x;
		}

		finalCols[0] = topRow[0] * (1.0f-tempPos.y);
		finalCols[0] += bottomRow[0] * tempPos.y;

		finalCols[1] = topRow[1] * (1.0f-tempPos.y);
		finalCols[1] += bottomRow[1] * tempPos.y;

		finalCols[2] = topRow[2] * (1.0f-tempPos.y);
		finalCols[2] += bottomRow[2] * tempPos.y;

		setRingColours(finalCols);
	}
}
