require("scripts/styles/style.lua");

local function GetTitle()
	if gDialogTable.showResults then
		return "Results";
	else
		return "Goals";
	end
end

local function CreateGoals()
	local goals = gDialogTable.goals;
	local t = {};
	
	local targetScore = "#"..gDialogTable.targetScore..".<font size ='14'>00</font>";
	local targetTime = "#"..gDialogTable.parTimeString;
	local targetStars = "#"..gDialogTable.targetStars;
	
	if gDialogTable.showResults == false then
		
		targetScore = gDialogTable.targetScore;
		
		table.insert(t,
		
			BlgMovieClip
			{
				SetStyle(WindowBGStyle),
				BlgMovieClip 
				{
					enabled = false,
					-- Graphic content
					BlgBitmap{
						image = "common/backgrounds/palm",
						center = {x = 0, y = 0},
						transformMatrix = {x = 646, y = 33},
					}
				},
				
				SetStyle(WindowBGStyle),
				BlgMovieClip 
				{
					enabled = false,
					-- Graphic content
					BlgBitmap{
						image = "common/backgrounds/box_mainmenu",
						center = {x = 0, y = 0},
						transformMatrix = {x = 307, y = 174},
					}
				},
				
				SetStyle(Button1Style),
				BlgMovieClip {
					-- Graphic content
					BlgBitmap{
						image = "common/backgrounds/text_container_goals",
						center = {x = 0, y = 0},
						transformMatrix = {x = 336, y = 232},
					},
					
					BlgText {
						label = "#"..GetString("levelNum", tostring(gDialogTable.level)) .. " " .. GetTitle(),
						transformMatrix = {x = 500, y = 247},
						flags = kHAlignLeft + kVAlignBottom,
						w = 400,
						h = 38,
						font = {
							standardFont,
							32,
							Color(184, 42, 152),
						},
					},             
				},
				
				SetStyle(WindowLogoStyle),
				BlgMovieClip 
				{
					enabled = false,
					-- Graphic content
					BlgBitmap{
						image = "common/backgrounds/logo_01_big",
						center = {x = 0, y = 0},
						transformMatrix = {x = 236, y = 185},
					}
				},
				SetStyle(WindowBGStyle),
				BlgText {
					label = "#Quota:",
					transformMatrix = {x = 467, y = 309},
					flags = kHAlignLeft + kVAlignBottom,
					w = 200,
					h = 38,
					font = {
						standardFont,
						32,
						Color(184, 42, 152),
					},
				},
				
				SetStyle(WindowBGStyle),
				BlgMovieClip 
				{
					enabled = false,
					-- Graphic content
					BlgBitmap{
						image = "common/backgrounds/goal_coin",
						center = {x = 0, y = 0},
						transformMatrix = {x = 557, y = 309},
					}
				},
				
				BlgText {
					label = targetScore,
					transformMatrix = {x = 597, y = 309},
					flags = kHAlignLeft + kVAlignBottom,
					w = 200,
					h = 38,
					font = {
						standardFont,
						32,
						Color(184, 42, 152),
					},
				},
				
				BlgText {
					label = "#Par Time:",
					transformMatrix = {x = 479, y = 361},
					flags = kHAlignLeft + kVAlignBottom,
					w = 200,
					h = 38,
					font = {
						standardFont,
						32,
						Color(184, 42, 152),
					},
				},
				
				SetStyle(WindowBGStyle),
				BlgMovieClip 
				{
					enabled = false,
					-- Graphic content
					BlgBitmap{
						image = "common/backgrounds/goal_clock",
						center = {x = 0, y = 0},
						transformMatrix = {x = 609, y = 361},
					}
				},
				
				BlgText {
					label = targetTime,
					transformMatrix = {x = 649, y = 361},
					flags = kHAlignLeft + kVAlignBottom,
					w = 200,
					h = 38,
					font = {
						standardFont,
						32,
						Color(184, 42, 152),
					},
				},
				
				BlgText {
					label = "#Bonus:",
					transformMatrix = {x = 467, y = 417},
					flags = kHAlignLeft + kVAlignBottom,
					w = 200,
					h = 38,
					font = {
						standardFont,
						32,
						Color(184, 42, 152),
					},
				},
				
				SetStyle(WindowBGStyle),
				BlgMovieClip 
				{
					enabled = false,
					-- Graphic content
					BlgBitmap{
						image = "common/backgrounds/goal_star",
						center = {x = 0, y = 0},
						transformMatrix = {x = 577, y = 417},
					}
				},
				
				BlgText {
					label = targetStars,
					transformMatrix = {x = 617, y = 417},
					flags = kHAlignLeft + kVAlignBottom,
					w = 200,
					h = 38,
					font = {
						standardFont,
						32,
						Color(184, 42, 152),
					},
				},
				
				SetStyle(Button2Style),
				BlgButton{
					transformMatrix = {x = 614, y = 473},
					label = "wnd_okdialog_ok",
					font = {
						standardFont,
						28,
						Color(184, 42, 152),
					},
					upState = {
						bitmap = "common/buttons/btn_default",
					},
					overState = {
						bitmap = "common/buttons/btn_rollover",
					},
					downState = {
						bitmap = "common/buttons/btn_pushed",
						sound = kDefaultButtonSound,
					},

					command = function()
						--PopState();
						OnObjectivesButtonOk();
					end
				},
				
			}
			
		);
		return Group(t);
	end
	
	local customers = "#"..gDialogTable.customers;
	local score = "#"..gDialogTable.money;
	local time = "#"..gDialogTable.timeString;
	local stars = "#"..gDialogTable.stars;
	
	local scoreColor = Color(210, 0, 0);
	local timeColor = Color(210, 0, 0);
	local starsColor = Color(210, 0, 0);
	
	local scoreBonus = "#0";
	local timeBonus = "#No Bonus";
	local starsBonus = "#No Bonus";
	
	local timeCompleted = false;
	local starsCompleted = false;
	
	if gDialogTable.gameWon == true then
		scoreBonus = "#"..gDialogTable.money;
		scoreColor = Color(184, 42, 152);
		
	end
	
	if (gDialogTable.time <= gDialogTable.parTime + 1000) and (gDialogTable.gameWon == true) then
		timeBonus = "#"..gDialogTable.extraMoney;
		timeColor = Color(184, 42, 152);
		timeCompleted = true;
		
	end
	
	if (gDialogTable.stars >= gDialogTable.targetStars) and (gDialogTable.gameWon == true) then
		starsBonus = "#"..gDialogTable.extraMoney;
		starsColor = Color(184, 42, 152);
		starsCompleted = true;
	end
	
	local totalScore = gDialogTable.score;
	
	table.insert(t,
		BlgMovieClip
		{
			SetStyle(WindowBGStyle),
			BlgMovieClip 
			{
				enabled = false,
				-- Graphic content
				BlgBitmap{
					image = "common/backgrounds/palm",
					center = {x = 0, y = 0},
					transformMatrix = {x = 727, y = 18},
				}
			},
			
			SetStyle(WindowBGStyle),
			BlgMovieClip 
			{
				enabled = false,
				-- Graphic content
				BlgBitmap{
					image = "common/backgrounds/box_results",
					center = {x = 0, y = 0},
					transformMatrix = {x = 207, y = 41},
				}
			},
			
			SetStyle(Button1Style),
			BlgMovieClip {
				-- Graphic content
				BlgBitmap{
					image = "common/backgrounds/text_container",
					center = {x = 0, y = 0},
					transformMatrix = {x = 220, y = 118},
				},
				
				BlgText {
					label = "#"..GetString("levelNum", tostring(gDialogTable.level)) .. " " .. GetTitle(),
					transformMatrix = {x = 400, y = 137},
					flags = kHAlignLeft + kVAlignBottom,
					w = 400,
					h = 38,
					font = {
						standardFont,
						32,
						Color(184, 42, 152),
					},
				},            
			},
			
			SetStyle(WindowLogoStyle),
			BlgMovieClip 
			{
				enabled = false,
				-- Graphic content
				BlgBitmap{
					image = "common/backgrounds/logo_01_big",
					center = {x = 0, y = 0},
					transformMatrix = {x = 106, y = 75},
				}
			},
			
			SetStyle(WindowBGStyle),
			-- you earned
			BlgText {
				label = "Customers served",
				transformMatrix = {x = 360, y = 184},
				flags = kHAlignLeft + kVAlignBottom,
				w = 200,
				h = 38,
				font = {
					standardFont,
					28,
					Color(7, 99, 86),
				},
			},
			
			BlgMovieClip 
			{
				enabled = false,
				-- Graphic content
				BlgBitmap{
					image = "common/backgrounds/goal_coin",
					center = {x = 0, y = 0},
					transformMatrix = {x = 580, y = 192, scaleX = 0.75, scaleY = 0.75},
				}
			},
			
			BlgText {
				label = customers,
				transformMatrix = {x = 610, y = 184},
				flags = kHAlignLeft + kVAlignBottom,
				w = 200,
				h = 38,
				font = {
					standardFont,
					28,
					Color(184, 42, 152),
				},
			},
			
			-- your time
			BlgText {
				label = "Time",
				transformMatrix = {x = 360, y = 217},
				flags = kHAlignLeft + kVAlignBottom,
				w = 200,
				h = 38,
				font = {
					standardFont,
					28,
					Color(7, 99, 86),
				},
			},
			
			SetStyle(WindowBGStyle),
			BlgMovieClip 
			{
				enabled = false,
				-- Graphic content
				BlgBitmap{
					image = "common/backgrounds/goal_clock",
					center = {x = 0, y = 0},
					transformMatrix = {x = 580, y = 225, scaleX = 0.75, scaleY = 0.75},
				}
			},
			
			BlgText {
				label = time,
				transformMatrix = {x = 610, y = 217},
				flags = kHAlignLeft + kVAlignBottom,
				w = 200,
				h = 38,
				font = {
					standardFont,
					28,
					timeColor,
				},
			},
			
			
			
			-- your stars
			BlgText {
				label = "Stars",
				transformMatrix = {x = 360, y = 250},
				flags = kHAlignLeft + kVAlignBottom,
				w = 200,
				h = 38,
				font = {
					standardFont,
					28,
					Color(7, 99, 86),
				},
			},
			
			SetStyle(WindowBGStyle),
			BlgMovieClip 
			{
				enabled = false,
				-- Graphic content
				BlgBitmap{
					image = "common/backgrounds/goal_star",
					center = {x = 0, y = 0},
					transformMatrix = {x = 580, y = 258, scaleX = 0.75, scaleY = 0.75},
				}
			},
			
			BlgText {
				label = stars,
				transformMatrix = {x = 610, y = 250},
				flags = kHAlignLeft + kVAlignBottom,
				w = 200,
				h = 38,
				font = {
					standardFont,
					28,
					starsColor,
				},
			},
			
			-- -----------------------------------------------------
			-- Money
			-- -----------------------------------------------------
			
			-- money bonus			
			SetStyle(WindowBGStyle),
			BlgText {
				label = "Bonus_Money",
				transformMatrix = {x = 360, y = 294},
				flags = kHAlignLeft + kVAlignBottom,
				w = 200,
				h = 38,
				font = {
					standardFont,
					28,
					Color(7, 99, 86),
				},
			},
			
			BlgMovieClip 
			{
				enabled = false,
				-- Graphic content
				BlgBitmap{
					image = "common/backgrounds/goal_coin",
					center = {x = 0, y = 0},
					transformMatrix = {x = 580, y = 302, scaleX = 0.75, scaleY = 0.75},
				}
			},
			
			BlgText {
				label = scoreBonus,
				transformMatrix = {x = 610, y = 294},
				flags = kHAlignLeft + kVAlignBottom,
				w = 200,
				h = 38,
				font = {
					standardFont,
					28,
					scoreColor,
				},
			},
			
			-- time bonus			
			SetStyle(WindowBGStyle),
			BlgText {
				label = "Bonus_Time",
				transformMatrix = {x = 360, y = 327},
				flags = kHAlignLeft + kVAlignBottom,
				w = 200,
				h = 38,
				font = {
					standardFont,
					28,
					Color(7, 99, 86),
				},
			},
			
			BlgMovieClip 
			{
				enabled = false,
				-- Graphic content
				BlgBitmap{
					image = "common/backgrounds/goal_coin",
					center = {x = 0, y = 0},
					transformMatrix = {x = 580, y = 335, scaleX = 0.75, scaleY = 0.75},
				}
			},
			
			BlgText {
				label = timeBonus,
				transformMatrix = {x = 610, y = 327},
				flags = kHAlignLeft + kVAlignBottom,
				w = 200,
				h = 38,
				font = {
					standardFont,
					28,
					timeColor,
				},
			},
			
			-- stars bonus
			BlgText {
				label = "Bonus_Stars",
				transformMatrix = {x = 360, y = 360},
				flags = kHAlignLeft + kVAlignBottom,
				w = 200,
				h = 38,
				font = {
					standardFont,
					28,
					Color(7, 99, 86),
				},
			},
						
			SetStyle(WindowBGStyle),
			BlgMovieClip 
			{
				enabled = false,
				-- Graphic content
				BlgBitmap{
					image = "common/backgrounds/goal_coin",
					center = {x = 0, y = 0},
					transformMatrix = {x = 580, y = 368, scaleX = 0.75, scaleY = 0.75},
				}
			},
			
			BlgText {
				label = starsBonus,
				transformMatrix = {x = 610, y = 360},
				flags = kHAlignLeft + kVAlignBottom,
				w = 200,
				h = 38,
				font = {
					standardFont,
					28,
					starsColor,
				},
			},
			
			-- -----------------------------------------------------
			-- Goals
			-- -----------------------------------------------------
			
			-- quota
			BlgText {
				label = "Goal_Money",
				transformMatrix = {x = 420, y = 450},
				flags = kHAlignLeft + kVAlignBottom,
				w = 200,
				h = 38,
				font = {
					standardFont,
					20,
					Color(7, 99, 86),
				},
			},
				
			SetStyle(WindowBGStyle),
			BlgMovieClip 
			{
				enabled = false,
				-- Graphic content
				BlgBitmap{
					image = "common/backgrounds/goal_coin",
					center = {x = 0, y = 0},
					transformMatrix = {x = 539, y = 466, scaleX = 0.5, scaleY = 0.5},
				}
			},
			
			BlgText {
				label = targetScore,
				transformMatrix = {x = 560, y = 450},
				flags = kHAlignLeft + kVAlignBottom,
				w = 200,
				h = 38,
				font = {
					standardFont,
					20,
					Color(7, 99, 86),
				},
			},
			
			-- par time
			BlgText {
				label = "Goal_Time",
				transformMatrix = {x = 420, y = 480},
				flags = kHAlignLeft + kVAlignBottom,
				w = 200,
				h = 38,
				font = {
					standardFont,
					20,
					Color(7, 99, 86),
				},
			},
				
			SetStyle(WindowBGStyle),
			BlgMovieClip 
			{
				enabled = false,
				-- Graphic content
				BlgBitmap{
					image = "common/backgrounds/goal_clock",
					center = {x = 0, y = 0},
					transformMatrix = {x = 539, y = 496, scaleX = 0.5, scaleY = 0.5},
				}
			},
			
			BlgText {
				label = targetTime,
				transformMatrix = {x = 560, y = 480},
				flags = kHAlignLeft + kVAlignBottom,
				w = 200,
				h = 38,
				font = {
					standardFont,
					20,
					Color(7, 99, 86),
				},
			},
			
			-- stars
			BlgText {
				label = "Goal_Stars",
				transformMatrix = {x = 420, y = 510},
				flags = kHAlignLeft + kVAlignBottom,
				w = 200,
				h = 38,
				font = {
					standardFont,
					20,
					Color(7, 99, 86),
				},
			},
				
			SetStyle(WindowBGStyle),
			BlgMovieClip 
			{
				enabled = false,
				-- Graphic content
				BlgBitmap{
					image = "common/backgrounds/goal_star",
					center = {x = 0, y = 0},
					transformMatrix = {x = 539, y = 525, scaleX = 0.5, scaleY = 0.5},
				}
			},
			
			BlgText {
				label = targetStars,
				transformMatrix = {x = 560, y = 510},
				flags = kHAlignLeft + kVAlignBottom,
				w = 200,
				h = 38,
				font = {
					standardFont,
					20,
					Color(7, 99, 86),
				},
			},
			
			-- ---------------------------------------------
			-- Separators
			-- ---------------------------------------------
			SetStyle(WindowBGStyle),
			BlgMovieClip 
			{
				enabled = false,
				-- Graphic content
				BlgBitmap{
					image = "common/backgrounds/results_separator",
					center = {x = 0, y = 0},
					transformMatrix = {x = 345, y = 450},
				},
				BlgBitmap{
					image = "common/backgrounds/results_separator",
					center = {x = 0, y = 0},
					transformMatrix = {x = 345, y = 405},
				},
				BlgBitmap{
					image = "common/backgrounds/results_separator",
					center = {x = 0, y = 0},
					transformMatrix = {x = 345, y = 290},
				}
			},
			
		}
	);
	
	if gDialogTable.gameWon == true then
	
		table.insert(t,
			BlgMovieClip
			{
				-- level score
				SetStyle(WindowBGStyle),
				BlgText {
					label = "You_Earn",
					transformMatrix = {x = 390, y = 405},
					flags = kHAlignLeft + kVAlignBottom,
					w = 200,
					h = 49,
					font = {
						standardFont,
						38,
						Color(184, 42, 152),
					},
				},
				
				BlgMovieClip 
				{
					enabled = false,
					-- Graphic content
					BlgBitmap{
						image = "common/backgrounds/goal_coin_large",
						center = {x = 0, y = 0},
						transformMatrix = {x = 530, y = 409, scaleX = 0.8, scaleY = 0.8},
					}
				},
				
				BlgText {
					label = totalScore,
					transformMatrix = {x = 580, y = 408},
					flags = kHAlignLeft + kVAlignBottom,
					w = 200,
					h = 49,
					font = {
						standardFont,
						44,
						Color(184, 42, 152),
					},
				},
				
				SetStyle(Button2Style),
				BlgButton{
					transformMatrix = {x = 610, y = 564},
					label = "wnd_okdialog_ok",
					font = {
						standardFont,
						28,
						Color(184, 42, 152),
					},
					upState = {
						bitmap = "common/buttons/btn_default",
					},
					overState = {
						bitmap = "common/buttons/btn_rollover",
					},
					downState = {
						bitmap = "common/buttons/btn_pushed",
						sound = kDefaultButtonSound,
					},

					command = function()
						OnObjectivesButtonOk();
					end
				},
			}
		);
		
		if timeCompleted and starsCompleted then
		
			table.insert(t,
				BlgMovieClip
				{
					SetStyle(WindowBGStyle),
					BlgMovieClip 
					{
						enabled = false,
						-- Graphic content
						BlgBitmap{
							image = "common/backgrounds/results_badge_expert",
							center = {x = 0, y = 0},
							transformMatrix = {x = 290, y = 475},
						}
					},
					
					BlgText {
						label = "Expert",
						transformMatrix = {x = 307, y = 495},
						flags = kHAlignLeft + kVAlignBottom,
						w = 200,
						h = 38,
						font = {
							standardFont,
							20,
							Color(125, 21, 20),
						},
					},
				}
			);
		
		end
	
	else
	
		table.insert(t,
			BlgMovieClip{
				-- losing message and retry button
				SetStyle(Button1Style),
				BlgText {
					label = "Better_Luck",
					transformMatrix = {x = 350, y = 410},
					flags = kHAlignLeft + kVAlignBottom,
					w = 400,
					h = 46,
					font = {
						standardFont,
						44,
						Color(184, 42, 152),
					},
				},
				
				SetStyle(Button2Style),
				BlgButton{
					transformMatrix = {x = 490, y = 564},
					label = "Retry",
					font = {
						standardFont,
						28,
						Color(184, 42, 152),
					},
					upState = {
						bitmap = "common/buttons/btn_default",
					},
					overState = {
						bitmap = "common/buttons/btn_rollover",
					},
					downState = {
						bitmap = "common/buttons/btn_pushed",
						sound = kDefaultButtonSound,
					},

					command = function()
						PopState();
						RestartLevel();
					end
				},
				
				SetStyle(Button2Style),
				BlgButton{
					transformMatrix = {x = 610, y = 564},
					label = "Map",
					font = {
						standardFont,
						28,
						Color(184, 42, 152),
					},
					upState = {
						bitmap = "common/buttons/btn_default",
					},
					overState = {
						bitmap = "common/buttons/btn_rollover",
					},
					downState = {
						bitmap = "common/buttons/btn_pushed",
						sound = kDefaultButtonSound,
					},

					command = function()
						OnObjectivesButtonOk();
					end
				},
			}
		);
		
	end
	
	return Group(t);
	
end

MakeState{
	BlgContainer
	{
		transformMatrix = {x = 0, y = 0},
		
		BlgBitmap{
			image = "common/backgrounds/alpha_black",
			name = "mainmenuBG",
			center = {x = 0, y = 0},
		},
		
		CreateGoals();
	}
};
