/**
 *  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 <sstream>
#include "GuiButton.hpp"

void GuiButton::flip(bool val)
{
	_flip = val;
	setRotation(_flip ? -97.5f : 97.5f);
	setPosition(getPosition());
} 

void GuiButton::setCharacterSize(unsigned size)
{
	_caption.setCharacterSize(size);
}

void GuiButton::create(const sf::Font& font, std::string caption, bool* value)
{
	_string = caption;
	_caption.setFont(font); 
	_caption.setColor(sf::Color::Black);
	_value = value; 
	recreate(); 
}

void GuiButton::setColor(sf::Color color)
{
	color.a = (*_value ? 255 : 80);
	Triangle::setColor(color);
}

void GuiButton::recreate() 
{
	std::ostringstream oss;
	oss << _string << ':' << (*_value ? "on" : "off");
	_caption.setString(oss.str());
}

void GuiButton::click(sf::Vector2i mouseXY)
{
	if(mouseIntersection(mouseXY))
		(*_value) = !(*_value);
	recreate();
}

void GuiButton::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
	states.transform *= getTransform();

	states.texture = (_texture);
    
	target.draw(_vertices, states);
	target.draw(_caption);
}

void GuiButton::setPosition(sf::Vector2f position)
{ 
	Triangle::setPosition(position);
	_caption.setPosition(position);
	_caption.setOrigin(_caption.getLocalBounds().width / 2.f + (_flip ? -60.f : 60.f), _caption.getLocalBounds().height / 2.f + 10.f);
}

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