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

local pageSize = 5;

local pos = {x = 185, y = 275};
local offset = 120; 

local function GetScenaryButtonFunction(scenary, area, id, name)
	return function ()
		SelectLevel(scenary, area, id, name);
	end
end

local function CreateScenaryButtons(scenaryId, areaId, t --[[, lockLastButton ]])
	local amount = GetNextLevelInScenary(scenaryId);
	local name = scenaryNameMap[scenaryId];
	
	local dayTitle = "";
	local minigameTitle = "";
	local minigameIcon = "";
	
	-- create the title
	if scenaryId == kPoolSide then
	
		dayTitle = GetString("wnd_level_selector_pool");
		minigameTitle = GetString("wnd_level_selector_drums");
		minigameIcon = "common/buttons/level_selector/popups/minigame_pool";
		
	elseif scenaryId == kBeach then
	
		dayTitle = GetString("wnd_level_selector_beach");
		minigameTitle = GetString("wnd_level_selector_sandcastle");
		minigameIcon = "common/buttons/level_selector/popups/minigame_beach";
	
	elseif scenaryId == kNightClub then
	
		dayTitle = GetString("wnd_level_selector_the");
		minigameTitle = GetString("wnd_level_selector_cocktail");
		minigameIcon = "common/buttons/level_selector/popups/minigame_club";
		
	end
	
	table.insert(t,
		BlgText {
			name = "dayTitle",
			transformMatrix = {x = 360, y = 192},
			w = 303, h = 39,
			flags = kVAlignCenter + kHAlignLeft,
			dropShadow = true,
			label = "#"..dayTitle.." "..areaId,
			font = {
				standardFont,
				34,
				Color(184, 42, 152),
			},
		}
	);

	local offsetId = 0;
	local levelsPerArea = 5;
	
	if areaId ~= 1 then
		offsetId = (areaId - 1) * levelsPerArea;
	end
	
	for i = 0, 4 do
		local id = offsetId + i;
		
		if id > amount --[[ or (id == amount and lockLastButton) ]] then
			table.insert(t,
				BlgBitmap{
					transformMatrix = {x = pos.x + (offset * i), y = pos.y, },
					image = "common/buttons/level_selector/popups/btn_locked",
					center = {x = 0, y = 0},
				}
			);
		else		
			if id == amount --[[ and not lockLastButton ]] then
				
				table.insert(t,
					BlgMovieClip
					{
						transformMatrix = {x = pos.x + (offset * i) + 55.0, y = pos.y + 55.0, scaleX = 2.3, scaleY = 2.3},
						repeatCount = 0,
						BlgBitmap{
							image = "common/buttons/halo",
							center = {x = 30, y = 29},
							animation = {
								autoPlay = true,
								keyframes = {
									{
										index = 0,
										alpha = 0.7,
										blendMode = kBlendAdditiveAlpha,
										tweens = {
											{
												type = "SimpleEase",
												target = TWEEN_ALL,
												ease = 0,
											},
										},
									},
									{
										index = 15,
										alpha = 0.5,
										blendMode = kBlendAdditiveAlpha,
										skewX = 180,
										skewY = 180,
										tweens = {
											{
												type = "SimpleEase",
												target = TWEEN_ALL,
												ease = 0,
											},
										},
									},
									{
										index = 30,
										alpha = 0.7,
										blendMode = kBlendAdditiveAlpha,
										skewX = 360,
										skewY = 360,
									},
								},
							},
						},
					}
				);			
			
			end
			
			local state = GetLevelState(scenaryId, id);
			
			table.insert(t,
				BlgButton{
					name = "btn_"..scenaryId.."_"..id,
					transformMatrix = {x = pos.x + (offset * i), y = pos.y, },
					sound = "audio/FX/cp_levelselect_press.ogg",
					upState = {
						bitmap = "common/buttons/level_selector/popups/btn_".. state .."_normal",
					},
					overState = {
						bitmap = "common/buttons/level_selector/popups/btn_".. state .."_over",
					},
					downState = {
						bitmap = "common/buttons/level_selector/popups/btn_".. state .."_down",
					},
					command = GetScenaryButtonFunction(scenaryId, areaId, id + 1, "core/"..name),
				}
			);
			
			if id == amount --[[ and not lockLastButton ]] then
				table.insert(t,
					BlgParticles{
						path = "common/backgrounds/navigator.lua",
						transformMatrix = {x = pos.x + (offset * i) + 17, y = pos.y, },
					}
				);
			end
			
		end
	end
	
	if amount >= offsetId + 5 then
		table.insert(t,
			SetStyle(Button1Style)
		);
		table.insert(t,
			BlgMovieClip
			{
				transformMatrix = {x = 740, y =  505, },
				repeatCount = 0,
				BlgBitmap{
					image = "common/backgrounds/minigame_glow",
					center = {x = 128, y = 128},
					animation = {
						autoPlay = true,
						repeatCount = -1,
						keyframes = {
							{
								index = 0,
								alpha = 0.5,
								scaleX = 0.9,
								scaleY = 0.6,
								color = {
									red = 1,
									green = 0.2,
									blue = 0.1,
								},
								blendMode = kBlendAdditiveAlpha,
								tweens = {
									{
										type = "SimpleEase",
										target = TWEEN_ALL,
										ease = 0,
									},
								},
							},
							{
								index = 45,
								alpha = 1.0,
								scaleX = 1.7,
								scaleY = 1.2,
								color = {
									red = 1,
									green = 0.2,
									blue = 0.1,
								},
								blendMode = kBlendAdditiveAlpha,
								tweens = {
									{
										type = "SimpleEase",
										target = TWEEN_ALL,
										ease = 0,
									},
								},
							},
							{
								index = 90,
								alpha = 0.5,
								scaleX = 0.9,
								scaleY = 0.6,
								color = {
									red = 1,
									green = 0.2,
									blue = 0.1,
								},
								blendMode = kBlendAdditiveAlpha,
							},
						},
					},
				},
			}
		);
		table.insert(t,
			BlgButton{
				name = "btn_mg_"..scenaryId.."_"..areaId,
				transformMatrix = {x = 640, y =  460, },
				label = "",
				upState = {
					bitmap = "common/buttons/level_selector/popups/btn_minigame_normal",
				},
				overState = {
					bitmap = "common/buttons/level_selector/popups/btn_minigame_over",
				},
				downState = {
					bitmap = "common/buttons/level_selector/popups/btn_minigame_down",
					sound = kDefaultButtonSound,
				},
				command = GetScenaryButtonFunction(scenaryId, areaId, areaId, minigameMap[scenaryId]),
			}
		);
		table.insert(t,
			BlgText {
				transformMatrix = {x = 655, y = 470},
				w = 120, h = 70,
				flags = kVAlignCenter + kHAlignCenter,
				dropShadow = true,
				label = minigameTitle,
				font = {
					standardFont,
					32,
					Color(184, 42, 152),
				},
			}
		);
		table.insert(t,
			BlgBitmap{
				transformMatrix = {x = 760, y = 460, },
				image = minigameIcon,
				center = {x = 0, y = 0},
			}
		);
		
	end

	return t;
