/**
 *  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 "Camera.hpp"

Camera::Camera(): _offset(0, 0), _twilightViewport(0, 0), _view()
{
	
}

void Camera::setTwilightViewport(const sf::Vector2f twilightViewport)
{
	_twilightViewport = twilightViewport;
}

void Camera::setTwilightViewport(float x, float y)
{
	_twilightViewport = sf::Vector2f(x, y);
}

void Camera::setSize(const sf::Vector2f size)
{
	_view.setSize(size);
}
void Camera::setSize(float x, float y)
{
	_view.setSize(x, y);
}
void Camera::setCenter(const sf::Vector2f center)
{
	_view.setCenter(center);
	if(center.x - _view.getSize().x / 2.f <= 0)
		_view.setCenter(_view.getSize().x / 2.f, _view.getCenter().y);
	if(center.y - _view.getSize().y / 2.f <= 0)
		_view.setCenter(_view.getCenter().x, _view.getSize().y / 2.f);
	_offset = center - _view.getCenter();
}
void Camera::setCenter(float x, float y)
{
	setCenter(sf::Vector2f(x, y));
}

void Camera::move(const sf::Vector2f& offset)
{	
	bool offsetX = false, offsetY = false;

	if(offset.x < 0)
	{
		if(offset.x + _offset.x > -_twilightViewport.x)
			offsetX = true;
		if(_view.getCenter().x - _view.getSize().x / 2.f + offset.x <= 0)
			offsetX = true;
	}
	else
	{
		if(offset.x + _offset.x < _twilightViewport.x)
			offsetX = true;
	}
		
	if(offset.y < 0)
	{
		if(offset.y + _offset.y > -_twilightViewport.y)
			offsetY = true;
		if(_view.getCenter().y - _view.getSize().y / 2.f + offset.y <= 0)
			offsetY = true;
	}
	else
	{
		if(offset.y + _offset.y < _twilightViewport.y)
			offsetY = true;
	}
	
	_offset.x += offset.x * offsetX;
	_offset.y += offset.y * offsetY;
	_view.move(offset.x * !offsetX, offset.y * !offsetY);
}

void Camera::move(float offsetX, float offsetY)
{
	move(sf::Vector2f(offsetX, offsetY));
}

const sf::Vector2f Camera::getSize() const
{
	return _view.getSize();
}

const sf::Vector2f Camera::getCenter() const
{
	return _view.getCenter();
}

const sf::View* Camera::getView() const
{
	return &_view;
}