/*
 * Copyright (c) 2019-2020, yzrh <yzrh@noema.org>
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <SDL2/SDL.h>

#include "input.h"

void
input_keyboard(SDL_Keycode sym, Snake_Input *code)
{
	switch (sym) {
		case SDLK_LEFT:
			*code = SNAKE_LEFT;
			break;
		case SDLK_UP:
			*code = SNAKE_UP;
			break;
		case SDLK_DOWN:
			*code = SNAKE_DOWN;
			break;
		case SDLK_RIGHT:
			*code = SNAKE_RIGHT;
			break;
		case SDLK_SPACE:
			*code = SNAKE_JUMP;
			break;
		case SDLK_RETURN:
			*code = SNAKE_SELECT;
			break;
		case SDLK_ESCAPE:
			*code = SNAKE_PAUSE;
			break;
		case SDLK_r:
			*code = SNAKE_RESET;
			break;
		case SDLK_g:
			*code = SNAKE_GOD;
			break;
		case SDLK_h:
			*code = SNAKE_GROW;
			break;
		case SDLK_t:
			*code = SNAKE_TELEPORT;
			break;
		case SDLK_m:
			*code = SNAKE_MUSIC;
			break;
		case SDLK_COMMA:
			*code = SNAKE_TICK_DECREASE;
			break;
		case SDLK_PERIOD:
			*code = SNAKE_TICK_INCREASE;
			break;
		case SDLK_s:
			*code = SNAKE_LEFT;
			break;
		case SDLK_e:
			*code = SNAKE_UP;
			break;
		case SDLK_d:
			*code = SNAKE_DOWN;
			break;
		case SDLK_f:
			*code = SNAKE_RIGHT;
			break;
		default:
			*code = SNAKE_EMPTY;
			break;
	}
}

void
input_controller(uint8_t button, Snake_Input *code)
{
	switch (button) {
		case SDL_CONTROLLER_BUTTON_DPAD_LEFT:
			*code = SNAKE_LEFT;
			break;
		case SDL_CONTROLLER_BUTTON_DPAD_UP:
			*code = SNAKE_UP;
			break;
		case SDL_CONTROLLER_BUTTON_DPAD_DOWN:
			*code = SNAKE_DOWN;
			break;
		case SDL_CONTROLLER_BUTTON_DPAD_RIGHT:
			*code = SNAKE_RIGHT;
			break;
		case SDL_CONTROLLER_BUTTON_START:
			*code = SNAKE_SELECT;
			break;
		case SDL_CONTROLLER_BUTTON_BACK:
			*code = SNAKE_PAUSE;
			break;
		case SDL_CONTROLLER_BUTTON_GUIDE:
			*code = SNAKE_GOD;
			break;
		case SDL_CONTROLLER_BUTTON_A:
			*code = SNAKE_JUMP;
			break;
		case SDL_CONTROLLER_BUTTON_B:
			break;
		case SDL_CONTROLLER_BUTTON_X:
			*code = SNAKE_RESET;
			break;
		case SDL_CONTROLLER_BUTTON_Y:
			*code = SNAKE_MUSIC;
			break;
		case SDL_CONTROLLER_BUTTON_LEFTSTICK:
			*code = SNAKE_TELEPORT;
			break;
		case SDL_CONTROLLER_BUTTON_RIGHTSTICK:
			*code = SNAKE_GROW;
			break;
		case SDL_CONTROLLER_BUTTON_LEFTSHOULDER:
			*code = SNAKE_TICK_DECREASE;
			break;
		case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER:
			*code = SNAKE_TICK_INCREASE;
			break;
		default:
			*code = SNAKE_EMPTY;
			break;
	}
}

#ifdef GPIO

void
input_gpio(int gpio, uint8_t *button)
{
	switch (gpio) {
		case 0:
			*button = SDL_CONTROLLER_BUTTON_DPAD_LEFT;
			break;
		case 1:
			*button = SDL_CONTROLLER_BUTTON_DPAD_UP;
			break;
		case 2:
			*button = SDL_CONTROLLER_BUTTON_DPAD_DOWN;
			break;
		case 3:
			*button = SDL_CONTROLLER_BUTTON_DPAD_RIGHT;
			break;
		case 4:
			*button = SDL_CONTROLLER_BUTTON_BACK;
			break;
		case 5:
			*button = SDL_CONTROLLER_BUTTON_GUIDE;
			break;
		case 6:
			*button = SDL_CONTROLLER_BUTTON_START;
			break;
		case 7:
			*button = SDL_CONTROLLER_BUTTON_A;
			break;
		case 8:
			*button = SDL_CONTROLLER_BUTTON_B;
			break;
		case 9:
			*button = SDL_CONTROLLER_BUTTON_X;
			break;
		case 10:
			*button = SDL_CONTROLLER_BUTTON_Y;
			break;
		case 11:
			*button = SDL_CONTROLLER_BUTTON_LEFTSHOULDER;
			break;
		case 12:
			*button = SDL_CONTROLLER_BUTTON_RIGHTSHOULDER;
			break;
		case 13:
			*button = SDL_CONTROLLER_BUTTON_LEFTSTICK;
			break;
		case 14:
			*button = SDL_CONTROLLER_BUTTON_RIGHTSTICK;
			break;
		default:
			*button = SDL_CONTROLLER_BUTTON_INVALID;
			break;
	}
}

#endif /* GPIO */
