require( "scripts/style" );
------------------------------------------------------------------------
-- 									Init
------------------------------------------------------------------------
gFromMetastructure = 0;

-- Used for LEVEL elements
local kButtonX = kCenter - 337;
local kButtonY = kMax - 208;
local kButtonXSpace = 86 + 11;
local kButtonYSpace = 84 + 11 + 28/2;
-- Used for STAR elements
local kStarWidth = 30;
local kStarsTotal = gMaxLevels*3;
local kStarsXOffset = 20;
local kStarsAchieved = 0;
local kPathPerc = "elements/starwin";
kPercs = 0;
-- Used for VENUE elements
local kBmpVenueX = 285;
local kBmpVenueY = 70;
local kCurrentVenue = getLastPlayVenue();
--gCurrentVenue;
-- Used for ARROW elements
local kButtonXArrow = 460;
-- Used for PLAY and BACK elements
local kBtnsXOffset = 13;
local kBtnsYOffset = 19;
local kBtnWidth = 212;
local kBtnHeight = 60;
-- Used for MEDAL elements
local kSpaceMedalX = 60;
local kMedalXOffset = 180;
local kMedalYOffset = 110;

if (not kCurrentVenue)
then
	kCurrentVenue = 1;
end


--local kLvlsButtons = createLvlButtons(kCurrentVenue);
------------------------------------------------------------------------
-- 									Styles
------------------------------------------------------------------------
ButtonSelectLvlStyle =
{
	type = kRadio,
	flags = kHAlignCenter + kVAlignCenter,
	font = StandardButtonFont,
	graphics = CheckboxButtonGraphics,
};


------------------------------------------------------------------------
-- 									Functions
------------------------------------------------------------------------
function createMedals(venue)

	t = { SetStyle(DefaultStyle), BeginGroup()	};
	local tPath = "";
	
	local tMedalAch = 0;

	for i = 1, gMaxMedals
	do
		if (i == 1 or i == 3)
		then
			tPath = "buttons/medals/venue"..gCurrentVenue.."/medal";
		else
			tPath = "buttons/medals/medal";
		end
		tMedalAch = isMedalAchieved(venue,i);
		local tmp = "";

		if (tMedalAch)
		then
			tmp = tPath ..i.."active";
		else
			tmp = tPath ..i.."inactive";
		end
		table.insert(t,
			MedalButton
			{
				name = "medal"..i,
				medalNumber = i,
				isAchieved = tMedalAch,
				x = kCenter - kMedalXOffset + kSpaceMedalX*i,
				y = kCenter + kMedalYOffset,
				graphics = 
					{
						tmp,
						tmp,
						tmp,
						tmp
					},
			}
		);
	end
	
	return Group(t);
end

function createVenueBitmaps(maxVenues)
	t = { SetStyle(DefaultStyle), BeginGroup()	};

	for i = 1, maxVenues
	do
		table.insert(t,
			Bitmap
			{
				name = "venue"..i,
				x = kCenter,
				y = kCenter - kBmpVenueY,
				image = "elements/metamap/venue"..i.."active",
			}
		);
	end

	return Group(t);
end

function createStars(aLevel, numStars)
	local t = { SetStyle(DefaultStyle), BeginGroup() };
	local pathStarActive = "";

	if (numStars == -1)
	then
		pathStarActive = "nostarinactive";
	else
		pathStarActive = "nostar";
	end

	table.insert(t,
		Bitmap
		{
			name = "nostarslvl"..aLevel,
			y = kMax - kStarsXOffset,
			image = "elements/metamap/"..pathStarActive,
		}
	);

	local xCoord = 0;
	for i = 1, numStars
	do
		table.insert(t,
			Bitmap
			{
				name = "nostarslvl"..aLevel,
				x = xCoord,
				y = kMax - kStarsXOffset,
				image = "elements/metamap/star",
			}
		);
		xCoord = xCoord + kStarWidth;
	end
	return Group(t);
end

