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

Input::Input(const Input& other): _type(other._type), _positive(other._positive)
{
	std::memcpy(&_event, &other._event, sizeof(sf::Event));
}

Input& Input::operator=(const Input& other)
{
	std::memcpy(&_event, &other._event, sizeof(sf::Event));
	_type = other._type;
	return *this;
}

Input::Input(const sf::Keyboard::Key& key, int type): _type(type), _positive(true)
{
	_event.type = sf::Event::EventType::KeyPressed;
	_event.key.code = key;
}

Input::Input(const sf::Mouse::Button& button, int type): _type(type), _positive(true)
{
	_event.type = sf::Event::EventType::MouseButtonPressed;
	_event.mouseButton.button = button;
}

Input::Input(const int joystickButton, int type): _type(type), _positive(true)
{
	_event.type = sf::Event::EventType::JoystickButtonPressed;
	_event.joystickButton.button = joystickButton;
}

Input::Input(const sf::Joystick::Axis& axis, bool positive): _type(Type::RealTime|Type::Pressed), _positive(positive)
{
	_event.type = sf::Event::EventType::JoystickMoved;
	_event.joystickMove.joystickId = 0;
	_event.joystickMove.axis = axis;
}

float Input::test() const
{
	switch(_event.type)
	{
		case sf::Event::EventType::KeyPressed:
			if(_type & Type::Pressed)
				return sf::Keyboard::isKeyPressed(_event.key.code) ? 1.f : 0.f;
			break;
		case sf::Event::EventType::MouseButtonPressed:
			if(_type & Type::Pressed)
				return sf::Mouse::isButtonPressed(_event.mouseButton.button) ? 1.f : 0.f;
			break;
		case sf::Event::EventType::JoystickButtonPressed:
			if(_type & Type::Pressed)
				return sf::Joystick::isButtonPressed(0, _event.joystickButton.button) ? 1.f : 0.f;
			break;
		case sf::Event::EventType::JoystickMoved:
			if(_type & Type::Pressed)
			{
				float f = sf::Joystick::getAxisPosition(_event.joystickMove.joystickId, _event.joystickMove.axis) / 100.f;
				if(!_positive)
					f = -f;
				return f > 0.f ? f : 0.f;
			}
			break;
		default:
			break;
	}
	return 0.f;
}

bool Input::operator==(const sf::Event& event) const
{
	switch(event.type)
	{
		case sf::Event::EventType::KeyPressed:
			if(_type & Type::Pressed && _event.type == sf::Event::EventType::KeyPressed)
				return event.key.code == _event.key.code;
			break;
		case sf::Event::EventType::KeyReleased:
			if(_type & Type::Released && _event.type == sf::Event::EventType::KeyPressed)
				return event.key.code == _event.key.code;
			break;
		case sf::Event::EventType::MouseButtonPressed:
			if(_type & Type::Pressed && _event.type == sf::Event::EventType::MouseButtonPressed)
				return event.mouseButton.button == _event.mouseButton.button;
			break;
		case sf::Event::EventType::MouseButtonReleased:
			if(_type & Type::Released && _event.type == sf::Event::EventType::MouseButtonPressed)
				return event.mouseButton.button == _event.mouseButton.button;
			break;
		case sf::Event::EventType::JoystickButtonPressed:
			if(_type & Type::Pressed && _event.type == sf::Event::EventType::JoystickButtonPressed)
				return event.joystickButton.button == _event.joystickButton.button;
			break;
		case sf::Event::EventType::JoystickMoved:
			if(_type & Type::Pressed && _event.type == sf::Event::EventType::JoystickMoved)
				return event.joystickMove.position == _event.joystickMove.position;
			break;
		default: 
			break;
	}
	return false;
}
bool Input::operator==(const Input& other) const
{
	return _type == other._type and other == _event;
}