#include "generic_frame.h"
#include "types.h"

GenericFrame::GenericFrame(String name) : Frame(name) {
	// Setup input
	size_t nButtons = Player_Last_Action;
	playerInput = spInputVirtual(new InputVirtual(nButtons, 2));
}

void GenericFrame::setInputBindings()
{
	playerInput->clearBindings();

	// Keyboard bind0ing
	spInputKeyboard key = Input::getKeyboard();
	playerInput->bindAxisButton(0, key, SDLK_LEFT, SDLK_RIGHT);
	playerInput->bindAxisButton(1, key, SDLK_UP, SDLK_DOWN);

	playerInput->bindButton(Player_Action1,			key, SDLK_z);
	playerInput->bindButton(Player_Action1,			key, SDLK_y);
	playerInput->bindButton(Player_Action1,			key, SDLK_RETURN);
	playerInput->bindButton(Player_Action2,			key, SDLK_x);
	playerInput->bindButton(Player_Action3,			key, SDLK_LSHIFT);
	playerInput->bindButton(Player_Action4,			key, SDLK_SPACE);
	playerInput->bindButton(Player_Menu,			key, SDLK_ESCAPE);
	playerInput->bindButton(Player_Restart,			key, SDLK_F5);
	playerInput->bindButton(Player_Toggle_Debug,	key, SDLK_F6);
	playerInput->bindButton(Player_Freeze,			key, SDLK_F7);
	playerInput->bindButton(Player_Step,			key, SDLK_F8);

	// Joystick binding
	spInputJoystick joy = Input::getFirstJoystick();
	if (joy) {
		// Hat
		auto hat = joy->getHat(0);
		if (hat) {
			playerInput->bindAxisButton(0, hat, 3, 1);	// Bind X axis to hat 3 and 1
			playerInput->bindAxisButton(1, hat, 0, 2);	// Bind Y axis to hat 0 and 2
		}

		// Left stick
		playerInput->bindAxis(0, joy, 0);
		playerInput->bindAxis(1, joy, 1);

		// Buttons
		playerInput->bindButton(Player_Action1,			joy, joy->getButtonAtPosition('S'));
		playerInput->bindButton(Player_Action2,			joy, joy->getButtonAtPosition('E'));
		playerInput->bindButton(Player_Action2,			joy, joy->getButtonAtPosition('W'));
		//playerInput->bindButton(Player_Action3,			joy, joy->getButtonAtPosition('N'));
		//playerInput->bindButton(Player_Action4,			joy, joy->getButtonAtPosition('W'));
		playerInput->bindButton(Player_Menu,			joy, 4);
		playerInput->bindButton(Player_Restart,			joy, 5);
		playerInput->bindButton(Player_Toggle_Debug,	joy, 6);
		playerInput->bindButton(Player_Freeze,			joy, 7);
		playerInput->bindButton(Player_Step,			joy, 8);

		// Vibration
		playerInput->bindVibration(joy);
	}
}

