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

#include "LevelButton.h"
#include "../prep/misc/SPrintF.h"
#include "../misc/Config.h"

LevelButton::LevelButton(Rect r, int no)
	: GuiButton(r, "", false), mNo(no)
{
	Resources::load(mSheet, "media/completesheet.png");
	mText = SPrintF::format("%d", no+1);
	mEnabled = Config::get().levelinfo(no).unlocked;
}

void LevelButton::update(double dt)
{
	mEnabled = Config::get().levelinfo(mNo).unlocked;
	GuiButton::update(dt);
}

void LevelButton::render()
{
	GuiButton::render();

	LevelInfo li = Config::get().levelinfo(mNo);
	if (li.timeattack)
		al_draw_bitmap(mSheet->get(2), mRect.topCenter().x - 9, mRect.top() - 16, 0);
	if (li.complete100)
		al_draw_bitmap(mSheet->get(1), mRect.topCenter().x - 9, mRect.top() - 16, 0);
	if ((!li.timeattack) && (!li.complete100) && li.complete)
		al_draw_bitmap(mSheet->get(0), mRect.topCenter().x - 9, mRect.top() - 16, 0);

	if (mState == HIGHLIGHTED)
	{
		al_draw_textf(mFont, 400.0, 315.0, ALLEGRO_ALIGN_CENTRE, "Level %d", mNo+1);
		if (li.complete)
		{
			al_set_blender(ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, al_map_rgb(255,255,0));
			al_draw_textf(mFont, 400.0, 350.0, ALLEGRO_ALIGN_CENTRE, "Completed");
			al_set_blender(ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, al_map_rgb(255,255,255));
			if (li.complete100)
			{
				al_set_blender(ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, al_map_rgb(255,255,0));
				al_draw_textf(mFont, 400.0, 370.0, ALLEGRO_ALIGN_CENTRE, "100%% completion achieved");
				al_set_blender(ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, al_map_rgb(255,255,255));
			}
			else
				al_draw_textf(mFont, 400.0, 370.0, ALLEGRO_ALIGN_CENTRE, "100%% completion not achieved");

			if (li.bestTime < 999.00)
			{
				if (li.timeattack)
				{
					al_set_blender(ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, al_map_rgb(255,255,0));
					al_draw_textf(mFont, 400.0, 390.0, ALLEGRO_ALIGN_CENTRE, "Best time: %.2fs", li.bestTime);
					al_set_blender(ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, al_map_rgb(255,255,255));
				}
				else
					al_draw_textf(mFont, 400.0, 390.0, ALLEGRO_ALIGN_CENTRE, "Best time: %.2fs", li.bestTime);
			}
			else
				al_draw_textf(mFont, 400.0, 390.0, ALLEGRO_ALIGN_CENTRE, "Best time: ---");

			al_draw_textf(mFont, 400.0, 410.0, ALLEGRO_ALIGN_CENTRE, "Target time: %.2fs", Config::get().timeAttackTime(mNo));
		}
		else
			al_draw_textf(mFont, 400.0, 350.0, ALLEGRO_ALIGN_CENTRE, "Not completed");
	}

}
