// Includes
#include "template.h"
#include "Ball.h"
#include "Flipper.h"

app App;

void app::Begin(void)
{
	//Se le asigna el valor de la pantalla que se quiere ver al ejecutar el juego
	gameScreen = gameState::gameScreen;

	//Cambia la escala de las fisicas
	agk::SetPhysicsScale(0.025f);
	agk::SetPhysicsWallBottom(0);

	//Se configura la pantalla de ejecució:
	//Se establecen las medidas que tendra la ventana de ejecución.
	agk::SetWindowSize(600, 800, 0);
	//Establece la resolución que se tendra, para esto se dan como argumentos la altura y anchura del dispositivo
	agk::SetVirtualResolution(agk::GetDeviceWidth(), agk::GetDeviceHeight());
	//Titulo que tendra la ventana de ejecución
	agk::SetWindowTitle("Pinball");
	//Evita que el tamaño de la ventana pueda alterarse
	agk::SetWindowAllowResize(0);

	createTable();

	//Se instancia la pelota
	ball = new Ball();
	ball->launch();

	//Se instancian los flippers
	const unsigned int flipper_image = agk::LoadImage("media/flipper_1.png");
	left_flipper = new Flipper(flipper_image, 205.0f, 680.0f, false);
	right_flipper = new Flipper(flipper_image, 395.0f, 680.0f, true);

	#ifdef _DEBUG
		debug_physics = false;
	#endif // _DEBUG

}

int app::Loop (void)
{
	//Esta sentencia switch se encarga de ver que función ejecutar dependiendo del valor de la variable gameScreen
	switch (gameScreen)
	{
		case gameState::highscoreScreen:
			updateHighscore();
			break;
		case gameState::gameScreen:
			updateGame();
			break;
	}

#ifdef _DEBUG
	if (agk::GetRawKeyPressed(AGK_KEY_P)) 
	{
		debug_physics = !debug_physics;
		if (debug_physics)
		{
			agk::SetPhysicsDebugOn();
		}
		else
		{
			agk::SetPhysicsDebugOff();
		}
	}
#endif


	agk::Sync();

	return 0; // return 1 to close app
}


void app::End (void)
{

}

void app::updateHighscore()
{

}

void app::updateGame()
{
	if (agk::GetRawKeyPressed(AGK_KEY_LEFT))
	{
		left_flipper->activate();
	}
	if (agk::GetRawKeyReleased(AGK_KEY_LEFT))
	{
		left_flipper->desactivate();
	}
	if (agk::GetRawKeyPressed(AGK_KEY_RIGHT))
	{
		right_flipper->activate();
	}
	if (agk::GetRawKeyReleased(AGK_KEY_RIGHT))
	{
		right_flipper->desactivate();
	}

	left_flipper->update();
	right_flipper->update();
}

void app::createTable()
{
	//Se crea el sprite para el fondo del juego
	const unsigned int background = agk::LoadSprite("media/background.png");
	agk::SetSpriteDepth(background, 100);

	//Se crean los sprites para los bordes del tablero
	const unsigned int top_edge = agk::LoadSprite("media/top_edge_1.png");
	const unsigned int left_edge = agk::LoadSprite("media/left_edge_1.png");
	const unsigned int right_edge = agk::LoadSprite("media/right_edge_1.png");
	agk::SetSpritePosition(right_edge, agk::GetVirtualWidth() - agk::GetSpriteWidth(right_edge), 0.0f);

	const unsigned int slope_edge_image = agk::LoadImage("media/slope_edge_1.png");
	const unsigned int left_slope = agk::CreateSprite(slope_edge_image);
	agk::SetSpritePosition(left_slope, 0.0f, agk::GetVirtualHeight() - agk::GetSpriteHeight(left_slope));
	const unsigned int right_slope = agk::CreateSprite(slope_edge_image);
	agk::SetSpriteFlip(right_slope, 1, 0);
	agk::SetSpritePosition(right_slope, agk::GetVirtualWidth() - agk::GetSpriteWidth(right_slope), agk::GetVirtualHeight() - agk::GetSpriteHeight(right_slope));

	agk::SetSpriteDepth(top_edge, 80);
	agk::SetSpriteDepth(left_edge, 80);
	agk::SetSpriteDepth(right_edge, 80);
	agk::SetSpriteDepth(left_slope, 80);
	agk::SetSpriteDepth(right_slope, 80);

	agk::SetSpritePhysicsOn(top_edge, 1);
	agk::SetSpritePhysicsOn(left_edge, 1);
	agk::SetSpritePhysicsOn(right_edge, 1);
	agk::SetSpritePhysicsOn(left_slope, 1);
	agk::SetSpritePhysicsOn(right_slope, 1);

	agk::SetSpriteShape(left_slope, 3);
	agk::SetSpriteShape(right_slope, 3);
}