function createLvlButtons(venue)
	local t = { SetStyle(ButtonMetaStyle), BeginGroup()};
	local iAux = 1;
	local newX = 0;
	local newY = 0;

	for i = 1, gMaxLevels
	do
		if (venue)
		then
			iAux  = (venue-1) * gMaxLevels + i;
		end

		local numStars = getNumberOfStars(iAux);
		
		local tGraphics = StandardButtonMetaInactiveGraphics;
		local tColorBtn = DefaultColor;
		local tColorShBtn = DefaultShadowColor;

		if not (venue==2) then
			if (numStars >= 0)
			then
				kStarsAchieved = kStarsAchieved+numStars;
				tGraphics = StandardButtonMetaGraphics;
			else
				tColorBtn = Color(20,20,22,255);
				tColorShBtn = Color(226,211,182,255);
			end
		end

		if (iAux==13) then
			kStarsAchieved = kStarsAchieved+numStars;
			tGraphics = StandardButtonMetaGraphics;
		end

		local iAuxLocal = iAux;
		newX = kButtonX + (kButtonXSpace*i);
		newY = kButtonY;

		if (i > gMaxLevels/2)
		then
			newX = kButtonX + (kButtonXSpace*(i-gMaxLevels/2));
			newY = newY + kButtonYSpace;
		end
		
		table.insert(t,
			Button
			{
				name = ""..iAux,
				x = newX,
				y = newY,
				label = "#",--Added to show completely the stars
				ColorShadowColorText(100,
										100,
										kCenter,
										kCenter + 2,
										70,
										"#"..iAux, 
										tColorBtn, 
										tColorShBtn,
										"#"..iAux,
										kDefaultButtonFontShadowSize,
										kHAlignCenter + kVAlignCenter,
										defaultFont),
				flags = kHAlignCenter + kVAlignCenter,
				type = kRadio,
				createStars(iAux,numStars),
				graphics = tGraphics,
				
				command =
					function()
						gCurrentLevel = iAuxLocal;
					end,
			}
		);
	end

	kPercs = (kStarsAchieved/kStarsTotal*100);
	
	return Group(t);
end

local kLvlsButtons = createLvlButtons(kCurrentVenue);

function changeVenue(currentIndex)
	gNavigatingMetamap = 1;
	SetWindowVisible("venue"..kCurrentVenue,false);

	if (kCurrentVenue == gMaxVenues)
	then
		SetWindowVisible("btnRight",true);
	end
	if (kCurrentVenue == 1)
	then
		SetWindowVisible("btnLeft",true);
	end
	kCurrentVenue = kCurrentVenue + currentIndex;

	if (kCurrentVenue >= gMaxVenues)
	then
		kCurrentVenue = gMaxVenues;
		SetWindowVisible("btnRight",false);
	elseif (kCurrentVenue <= 1)
	then
		SetWindowVisible("btnLeft",false);
		kCurrentVenue = 1;
	end
	SetWindowVisible("venue"..kCurrentVenue,true);
	gCurrentVenue = kCurrentVenue;
	doTransition("goMetamap");
end

local isCurrentVenueStarted = isVenueStarted(kCurrentVenue);
local isNextVenueStarted = isVenueStarted(kCurrentVenue+1);

local pathNextVenue = "";
local pathPrevVenue = "";
local pathCurrentVenue = "";
local colorVenuePrevNext = DefaultColor;
local colorShVenuePrevNext = DefaultShadowColor;

if (kCurrentVenue)
then
	
	pathNextVenue  = "elements/metamap/venue"..(kCurrentVenue+1).."s";
	pathPrevVenue  = "elements/metamap/venue"..(kCurrentVenue-1).."s";
	
	if (not isNextVenueStarted)
	then
		pathNextVenue  = "elements/metamap/venue"..(kCurrentVenue+1).."sinactive";
		colorVenuePrevNext = Color(173,160,154,255);
		colorShVenuePrevNext	= Color(245,239,255,255);
	end

	if (isCurrentVenueStarted)
	then
		pathCurrentVenue = "elements/metamap/venuesframe";
	else
		pathCurrentVenue = "elements/metamap/venuesframeinactive"
		kPathPerc = "elements/starlose";
	end
