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

local navigatorWidth = 10;

scenaryNameMap = {
	[kNightClub] = "club",
	[kPoolSide] = "pool",
	[kBeach] = "beach",
};

minigameMap = {
	[kNightClub] = "sandwich",
	[kPoolSide] = "ravingballs",
	[kBeach] = "sandcastle",
};

local buttonPos = {
	[kNightClub] = {
		{x = 342, y = 317},
		{x = 461, y = 213},
		{x = 238, y = 155},
		{x = 91, y = 259},
		{x = 91, y = 280},
	},
	[kPoolSide] = {
		{x = 895, y = 286},
		{x = 678, y = 176},
		{x = 830, y = 138},
		{x = 1084, y = 141},
		{x = 862, y = 304},
	},
	[kBeach] = {
		{x = 495, y = 515},
		{x = 274, y = 533},
		{x = 422, y = 737},
		{x = 527, y = 854},
		{x = 663, y = 535},
	},
};

local buttonOrder = {
	[kNightClub] = {
		5,
		4,
		3,
		2,
		1,
	},
	[kPoolSide] = {
		5,
		2,
		1,
		3,
		4,
	},
	[kBeach] = {
		3,
		2,
		5,
		4,
		1,
	},
};

local particlesPos = {
	[kNightClub] = {
		{x = 100, y = 102},
		{x = 101, y = 105},
		{x = 110, y = 65},
		{x = 68, y = 29},
		{x = 223, y = 86},
	},
	[kPoolSide] = {
		{x = 46, y = 38},
		{x = 140, y = 55},
		{x = 128, y = 44},
		{x = 102, y = 87},
		{x = 230, y = 105},
	},
	[kBeach] = {
		{x = 142, y = 100},
		{x = 169, y = 106},
		{x = 108, y = 68},
		{x = 268, y = 55},
		{x = 189, y = 135},
	},
};

local areaDependency = {
	[kNightClub] = {
		[1] = {
			{scenaryId = kBeach, areaId = 1, }
		},
	},
	[kPoolSide] = {
		[2] = {
			{scenaryId = kNightClub, areaId = 1, }
		},
	},
	[kBeach] = {
		[1] = {
			{scenaryId = kPoolSide, areaId = 1, }
		},
		[2] = {
			{scenaryId = kNightClub, areaId = 1, }
		},
	},
};

local function CreateEndingButton(t)	
	table.insert(t, 
		BlgButton{
			name = "endingButton",
			transformMatrix = {x = 1100, y = 880},
			--labelOffset = {x = -40, y = 5},
			label = "#End",
			upState = {
				bitmap = "common/buttons/btn_ending_default",
			},
			overState = {
				bitmap = "common/buttons/btn_ending_rollover",
			},
			downState = {
				bitmap = "common/buttons/btn_ending_pushed",
			},
			command = function()
				PushState({"scripts/states/stateendcomic.lua", basePath="level/story/ending/", background="level/story/textures/bg_pool"});
			end
		}
	);
end

local function GetScenaryButtonFunction(scenaryId, areaId)
	return function ()
		OnAreaClicked(scenaryId, areaId);
	end;
end

local function IsDependencyFinished(scenaryId, areaId)
	local dependencies = areaDependency[scenaryId][areaId];
	
	if dependencies ~= nil then
		for i, dependency in ipairs(dependencies) do
			if not IsAreaFinished(dependency.scenaryId, dependency.areaId - 1) then
				return false;
			end
		end
	end
	
	return true;
end

local function CreateAreaButtons(scenaryId, t, rebuildPoints)
	local nextArea = math.floor(GetNextLevelInScenary(scenaryId) / 5) + 1;
	
	local areaButtons = {};
	
	for areaId, order in ipairs(buttonOrder[scenaryId]) do
		areaButtons[order] = areaId;
	end
	
	for idx, areaId in ipairs(areaButtons) do
		local name = scenaryNameMap[scenaryId];	
		local particlePos = particlesPos[scenaryId][areaId];
		local pos = buttonPos[scenaryId][areaId];
		
		local bLocked = true;
		local styleId = GetAreaStyle(scenaryId, areaId - 1);

		if areaId <= nextArea then
		
			if styleId == 0 then
			
				if IsDependencyFinished(scenaryId, areaId) and rebuildPoints > 0 then
				
					bLocked = false;
				
				end
				
			else
			
				bLocked = false;
			
			end
		
		end
		
		
		if bLocked == true then
		
			table.insert(t,
				BlgBitmap{
					name = "img_"..scenaryId.."_"..areaId,
					transformMatrix = pos,
					image = "common/buttons/level_selector/".. name .."/btn"..areaId.."/locked",
					center = {x = 0, y = 0},
					--tint = Color(100, 100, 100),
				}
			);
			
		else
		
			table.insert(t,
				BlgButton{
					name = "btn_"..scenaryId.."_"..areaId,
					transformMatrix = pos,
					sound = kDefaultButtonSound,
					upState = {
						bitmap = "common/buttons/level_selector/".. name .."/btn"..areaId.."/style"..styleId,
					},
					overState = {
						bitmap = "common/buttons/level_selector/".. name .."/btn"..areaId.."/style"..styleId.."_over",
					},
					downState = {
						bitmap = "common/buttons/level_selector/".. name .."/btn"..areaId.."/style"..styleId,
					},
					command = GetScenaryButtonFunction(scenaryId, areaId - 1, styleId),
					animation = anim,
					alpha = 1,
				}
			);
			
			if areaId == nextArea then
				table.insert(t,
					BlgParticles{
						name = "prt_"..scenaryId.."_"..areaId,
						transformMatrix = {x = pos.x + particlePos.x, y = pos.y + particlePos.y},
						path = "common/particles/foundstars.lua",
					}
				);
			end
		
		end
		
	end
