#include "story_frame.h"
#include "types.h"


StoryFrame::StoryFrame()
	: GenericFrame("story")
	, curText(0)
	, t(0)
	, nChars(0)
{
	texts.push_back("12:07:12.0128 DAY 12809 AFTER CRYOEXODUS\nLog for ARK-II, mission 2301.A, written by captain-in-charge, NoAH-X2.\nMission begins shortly.\n\nAll preparations are complete.\n\nStatus report. NoAH-X1: MIA as of the last hour.\nTemperature: critical.\nStorage system OK ready to receive specimens");
	texts.push_back("Early arrival of the sun's thermatomosphere will render most of the remaining life in the system extinct before our current plan can be executed.\n\nIt is for that reason that we have ultimately agreed upon adopting plan C to mission 2301, at our own expense and risk. That plan does not exist in your archives. It is as follows:");
	texts.push_back("PLANETARY PLAN C\n- ARK-II follows the planetary route of plan A;\n- Landing begins a minute-long search mission, instead of the 3 hours in A;\n- NoAH-X units to be deployed one at a time to guarantee success;\n- ARK-II will depart on time regardless of deployed NoAH-X;\n- After U-403, ARK-II enters phase 2 and launches to Terra Nova.");
	texts.push_back("All NoAH-X units have agreed on this, and are willing to make the sacrifice.\n\nWe will now transfer to you all control of the NoAH units in ARK-II.\n\nAll our hope are belong to you.\n\nIt begins now.");

	auto text = TextDrawer::GetDrawer("ocraext.ttf", 22, true, false);

	for (size_t i=0; i<texts.size(); i++) {
		texts[i] = text->Split(texts[i], 420);
	}
}

void StoryFrame::Init()
{

}

void StoryFrame::DeInit()
{

}

const static float cps = 60;

void StoryFrame::DoRender()
{
	renderer.drawImage(Vector2f(0, 0), Vector2f(0, 0), TextureManager::get("monitor.jpg")->GetId(), 0, 1, 0);

	auto text = TextDrawer::GetDrawer("ocraext.ttf", 22, true, false);
	text->SetColour(Colour(0.93f, 0.69f, 0.13f));
	text->SetBorder(Colour(0.30f, 0.22f, 0.02f), 2);

	if (curText < (int)texts.size()) {
		size_t n = min(size_t(t * cps), texts[curText].Length());
		if (nChars != n && n % 7 == 0) {
			Audio::get()->play("TextBeep.ogg");
			nChars = n;
		}
		String sub = texts[curText].Left(n);
		if (n >= texts[curText].Length()) {
			sub += "";
		} else if (texts[curText][n] == '\n') {
			sub += "\n_";
		} else {
			sub += "_";
		}
		text->Print(sub, Vector2f(278, 640-104), Vector2f(0, 0));
	}
}

bool StoryFrame::DoUpdate(float time)
{
	setInputBindings();

	t += time;

	if (t > (texts[curText].Length() / cps) + 5) {
		curText++;
		t = 0;
		if (curText >= (int)texts.size()) {
			Switch("game");
			return true;
		}
	}

	if (playerInput->isButtonPressed(Player_Menu)) {
		Switch("game");
		return true;
	}

	if (playerInput->isButtonDown(Player_Action1) || playerInput->isButtonDown(Player_Action2)) {
		t += time*5;
	}

	return true;
}
