#include "sdl.h"
#include "interface.h"
#include "input.h"

int Input(APP *app, char *run, FONTS *fonts, char flag) {
	SDL_Event event;

	while (SDL_PollEvent(&event)) {
		switch (event.type) {
			case SDL_KEYDOWN:
				if (event.key.keysym.sym == SDLK_q) {
					*run = 0;
					return EXIT;
				}

				else if (event.key.keysym.sym == SDLK_k)
					return SAVE;

				else if (flag == IN_MENU) {
					if (event.key.keysym.sym == SDLK_s)
						return SETTINGS;

					else if (event.key.keysym.sym == SDLK_c)
						return CONTINUE;

					else if (event.key.keysym.sym == SDLK_n)
						return NEW_GAME;
				}

				else if (flag == IN_SETTINGS || flag == IN_GAME) {
					if (event.key.keysym.sym == SDLK_DOWN)
						return KEY_DOWN;

					else if (event.key.keysym.sym == SDLK_UP)
						return KEY_UP;

					else if (event.key.keysym.sym == SDLK_RIGHT)
						return KEY_RIGHT;

					else if (event.key.keysym.sym == SDLK_LEFT)
						return KEY_LEFT;
				}

				break;

			case SDL_QUIT:
				if (flag == IN_MENU)
					*run = 0;
				break;

			case SDL_WINDOWEVENT:
				if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
					SDL_GetWindowSize(app->window, &app->scr_width, &app->scr_height);
					SDL_UpdateWindowSurface(app->window);
				}

				CloseFonts(fonts);
				InitFonts(fonts, *app);
				break;

			default:
				break;
		}
	}

	return INPUT_NORMAL;
}
