/**
 *  Triangles
 *  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 "Utility.hpp"
#include "Circle.hpp"

Circle::Circle(sf::Color color, float scale): _size(10) 
{
	_arc.resize(100);
	for(unsigned i = 0; i < 100; ++i)
	{
		_arc[i].setColor(color);
		_arc[i].setRotation(3.6f * i);
		_arc[i].setScale(scale, scale);
	}
	_elapsedTime = sf::seconds(5.f);
}

Circle::~Circle()
{
	Echo::debug("Deleting a circle.");
	_arc.clear();
}

void Circle::setTexture(sf::Texture& texture)
{
	for(unsigned i = 0; i < 100; ++i)
	{
		_arc[i].setOrigin(texture.getSize().x / 2.f, 205.f);
		_arc[i].setTexture(texture);
	}
}

void Circle::setPosition(sf::Vector2f position)
{
	for(unsigned i = 0; i < 100; ++i)
		_arc[i].setPosition(position);
}

void Circle::setPosition(float x, float y)
{
	setPosition(sf::Vector2f(x, y));
}

void Circle::setRotation(float rotation)
{
	for(unsigned i = 0; i < 100; ++i)
		_arc[i].setRotation(3.6f * i + rotation);
}

void Circle::rotate(float rotation)
{
	for(unsigned i = 0; i < 100; ++i)
		_arc[i].rotate(rotation);
}

void Circle::setSize(unsigned size)
{
	if(size <= 100)
		_size = size;
}

void Circle::show()
{
	_elapsedTime = sf::Time::Zero;
}

void Circle::update(sf::Time delta)
{
	_elapsedTime += delta;
	float seconds = _elapsedTime.asSeconds();
	if(seconds >= 2.75f && seconds < 3.f)
	{
		sf::Color color = _arc[0].getColor();
		color.a = int((3.f - _elapsedTime.asSeconds()) * 255 * 4);
		for(unsigned i = 0; i < _size; ++i)
			_arc[i].setColor(color);
	}
	else if(seconds >= 3.f)
	{
		sf::Color color = _arc[0].getColor();
		color.a = 255;
		for(unsigned i = 0; i < _size; ++i)
			_arc[i].setColor(color);
	}
}

void Circle::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
	if(_elapsedTime.asSeconds() >= 3.f)
		return;

	for(unsigned i = 0; i < _size; ++i)
		target.draw(_arc[i]); 
}