end

function CreateMapButtons()
	local t = {};
	
	local nextLevels = {GetNextLevelInScenary(kPoolSide), GetNextLevelInScenary(kBeach), GetNextLevelInScenary(kNightClub)};
	local levels = nextLevels[1] + nextLevels[2] + nextLevels[3];
	local rebuildPoints = GetRebuildPoints();
	
	CreateAreaButtons(kPoolSide, t, rebuildPoints);
	
	CreateAreaButtons(kBeach, t, rebuildPoints);
	
	CreateAreaButtons(kNightClub, t, rebuildPoints);
	
	if levels == 75 then
		CreateEndingButton(t);
	end
	
	return Group(t);
end

MakeState{
	type = "StateLevelSelector",

	selectedParticles ="common/particles/pfx_core_minigame_organize.lua",

	BlgContainer{
		transformMatrix = {scaleX = 0.75, scaleY = 0.75},
		name = "backgroundContainer",
		BlgContainer{
			BlgBitmap{
				image = "common/backgrounds/level_selector/bg_levelselector_1",
				name = "levelselectorBG1",
				center = {x = 0, y = 0},
			},

			BlgBitmap{
				image = "common/backgrounds/level_selector/bg_levelselector_2",
				name = "levelselectorBG2",
				transformMatrix = {x = 1024},
				center = {x = 0, y = 0},
			},
		},
	},
	
	-- Clouds
	BlgContainer{
		transformMatrix = {scaleX = 0.75, scaleY = 0.75},
		name = "cloudContainer",
		
		-- Waves
		BlgBitmap {
			playAnim = "Waves01",
			blendMode = kBlendAdditiveAlpha,
			alpha = 0.2,
			image="common/backgrounds/level_selector/waves",
			center = {x = 0, y = 0},
			transformMatrix = {x = 430, y = 130, scaleX = 3.5, scaleY = 2.0, rotation = 155},
		},	
		
		BlgBitmap {
			playAnim = "Waves03",
			blendMode = kBlendAdditiveAlpha,
			alpha = 0.2,
			image="common/backgrounds/level_selector/waves",
			center = {x = 0, y = 0},
			transformMatrix = {x = 170, y = 810, scaleX = 3.5, scaleY = 1.8, rotation = 15},
		},
		
		BlgBitmap {
			playAnim = "Waves02",
			blendMode = kBlendAdditiveAlpha,
			alpha = 0.2,
			image="common/backgrounds/level_selector/waves",
			center = {x = 0, y = 0},
			transformMatrix = {x = 100, y = 450, scaleX = 3.5, scaleY = 2.5, rotation = 50},
		},
		
		BlgBitmap {
			playAnim = "Waves01",
			blendMode = kBlendAdditiveAlpha,
			alpha = 0.2,
			image="common/backgrounds/level_selector/waves",
			center = {x = 0, y = 0},
			transformMatrix = {x = 870, y = 1000, scaleX = 3.5, scaleY = 2.5, rotation = -35},
		},
		
		BlgBitmap {
			playAnim = "Waves03",
			blendMode = kBlendAdditiveAlpha,
			alpha = 0.2,
			image="common/backgrounds/level_selector/waves",
			center = {x = 0, y = 0},
			transformMatrix = {x = 1050, y = 680, scaleX = 3.5, scaleY = 2.5, rotation = -40},
		},
		
		BlgBitmap {
			playAnim = "Waves02",
			blendMode = kBlendAdditiveAlpha,
			alpha = 0.2,
			image="common/backgrounds/level_selector/waves",
			center = {x = 0, y = 0},
			transformMatrix = {x = 1440, y = 250, scaleX = 3.0, scaleY = 2.2, rotation = -170},
		},
		
		BlgBitmap{
			image = "common/backgrounds/level_selector/bg_island_1",
			center = {x = 0, y = 0},
		},

		BlgBitmap{
			image = "common/backgrounds/level_selector/bg_island_2",
			transformMatrix = {x = 1024},
			center = {x = 0, y = 0},
		},
		
		BlgBitmap{
			image = "common/backgrounds/level_selector/club_fence",
			transformMatrix = {x = 0, y = 71},
			center = {x = 0, y = 0},
		},
		
		BlgBitmap{
			image = "common/backgrounds/level_selector/road_beach",
			transformMatrix = {x = 284, y = 514},
			center = {x = 0, y = 0},
		},
		
		BlgBitmap{
			image = "common/backgrounds/level_selector/fence_beach",
			transformMatrix = {x = 205, y = 479},
			center = {x = 0, y = 0},
		},
		
		BlgContainer
		{
			name = "buttonContainer",
			CreateMapButtons(),
			
		},
		
		-- Reefs
		BlgBitmap{
			image = "common/backgrounds/level_selector/reef1",
			transformMatrix = {x = 1198, y = 688},
			center = {x = 0, y = 0},
			playAnim = "DoAnim",
			alpha = 0.8,
		},
		
		BlgBitmap{
			image = "common/backgrounds/level_selector/reef2",
			transformMatrix = {x = 40, y = 651},
			center = {x = 0, y = 0},
			playAnim = "DoAnim",
			alpha = 0.8,
		},
		
		BlgBitmap{
			image = "common/backgrounds/level_selector/reef3",
			transformMatrix = {x = 1161, y = 40},
			center = {x = 0, y = 0},
			playAnim = "DoAnim",
			alpha = 0.8,
		},
		
		-- pool decos
		BlgBitmap{
			image = "common/backgrounds/level_selector/front_fence",
			transformMatrix = {x = 690, y = 285},
			center = {x = 0, y = 0},
		},
		
		BlgBitmap{
			image = "common/backgrounds/level_selector/logo_pool",
			transformMatrix = {x = 892, y = 300},
			center = {x = 0, y = 0},
		},
		
		BlgBitmap {
			playAnim = "Waves02",
			blendMode = kBlendAdditiveAlpha,
			alpha = 0.2,
			image="common/backgrounds/level_selector/waves",
			center = {x = 0, y = 0},
			transformMatrix = {x = 900, y = 240, scaleX = 2.5, scaleY = 2.0},
		},
		
		BlgBitmap {
			playAnim = "Waves03",
			blendMode = kBlendAdditiveAlpha,
			alpha = 0.2,
			image="common/backgrounds/level_selector/waves",
			center = {x = 0, y = 0},
			transformMatrix = {x = 900, y = 240, scaleX = 2.5, scaleY = -2.0},
		},		
		
		-- additional decos
		BlgBitmap{
			image = "common/backgrounds/level_selector/fountain",
			transformMatrix = {x = 672, y = 360},
			center = {x = 0, y = 0},
			playAnim = "DoAnim",
		},
		
		BlgBitmap{
			image = "common/backgrounds/level_selector/waterfall",
			transformMatrix = {x = 618, y = 64},
			center = {x = 0, y = 0},
			playAnim = "DoAnim",
		},
		
		-- particles
		BlgParticles{
			path = "common/particles/level_selector_mist_waterfall.lua",
			playTime = -1,
			transformMatrix = {x = 690, y = 150},
			bindGenerator = true,
			useGlobalRenderOrigin = true,
		},
		
		BlgParticles{
			path = "common/particles/level_selector_mist_waterfall.lua",
			playTime = -1,
			transformMatrix = {x = 675, y = 120},
			bindGenerator = true,
			useGlobalRenderOrigin = true,
		},
		
		BlgParticles{
			path = "common/particles/level_selector_mist_waterfall.lua",
			playTime = -1,
			transformMatrix = {x = 680, y = 170},
			bindGenerator = true,
			useGlobalRenderOrigin = true,
		},
		
		BlgParticles{
			path = "common/particles/level_selector_mist_fountain.lua",
			playTime = -1,
			transformMatrix = {x = 710, y = 460},
			bindGenerator = true,
			useGlobalRenderOrigin = true,
		},
		
		BlgParticles{
			path = "common/particles/level_selector_mist_fountain.lua",
			playTime = -1,
			transformMatrix = {x = 740, y = 460},
			bindGenerator = true,
			useGlobalRenderOrigin = true,
		},
	},
	
	BlgButton{
		name = "backBtn",
		transformMatrix = {x = 20, y = 700, scaleX=1.0, scaleY=1.0},
		label = "back",
		font = {
			standardFont,
			25,
			DefaultColor,
		},
		upState = {
			bitmap = "common/buttons/btn_default",
		},
		overState = {
			bitmap = "common/buttons/btn_rollover",
		},
		downState = {
			bitmap = "common/buttons/btn_pushed",
			sound = kDefaultButtonSound,
		},

		command =
			function()
				SwapToState("scripts/states/statemainmenu.lua");
			end
	},
	
	BlgMovieClip
	{
		name = "dialogContainer",
		commands = {
			[30] = function()
				StopDialogAnimation();
			end,
			[60] = function()
				OnDialogAnimationFinished();
			end,
		},
	},
	
}