end

------------------------------------------------------------------------
-- 									Controls
------------------------------------------------------------------------
bmpVenue2sLeft = 
	Bitmap
	{
		x = kCenter - kBmpVenueX,
		y = kCenter - kBmpVenueY,
		name = "bmpVenue2sLeft",
		image = pathPrevVenue,
		ColorShadowColorText(300,
								59,
								kCenter,
								-2,
								18,
								"venue" .. (kCurrentVenue-1), 
								colorVenuePrevNext, 
								colorShVenuePrevNext,
								"txtPrevVenue",
								kDefaultButtonFontShadowSize,
								kHAlignCenter + kVAlignCenter,
								defaultFont),
	};

bmpVenue2sRigth = 
	Bitmap
	{
		x = kCenter + kBmpVenueX,
		y = kCenter - kBmpVenueY,
		name = "bmpVenue2sRigth",
		image = pathNextVenue,
		ColorShadowColorText(300,
								59,
								kCenter,
								-2,
								18,
								"venue" .. (kCurrentVenue+1), 
								colorVenuePrevNext, 
								colorShVenuePrevNext,
								"txtNextVenue",
								kDefaultButtonFontShadowSize,
								kHAlignCenter + kVAlignCenter,
								defaultFont),
	};

bmpVenuesframe = 
	Bitmap
	{
		name = "bmpVenuesframe",
		image = pathCurrentVenue,
		
		-- Creating Medals buttons
		createMedals(kCurrentVenue),
	};

bmpTitle = 
	Bitmap
	{
		x = kCenter,
		name = "bmpTitle",
		image = "elements/metamap/title",
	};

btnPlay = 
	Button
	{
		name = "btnPlay",
		x = kMax - kBtnWidth - kBtnsXOffset,
		y = kMax - kBtnHeight - kBtnsYOffset,
		DefaultButtonLabel("play"),
		type = kPush,
		command =
			function()
				if ( gCurrentLevel == 1  or gCurrentLevel == 13 or
					 gCurrentLevel == 25 or gCurrentLevel == 36    ) then
					 
					gLoadComicLevel = true; 
					doTransition("goComic");
					--doTransition("goGame");
				else
					doTransition("goGame");
				end
			end,
	};

btnBack = 
	Button
	{
		name = "btnBack",
		x = kBtnsXOffset,
		y = kMax - kBtnHeight - kBtnsYOffset,
		DefaultButtonLabel("back"),
		type = kPush,
		command =
			function()
				doTransition("goMainMenu");
			end,
	};

btnLeft = 
	Button
	{
		name = "btnLeft",
		x = kCenter - kButtonXArrow,
		y = kCenter - kBmpVenueY,
		type = kPush,
		graphics = MetamapArrowButtonGraphics,
		hflip = true,
		command =
			function()
				changeVenue(-1);
			end,
	};

btnRight = 
	Button
	{
		name = "btnRight",
		x = kCenter + kButtonXArrow,
		y = kCenter - kBmpVenueY,
		type = kPush,
		graphics = MetamapArrowButtonGraphics,
		command =
			function()
				changeVenue(1);
			end,
	};

txtMetamapTitle = 
	ColorShadowColorText(150,
							59,
							kCenter - 1,
							kCenter - 365,
							18,
							"metamaptitle", 
							Color(51,3,25,255), 
							Color(241,212,48,255),
							"txtMetatamapTitle",
							kDefaultButtonFontShadowSize,
							kHAlignCenter + kVAlignCenter,
							defaultFont);

txtVenueName = 
	ColorShadowColorText(400,
							70,
							kCenter,
							kCenter - 174 - kBmpVenueY,
							34,
							"venue".. kCurrentVenue, 
							Color(142,34,6,255), 
							Color(255,255,57,255),
							"txtVenueName",
							kDefaultButtonFontShadowSize,
							kHAlignCenter + kVAlignCenter,
							defaultFont);
	
