//	MADButton.cpp - The collidable MADButton class.
//  ----------------------------------------------------------------------------
//	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 "drawing/Drawer.h"
#include "ColourHelpers.h"
#include "MiscHelper.h"
#include "MADButton.h"
#include "Random.h"
#include "Bullet.h"
#include "Tank.h"

#include <cassert>

//------------------------------------------------------------------------------
MADButton::MADButton(const TwoFloats& pos):
Collidable(L"MADButton"),
position(pos),
cracked(0),
crackColour(1.0f, 0.0f, 0.0f),
crackTweener(crackColour, ThreeFloats(0.0f, 0.0f, 1.0f), 0.1f),
spokesRot(0.0f),
bulletCollection(false),
collected(false),
colour(0.1875f)
{
	
}

//------------------------------------------------------------------------------
MADButton::~MADButton()
{

}

//------------------------------------------------------------------------------
void MADButton::update(const TwoFloats& arenaTopLeft,
					   const TwoFloats& arenaBottomRight)
{
	ThreeFloats tempCol;

	if(!crackTweener.getFinished())
		crackTweener.tick();
	else
	{
		tempCol.x = Random::getGlobal().getRandomFloat();
		tempCol.y = 0.8f;
		tempCol.z = 0.66f;
		tempCol = ColourHelpers::hslToRgb(tempCol);
		crackTweener.reset(tempCol);
	}

	spokesRot += 0.0025f + (0.0175f * ((float)cracked*0.25f));
	if(spokesRot > (2.0f * NIALL_PI))
		spokesRot -= (2.0f * NIALL_PI);
}

//------------------------------------------------------------------------------
void MADButton::draw(shared_ptr<Drawer>& d,
					 float timestepInterp,
					 const ThreeFloats& leftCol,
					 const ThreeFloats& rightCol,
					 const ThreeFloats& borderCol,
					 const ThreeFloats& backCol)
{
	const float interp(position.x/d->getDrawingAreaSize().x);
	ThreeFloats col(rightCol * interp);
	float radius;

	col += leftCol * (1.0f-interp);

	if(cracked > 0)
	{
		radius = 0.25f + (0.75f * (((float)cracked * 0.25f)));

		d->drawRotImage(L"RadialSpokes",
						position-(256.0f * radius),
						spokesRot,
						256.0f,
						radius,
						crackColour,
						0.75f);
		/*d->drawRotImage(L"RadialSpokes",
						position-(256.0f * radius),
						spokesRot + (0.5f * NIALL_PI),
						256.0f,
						radius,
						ThreeFloats(crackColour.y, crackColour.z, crackColour.x),
						0.75f);*/
	}

	//Draw the icon.
	d->drawImage(L"MADButtonFloor",
				 position-27.5f,
				 1.0f,
				 backCol);
	d->drawImage(L"MADButtonBottom",
				 TwoFloats(position.x-15.0f, position.y-15.5f),
				 1.0f,
				 borderCol);
	d->drawImage(L"MADButtonTop",
				 TwoFloats(position.x-18.5f, position.y-19.5f),
				 1.0f,
				 ThreeFloats(col.y, col.z, col.x));

	if(cracked < 4)
	{
		//Draw the glass above it.
		//d->drawImage(L"MADButtonGlass", TwoFloats(position.x-27.5f, position.y+27.5f), TwoFloats(1.0f, -1.0f));
		//...any cracks...
		switch(cracked)
		{
			case 0:
				d->drawImage(L"MADButtonGlass", position-27.5f);
				break;
			case 1:
				d->drawImage(L"MADButtonCracks1", position-27.5f);
				break;
			case 2:
				d->drawImage(L"MADButtonCracks2", position-27.5f);
				break;
			case 3:
				d->drawImage(L"MADButtonCracks3", position-27.5f);
				break;
		}
		//...and finally the outer wall.
		d->drawImage(L"MADButtonWall",
					 position-30.0f,
					 1.0f,
					 col,
					 1.0f);
	}
}

//------------------------------------------------------------------------------
void MADButton::setBulletCollection()
{
	bulletCollection = true;
}

//------------------------------------------------------------------------------
bool MADButton::interestedInCollisionsWith(const std::wstring& entity) const
{
	if(cracked >= 4)
	{
		if((bulletCollection && (entity == L"Bullet")) || (entity == L"Tank"))
			return true;
		else
			return false;
	}
	else
		return true;
}

//------------------------------------------------------------------------------
bool MADButton::collide(const shared_ptr<Collidable>& other,
						GamePlayState& state)
{
	const std::wstring& n(other->getName());

	if(cracked >= 4)
	{
		if((bulletCollection && (n == L"Bullet")) ||
		   (n == L"Tank"))
		{
			shared_ptr<Tank> t(dynamic_pointer_cast<Tank>(other));

			if(t)
				collectorColour = t->getColour();
			else //It was collected by a bullet.
			{
				shared_ptr<Bullet> b(dynamic_pointer_cast<Bullet>(other));

				assert(b != 0);

				collectorColour = b->getColour();
			}

			collected = true;
		}
	}
	else if(n == L"Bullet")
		++cracked;

	return true;
}
