//	PickupSplit.cpp - Split Pickup.
//  ----------------------------------------------------------------------------
//	This file is part of 'GameTemplate', template code for a game.
//	Copyright (C) 2011  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 "../tones/CopyTone.h"
#include "drawing/Drawer.h"
#include "DebugHeaders.h"
#include "PickupSplit.h"
#include "GameCounter.h"
#include "../Tone.h"

#include <sstream>
#include <cassert>

using namespace std;

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
PickupSplit::PickupSplit():
Pickup(L"Split")
{

}

//------------------------------------------------------------------------------
PickupSplit::~PickupSplit()
{

}

//------------------------------------------------------------------------------
shared_ptr<OverrideBehaviour> PickupSplit::getBehaviour() const
{
	return shared_ptr<OverrideBehaviour>(NiallNew BehaviourSplit(L"pickup-split"));
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
BehaviourSplit::BehaviourSplit(const wstring& icon):
OverrideBehaviour(icon),
pickupIcon(icon)
{

}

//------------------------------------------------------------------------------
BehaviourSplit::~BehaviourSplit()
{

}

//------------------------------------------------------------------------------
bool BehaviourSplit::moveTone(Tone& tone,
							  std::vector<TonePtr>& allTones,
							  float globalSpeed)
{
	if(needsInit)
	{
		TonePtr newTone(new CopyTone(tone));

		//Swipe the Tone up to the next rail if there is a rail there.
		if(tone.rail > 0)
		{
			tone.startSwipe(tone.position);
			tone.endSwipe(tone.position-32.0f);
		}

		//Swipe the new Tone down to the next rail if there is a rail there.
		if(tone.rail < (tone.numRails-1))
		{
			newTone->startSwipe(tone.position);
			newTone->endSwipe(tone.position+32.0f);
		}

		//If either of these is true we'll get into an infinite loop.
		assert((tone.rail != tone.newRail) || (newTone->rail != newTone->newRail));

		//Move them so they won't collide with each other.
		while(tone.getPosition().getDistance(newTone->getPosition()) < 20.0f)
		{
			tone.move(globalSpeed, allTones, true);
			newTone->move(globalSpeed, allTones);

			//Failsafe mechanism.
			if((tone.rail == tone.newRail) && (tone.rail > 0))
			{
				tone.startSwipe(tone.position);
				tone.endSwipe(tone.position-32.0f);
			}
			//Failsafe mechanism.
			if((newTone->rail == tone.newRail) && (newTone->rail < (newTone->numRails-1)))
			{
				newTone->startSwipe(tone.position);
				newTone->endSwipe(tone.position+32.0f);
			}
		}

		//Add the new Tone.
		allTones.push_back(newTone);

		needsInit = false;
	}

	return true;
}

//------------------------------------------------------------------------------
void BehaviourSplit::drawExtraInformation(DrawerPtr& d,
										  float timestepInterp,
										  const Tone& tone)
{
	//Draw the icon.
	d->drawImage(pickupIcon, tone.position-64.0f);
}
