/*
 * TitleState.cpp
 *
 *  Created on: 2009-07-11
 *      Author: wasyl
 */

#include <cstdio>
#include "TitleState.h"

#include "../prep/a5dumb.h"
#include "../prep/Common.h"
#include "../prep/ent/World.h"
#include "../prep/misc/SPrintF.h"
#include "../gui/LevelButton.h"
#include "../gui/GuiText.h"
#include "../misc/Controls.h"
#include "../misc/Config.h"
#include "../misc/Theme.h"
#include "../prep/res/Resources.h"

TitleState::TitleState()
	: AppState<int>(al_get_current_display())
{
	Resources::load(mLogo, "media/logo.png");

	mBackground = new PlayState(-1, "levels/title.lv");
	mMusic = true;

	mWorld.add(new GuiText(Vec(150.0, 295.0), "Play a level:"));

	int base = 55;
	for (int i = 0; i < Config::get().numLevels(); i++)
	{
		int x = base + (i%5)*40;
		int y = 330.0 + (i/5)*50;
		Rect r(x,y,x+30.0,y+30.0);
		levelButtons.push_back(new LevelButton(r, i));
		mWorld.add(levelButtons.back());
	}

	mWorld.add(new GuiText(Vec(650.0, 295.0), "Select a theme:"));
	themeButton = new GuiButton(Rect(550.0, 330.0, 750.0, 360.0), "Sky", true);
	mWorld.add(themeButton);
	mWorld.add(new GuiText(Vec(650.0, 375.0), "Like the music?"));
	musicButton = new GuiButton(Rect(550.0, 400.0, 750.0, 430.0), "Yes", true);
	mWorld.add(musicButton);

	int x = base + (14%5)*40;
	int y = 330.0 + (14/5)*50;
	Rect r(x,y,x+30.0,y+30.0);
	winnerButton = new GuiButton(r, "??", true);
	if (Config::get().completions() == Config::get().numLevels())
		mWorld.add(winnerButton);

	quitButton =  new GuiButton(Rect(300.0, 530.0, 500.0, 560.0), "Quit", true);
	mWorld.add(quitButton);

	mThemeNo = 0;

	mPointsText = new GuiText(Vec(400.0, 582.0), "");
	mWorld.add(mPointsText);
}

TitleState::~TitleState()
{
	delete mBackground;
}

void TitleState::update(double dt)
{
	mBackground->update(dt);

	Entities &dynamics = mWorld.index(ID("dyna"));
	FOREACH(vector,Entity*,e,dynamics)
		(*e)->update(dt);
	mWorld.reindex();

	mPointsText->setText(SPrintF::format("Unlock points: %d/%d", Config::get().unlockPoints(), Config::get().numLevels() * 3));

	for (unsigned int i = 0; i < levelButtons.size(); i++)
		if (levelButtons[i]->clicked())
		{
			int toPlay = i;
			int result = 1;
			while(result)
			{
				if (result == 2)
					toPlay++;
				if (toPlay >= Config::get().numLevels())
					break;

				PlayState ps(toPlay, SPrintF::format("levels/%d.lv", toPlay+1).c_str());
				result = spawn(&ps);
			}

			if (Config::get().completions() == Config::get().numLevels())
			{
				if (!winnerButton->world())
					mWorld.add(winnerButton);
			}

			Tile::resetPresent();
			return;
		}

	if (winnerButton->clicked())
	{
		PlayState ps(-2, SPrintF::format("levels/winner.lv").c_str());
		spawn(&ps);

		Tile::resetPresent();
		return;
	}

	if (themeButton->clicked())
	{
		mThemeNo = (mThemeNo + 1) % 6;
		int unlock = Config::get().unlockPoints();
		if (Theme::get(mThemeNo).unlockPoints() <= unlock)
		{
			Theme::set(mThemeNo);
			themeButton->setText(Theme::active().name());
		}
		else
		{
			themeButton->setText(
				SPrintF::format("Locked (%d more)", Theme::get(mThemeNo).unlockPoints() - unlock));
		}
	}

	if (musicButton->clicked())
	{
		mMusic = !mMusic;
		if (mMusic)
			al_duh_set_volume(al_play_module("media/dreamland.xm", 0, true), 0.25f);
		else
			al_stop_module();
		musicButton->setText(mMusic ? "Yes" : "No");
	}

	if (Controls::get().quit->on() || quitButton->clicked())
		finish(0);
}

void TitleState::render(ALLEGRO_DISPLAY *disp)
{
	mBackground->render(disp);
	int x = 395.0 - al_get_bitmap_width(mLogo) * 0.5;
	al_draw_bitmap(mLogo, x, 40.0, 0);

	Entities &renders = mWorld.index(ID("rndr"));
	FOREACH(vector,Entity*,e,renders)
		(*e)->render();
}
