#include "title_frame.h"
#include "types.h"
#include "game_stats.h"


TitleFrame::TitleFrame()
	: GenericFrame("Title")
	, pos(0)
	, ang(0)
	, prevAxis(0)
	, holding(0)
	, t(0)
	, mode(0)
	, toPlay(0)
{
	if (GameStats::finished) mode = 2;
}

void TitleFrame::Init()
{
	Audio::get()->playMusic("music02.ogg");
}

void TitleFrame::DeInit()
{
}

bool TitleFrame::DoUpdate(float time)
{
	t += time;

	toPlay -= time;
	if (toPlay < 0) {
		//Audio::get()->play("fireambience.ogg");
		toPlay = 15;
	}

	setInputBindings();
	ang += 0.1f * time;

	bool confirm = playerInput->isButtonPressed(Player_Action1);
	bool cancel = playerInput->isButtonPressed(Player_Action2);

	if (confirm) {
		Audio::get()->play("Menu_ok.ogg");
	}

	if (mode == 0) {
		float axis = playerInput->getAxis(1);
		if (fabs(axis) > 0.9f) {
			holding++;

			int delta = 0;
			if (holding >= 20 || fabs(prevAxis) < 0.1f) {
				delta = axis > 0 ? 1 : -1;
				prevAxis = axis;
				holding = 0;
			}

			if (delta != 0) {
				Audio::get()->play("Menu_select.ogg");
				pos = (pos + 4 + delta) % 4;
			}
		} else {
			holding = 0;
		}

		if (fabs(axis) < 0.1f && fabs(prevAxis) > 0.9f) {
			prevAxis = 0.0f;
		}

		if (confirm && pos == 0) {
			Switch("story");
			return true;
		}

		if (confirm && pos == 1) {
			mode = 1;
			return true;
		}

		if (confirm && pos == 2) {
			mode = 2;
			return true;
		}

		if (playerInput->isButtonPressed(Player_Menu) || (confirm && pos == 3) || (cancel && pos == 3)) {
			return false;
		}
		
		if (cancel) {
			pos = 3;
		}
	}

	else if (mode == 1 || mode == 3) {
		if (confirm || cancel) {
			mode = 0;
		}
	}

	else if (mode == 2) {
		if (confirm || cancel) {
			mode = 3;
		}
	}


	return true;
}

void TitleFrame::DoRender()
{
	glClearColor(0, 0, 0, 0);
	glClear(GL_COLOR_BUFFER_BIT);

	if (mode == 0) {
		int tex = TextureManager::get("title_screen.jpg")->GetId();
		int texPl = TextureManager::get("planet2.png")->GetId();
		int texStar = TextureManager::get("star_marker.png")->GetId();

		float x = 130;
		float y0 = 334;
		float y1 = 460;
		float y2 = 536;
		float y3 = 606;
		float y = pos == 0 ? y0 : (pos == 1 ? y1 : (pos == 2? y2 : y3));

		// Background
		renderer.drawImage(Vector2f(0, 0), Vector2f(0, 0), tex, 0, 1, 0);
		renderer.drawImage(Vector2f(861, 640-436), Vector2f(0.5f, 0.5f), texPl, ang, 0.3f, 0);
		renderer.drawImage(Vector2f(x, 640-y), Vector2f(0.5f, 0.5f), texStar, ang, 1, 0);

		// Fade
		if (t < 2) {
			BindTexture(0);
			SetBlendType(Texture::ALPHA_BLEND);
			glColor4f(1, 1, 1, 1-(t/2.0f));
			glBegin(GL_QUADS);
			glVertex2f(0, 0);
			glVertex2f(1024, 0);
			glVertex2f(1024, 640);
			glVertex2f(0, 640);
			glEnd();
		}
	}

	else {
		if (mode == 1) {
			int tex = TextureManager::get("controls_screen.jpg")->GetId();
			renderer.drawImage(Vector2f(0, 0), Vector2f(0, 0), tex, 0, 1, 0);
		}

		else if (mode == 2) {
			int tex = TextureManager::get("credits_screen.jpg")->GetId();
			renderer.drawImage(Vector2f(0, 0), Vector2f(0, 0), tex, 0, 1, 0);
		}

		else if (mode == 3) {
			int tex = TextureManager::get("thanks_screen.jpg")->GetId();
			renderer.drawImage(Vector2f(0, 0), Vector2f(0, 0), tex, 0, 1, 0);
		}

		int texPl = TextureManager::get("planet1.png")->GetId();
		renderer.drawImage(Vector2f(936, 540), Vector2f(0.5f, 0.5f), texPl, ang, 0.17f, 0);
	}
}