end

local function CreateButtons()
	local t = {};
	
	--[[
	local nextLevels = {GetNextLevelInScenary(kPoolSide), GetNextLevelInScenary(kBeach), GetNextLevelInScenary(kNightClub)};
	
	local days = {};
	days[1] = math.floor((nextLevels[1]) / 5);
	days[2] = math.floor((nextLevels[2]) / 5);
	days[3] = math.floor((nextLevels[3]) / 5);
	
	DebugOut("d[1]= "..days[1]..", d[2]= "..days[2]..", d[3]= "..days[3])

	local lowestDay = days[1];
	
	if days[2] < lowestDay then
		lowestDay = days[2];
	end
	if days[3] < lowestDay then
		lowestDay = days[3];
	end
	
	local levels = nextLevels[1] + nextLevels[2] + nextLevels[3];
	
	local lockLastButton = false;
	
	if gScenary == kPoolSide then
		lockLastButton = (nextLevels[1] == 5 and nextLevels[2] == 0);
		
		if lowestDay < days[1] then
			lockLastButton = true;
		end
		
	elseif gScenary == kBeach then
		lockLastButton = (nextLevels[2] == 5 and nextLevels[3] == 0);
		
		if lowestDay < days[2] then
			lockLastButton = true;
		end
		
	
	else
		if lowestDay < days[3] then
			lockLastButton = true;
		end	
	end
	]]
	
	--CreateScenaryButtons(gScenary, t, lockLastButton);
	CreateScenaryButtons(gScenary, gArea, t);
	
	return Group(t);
end

MakeDisplayHierarchy{
	autoPlay = true,
	enabled = false,
	
	BlgContainer
	{
		--transformMatrix = {x = -220, y = -100},
		animation = {
			keyframes = {
				{
					index = 0,
					y = -1200,
					tweens = {
						{
							type = "FunctionEase",
							target = TWEEN_ALL,
							functionName = "Back",
							easingMethod = "easeOut",
						},
					},
				},
				{
					index = 5,
					y = -1200,
					tweens = {
						{
							type = "FunctionEase",
							target = TWEEN_ALL,
							functionName = "Back",
							easingMethod = "easeOut",
						},
					},
				},
				{
					index = 30,
					y = 0,
					tweens = {
						{
							type = "FunctionEase",
							target = TWEEN_ALL,
							functionName = "Back",
							easingMethod = "easeIn",
						},
					},
				},
				{
					index = 60,
					y = -800,
				},
			},
		},
		
		BlgBitmap {
			image = "common/buttons/level_selector/popups/ls_bg",
			center = {x = 0, y = 0},
			transformMatrix = {x = 123, y = 141},
		},
		
		BlgBitmap {
			image = "common/buttons/level_selector/popups/ls_caption",
			center = {x = 0, y = 0},
			transformMatrix = {x = 223, y = 167},
		},
		
		BlgBitmap {
			image = "common/buttons/level_selector/popups/deco_seahorse",
			center = {x = 0, y = 0},
			transformMatrix = {x = 165, y = 137},
		},
		
		SetStyle(nil),
		BlgMovieClip {
			name = "buttonContainer",
			CreateButtons();
		},
		
		SetStyle(LevelSelectorButtonStyle),
		BlgButton{
			name = "backBtn",
			transformMatrix = {x = 160, y = 470},
			label = "back",
			
		 	upState =
		 	{
		 		bitmap = "common/buttons/btn_default",
		 	},
		 	overState = {
		 		bitmap = "common/buttons/btn_rollover",
		 	},
			downState = {
		 		bitmap = "common/buttons/btn_pushed",
		 		sound = kDefaultButtonSound,
		 	},
			
			command =
				function()
					CloseScenary();
				end
		},
		
	},
}

