#include <sgg/graphics.h>
#include <sgg/scancodes.h>

//------------ Player declaration
struct Player
{
	float m_target_x = 5.0f;
	struct Game* m_parent;

	float m_pos[2] = {5.0f,3.5f};
	void draw();
	void update(float dt);
	Player(struct Game* parent);
	
};


//------------- Game declaration
struct Game
{
	const float m_canvas_size[2] = { 10.0f, 7.0f };
	Player * m_player = nullptr;
	
	void draw();

	void update(float dt) { m_player->update(dt); }

	Game() { m_player = new Player(this);}
	~Game() { delete m_player; }
};


struct Game mygame; 

void draw()
{
	mygame.draw();
}

void update(float dt)
{
	mygame.update(dt);
}

int main(int argc, char** argv)
{
	graphics::createWindow(800, 600, "Boo");
	graphics::setCanvasScaleMode(graphics::CANVAS_SCALE_FIT);
	graphics::setCanvasSize(10,7);

	graphics::setDrawFunction(draw);
	graphics::setUpdateFunction(update);

	graphics::setFont("FreeSans.ttf");

	graphics::startMessageLoop();

	return 0;
}

// -------- Player methods definition -------------------------------

void Player::draw()
{
	graphics::Brush br;
	graphics::drawRect(m_pos[0], m_pos[1], 0.5f, 1.8f,br);
	graphics::drawText(m_pos[0] - 0.3f, m_pos[1] - 1.0f, 0.25f, "Player", br);
}

void Player::update(float dt)
{
	graphics::MouseState ms;
	graphics::getMouseState(ms);

	// Mouse control: point and click to set the intended position 
	// for the player to move (x axis only)
	if (ms.button_left_pressed)
	{
		m_target_x = graphics::windowToCanvasX(ms.cur_pos_x);
	}
	
	// Now, if not close, move towards that point with a 
	// fixed velocity in each frame, until there.
	if (fabs(m_target_x - m_pos[0]) > 0.05f)
	{
		float dir = (m_target_x - m_pos[0]) / fabs(m_target_x - m_pos[0]);
		m_pos[0] += dt * dir * 0.005f;
	}

	// Also control movement by keyboard.
	// We need to move the "target" point along, to avoid
	// "dragging" the player pack to its old posititon
	if (graphics::getKeyState(graphics::SCANCODE_A))
	{
		m_pos[0] -= dt * 0.005f;
		m_target_x = m_pos[0];
	}
	if (graphics::getKeyState(graphics::SCANCODE_D))
	{
		m_pos[0] += dt * 0.005f;
		m_target_x = m_pos[0];
	}
}

Player::Player(Game* parent)
	: m_parent(parent)
{
	m_pos[0] = m_parent->m_canvas_size[0]/2.0f;
	m_pos[1] = m_parent->m_canvas_size[1]/2.0f;
}

//--------------- Game methods definition --------------------

void Game::draw()
{
	graphics::Brush br;
	br.fill_color[0] = 0.4f;
	br.fill_color[1] = 0.4f;
	br.fill_color[2] = 0.5f;
	br.outline_opacity = 0.0f;
	
	// Draw a gradient on a canvas-filling rectangle to act as the background.
	// Use this approach if you want to have a non-uniform color background
	// because graphics::setWindowBackground() a) only uses the fill color
	// from the Brush and b) also paints the region outside the canvas. 
	br.fill_secondary_color[0] = 0.5f;
	br.fill_secondary_color[1] = 0.2f;
	br.fill_secondary_color[2] = 0.1f;
	br.gradient = true;
	graphics::drawRect(mygame.m_canvas_size[0] * 0.5f, mygame.m_canvas_size[1] * 0.5f,
		mygame.m_canvas_size[0], mygame.m_canvas_size[1], br);

	// Draw a "floor"
	br.gradient = false;
	graphics::drawRect(mygame.m_canvas_size[0] * 0.5f, mygame.m_canvas_size[1] * 0.5f + 1.0f,
		mygame.m_canvas_size[0], 0.2f, br);
	
	// Draw the player
	m_player->draw();
}

