/**
 *  one room arena
 *  Copyright (C) 2016 POSITIVE MENTAL ATTITUDE
 *
 *  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, version 3 of the License.
 *
 *  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 <ctime>
#include "Powerup.hpp"
#include "Map.hpp"
#include "Player.hpp"

Powerup::Powerup(std::vector<Texture>* textures)
{
	_type = rand() % count;
	this->setTexture(textures->at(_type));
	this->setOrigin((Vector2f)textures->at(_type).getSize() / 2.f);
}

const int Powerup::getType() const
{
	return _type;
}

PowerupManager::PowerupManager(Map* map)
{
	_map = map;
	
	_decentlyAllocatedTextures.resize(Powerup::count);
	_decentlyAllocatedTextures[Powerup::Haste].loadFromFile("data/haste.png");
	_decentlyAllocatedTextures[Powerup::Fatman].loadFromFile("data/fatman.png");
	_decentlyAllocatedTextures[Powerup::Steroids].loadFromFile("data/powerup.png");
	_decentlyAllocatedTextures[Powerup::RateOfFire].loadFromFile("data/rof.png");
}

void PowerupManager::registerPlayer(Player* player)
{
	_ptr.push_back(player);
}

void PowerupManager::deregisterPlayer(Player* player)
{
	for(unsigned i = 0; i < _ptr.size(); ++i)
	{
		if(_ptr[i] == player)
		{
			_ptr.erase(_ptr.begin() + i);
			break;
		}
	}
}

void PowerupManager::logic()
{
	static Clock clock;
	
	if(clock.getElapsedTime().asSeconds() > 2.f && _powerups.size() < 10)
	{
		clock.restart();
		_powerups.emplace_back(&_decentlyAllocatedTextures);
		int x, y;
		do {
			x = rand() % (_map->getSize().x - 200) + 100;
			y = rand() % (_map->getSize().x - 200) + 100;
			_powerups.back().setPosition(x, y);
		} while(_map->collision(Vector2f(x, y)));
	}
	
	collision();
}

const unsigned PowerupManager::getSize() const
{
	return _powerups.size();
}

const Powerup& PowerupManager::getPowerup(unsigned i) const
{
	return _powerups[i];
}

void PowerupManager::collision()
{
	for(unsigned i = 0; i < _ptr.size(); ++i)
	{
		for(unsigned j = 0; j < _ptr[i]->getPointCount(); ++j)
		{
			Vector2f coords = _ptr[i]->getTransform().transformPoint(_ptr[i]->getPoint(j));
			for(unsigned k = 0; k < _powerups.size(); ++k)
			{
				if(coords.x > _powerups[k].getPosition().x - _powerups[k].getOrigin().x
				&& coords.x < _powerups[k].getPosition().x + _powerups[k].getOrigin().x
				&& coords.y > _powerups[k].getPosition().y - _powerups[k].getOrigin().y
				&& coords.y < _powerups[k].getPosition().y + _powerups[k].getOrigin().y)
				{
					switch(_powerups[k].getType())
					{
						case Powerup::Haste: _ptr[i]->hasten(); break;
						case Powerup::Fatman: _ptr[i]->addNuke(); break;
						case Powerup::Steroids: _ptr[i]->growStronger(); break;
						case Powerup::RateOfFire: _ptr[i]->rateOfFire(); break;
						default: break;
					}
					_powerups.erase(_powerups.begin() + k);
				}
			}
		}
	}
}

void PowerupManager::draw(RenderTarget& target, RenderStates states) const
{
	for(unsigned i = 0; i < _powerups.size(); ++i)
		target.draw(_powerups[i], states);
}