bmpPercentageDone = 
	Bitmap
	{
		name = "bmpPercentageDone",
		image = kPathPerc,
		x = kCenter + 172,
		y = kCenter - 264,
		scale = 1.7,
		ColorShadowColorText(180,
								100,
								kCenter,
								kCenter +3,
								46,
								"#".. string.format("%d",kPercs) .. "%",
								Color(85,25,0,255), 
								Color(255,219,90,255),
								"txtPercentageDone",
								kDefaultButtonFontShadowSize,
								kHAlignCenter + kVAlignCenter,
								defaultFont),
		ColorShadowColorText(180,
								100,
								kCenter - 2,
								kCenter + 28,
								20,
								"completed", 
								Color(85,25,0,255), 
								Color(255,219,90,255),
								"txtPercentageTextDone",
								kDefaultButtonFontShadowSize,
								kHAlignCenter + kVAlignCenter,
								defaultFont),
	};

bmpPopupMedal = 
	Bitmap
	{
		x = 0,
		y = 0,
		name = "popmedal",
		image = "backgrounds/popups/metamap/popmedalactive",

		SetStyle(DescMedalsTitleTextStyle),
		Text
		{
			name = "txtMedalTitle",
			label = "#";
			x = kCenter,
			y = 25;
			w = 200,
			h = 59,
			fontsize = 40,
			flags = kHAlignCenter + kVAlignCenter,
		},
		SetStyle(DescMedalsTextStyle),
		Text
		{
			name = "txtMedalDesc",
			label = "#";
			x = kCenter,
			y = kCenter;
			w = 254,
			h = 59,
			--flags = kHAlignCenter + kVAlignCenter,
		}
	};
------------------------------------------------------------------------
-- 									Init
------------------------------------------------------------------------
MakeDialog
{
	name = "mainDialog",
	Bitmap
	{
		name = "dialogmetamap",
		image = "backgrounds/dialogs/dialogmetamap",
		DialogMetaMap
		{
			x = kCenter,
			y = kCenter,
			w = kMax,
			h = kMax,
			
			-- Metamap title
			bmpTitle,

			-- Venue elements
			bmpVenue2sLeft,
			bmpVenue2sRigth,
			bmpVenuesframe,
			createVenueBitmaps(gMaxVenues),
			
			-- Level elements
			SetStyle(ButtonSelectLvlStyle),
			kLvlsButtons,
			--createLvlButtons(kCurrentVenue),
			
			-- Play and Back
			SetStyle( ButtonStyle ),
			btnPlay,
			btnBack,
			
			-- Navigation arrows
			btnLeft,
			btnRight,

			-- Text Info
			txtMetamapTitle,
			txtVenueName,
			
			-- Precentage Done
			bmpPercentageDone,
			
			-- Used for Popup Medal
			bmpPopupMedal,
			
			-- Sound
			createSound(),
		}
	}
}
------------------------------------
-- 				Finalize
------------------------------------
-- Hiding elements according situation
local maxIter = 0;
if (gMaxLevels >= gMaxVenues)
then
	maxIter = gMaxLevels;
else
	maxIter = gMaxVenues;
end
local iAuxiliar = 1;
for i = 1, maxIter
do
	if (i ~= gCurrentVenue)
	then
		SetWindowVisible("venue"..i,false);
	end
end
if (kCurrentVenue == gMaxVenues)
then
		SetWindowVisible("btnRight",false);
elseif (kCurrentVenue == 1)
then
	SetWindowVisible("btnLeft",false);
end

DebugOut("Current Venue: " .. gCurrentVenue);
if (not isNextVenueStarted)
then
	SetWindowVisible("btnRight",false);
else
	if (gCurrentVenue == 5)
	then
		SetWindowVisible("btnRight",false);
	end
end

SetWindowVisible("popmedal",false);