:NAME level_selection_nubbin

:CONFIG int32 chapter_index
:CONFIG int32 level_index
:CONFIG boolean dont_spawn_counter

:VAR boolean enabled
:VAR boolean refresh

-- ------------------- initialization ------------------- 

k_invalid= {}
k_invalid.red= 0.0
k_invalid.green= 0.0
k_invalid.blue= 0.0

k_locked= {}
k_locked.red= 0.63		--0.2
k_locked.green= 0.286	--0.2
k_locked.blue= 0.282	--0.2

k_disabled={}
k_disabled.red= 0.227		--0.2
k_disabled.green= 0.103		--0.2
k_disabled.blue= 0.102		--0.2

k_unlocked= k_locked		-- they are now distinguished with a different bitmap
--k_unlocked= {}
--k_unlocked.red= 0.47--0.7			--0.8
--k_unlocked.green= 0.47--0.4		--0.8
--k_unlocked.blue= 0--0.4		--0.3

k_beaten= {}
k_beaten.red= 0.89--0.92			--0.8		--0.0
k_beaten.green=	0.89--0.92		--0.8		--0.9
k_beaten.blue= 0--0.3			--0.3		--0.0

k_current_not_beaten= {}
k_current_not_beaten.red= 1				--1			--1
k_current_not_beaten.green= 0.85		--1		--0
k_current_not_beaten.blue= 0.85			--0.82		--0

k_current_beaten= {}
k_current_beaten.red= 1			--0.56		--1
k_current_beaten.green= 1		--1			--0
k_current_beaten.blue= 0.82		--0.56		--0

k_level_flag_cinematic= e_value("e_level_flags", "_level_cinematic")

-- ------------------- initialization ------------------- 

function config_initialize(c)
	c.chapter_index= -1
	c.level_index= -1
	c.dont_spawn_counter= false
end

function var_initialize(v)
	v.enabled= true
	v.refresh= true
end


-- ------------------- component ------------------- 

function move_to_marker(object, marker_name, shield)
	local marker= object.marker_get(marker_name)
	if (marker) then
		local pos= marker.transform.offset
		shield.position_set(pos.x, pos.y)
	end
end

function spawn_shields(object, disabled, level, level_record, color, force_color)
	local total_badge_count= level.badge_count()
	if (total_badge_count>0) then
		local shield_count= 0
		local badges_available= level.badges
		local badges_won= 0
		
		local shield_spawn= object.spawn_get("shield")		-- was shield0
		local shield_closed_spawn= object.spawn_get("shield_closed")		-- was shield0
		local star_spawn= object.spawn_get("star")
		local scale_factor= 1
		local perfect= false
		
		if (object.transform.scale.x>0) then
			scale_factor= 1/object.transform.scale.x
		end
	
		if (level_record) then
			badges_won= level_record.badges
			
			if (level_record.is_perfect()) then
				perfect= true
			end
			
		end
		
		if (perfect and star_spawn) then
			
			local star= star_spawn.spawn()
			--star.scale_set(scale_factor,scale_factor)
			
			local this_color= k_unlocked
			if (not disabled) then
				this_color= color
			elseif (force_color) then
				this_color= color
			else
				this_color= k_locked		--k_unlocked
			end
		
			local tint= star.tint_get()
			if (tint) then
				tint.tint.red= this_color.red
				tint.tint.green= this_color.green
				tint.tint.blue= this_color.blue
			end

		else
			for i= 0, 31 do
				if (bit_test(badges_available, i)) then
					-- we're talking about shield shield_count
					local spawner= nil
					local this_color= k_unlocked
					
					if ((not disabled) and (bit_test(badges_won, i) or force_color)) then
						spawner= shield_spawn
						this_color= color
					else
						spawner= shield_closed_spawn
						if (force_color) then
							this_color= color
						else
							this_color= k_locked		--k_unlocked
						end
					end
					
					if (spawner) then
						local shield= spawner.spawn()
						shield.scale_set(scale_factor,scale_factor)
						move_to_marker(object, "shield"..shield_count, shield)
					
						local tint= shield.tint_get()
						if (tint) then
							tint.tint.red= this_color.red
							tint.tint.green= this_color.green
							tint.tint.blue= this_color.blue
						end
					end
					
					shield_count= shield_count+1
					if (shield_count>=total_badge_count) then
						break
					end
				end
			end
		end
	end
end


function tint_shields(shields, color)
	local l= #shields
	if (l > 0) then
		local i
		for i= 0, #shields do
			local s= shields[i]
			local tint= s.tint_get()
			if (tint) then
				tint.tint.red= color.red
				tint.tint.green= color.green
				tint.tint.blue= color.blue
			end
		end
	end
end

function update_shield_status(config, shields, max_shield_count, level, level_record, color)

	if (max_shield_count>0) then
		local shield_count= 0
		local badges_available= level.badges
		local badges_won= 0
		
		if (level_record) then
			badges_won= level_record.badges
		end
		
		for i= 0, 31 do
			if (bit_test(badges_available, i)) then
				-- we're talking about shield shield_count
				local s= shields[shield_count]
				local tint= s.tint_get()
				
				if (tint) then
					if (bit_test(badges_won, i)) then
						-- light it up!
						-- grey it out
						tint.tint.red= color.red
						tint.tint.green= color.green
						tint.tint.blue= color.blue
					else
						-- grey it out
						-- note, "unlocked" as in "the color of unlocked levels" not that the shield is unlocked
						tint.tint.red= k_unlocked.red
						tint.tint.green= k_unlocked.green
						tint.tint.blue= k_unlocked.blue
					
					end
				end
				
				shield_count= shield_count+1
				if (shield_count>=max_shield_count) then
					break
				end
			end
		end
	end
end

function update_state(object)
	local profile= player_profile_get()
	local app_settings= app_settings_get()
	local campaign= campaign_get()
	var.enabled= true

	-- delete all children and start again	
	object.delete_all_children()
	
	if (profile and campaign) then
		local current_campaign= campaign.current_campaign
		if (current_campaign) then
		
			-- spawn my shields
			local level= current_campaign.get_level(config.chapter_index, config.level_index)
			local level_record= profile.level_record_get(current_campaign, config.chapter_index, config.level_index, false)
			local shields={}

			local tint= object.tint_get()
			if (not current_campaign.valid_level(config.chapter_index, config.level_index)) then
				var.enabled= false
				if (tint) then
					-- invalid level
					tint.tint.red= k_invalid.red
					tint.tint.green= k_invalid.green
					tint.tint.blue= k_invalid.blue
				end
			else
			
				local current_campaign= campaign.current_campaign
				local use_color= k_unlocked
				local force_color= false
				local disabled= false

				-- check the player profile as well
				--[[
				if (app_settings and not app_settings.level_is_unlocked(current_campaign, config.chapter_index, config.level_index)) then
					disabled= true
				elseif (profile and (not profile.chapter_is_unlocked(current_campaign, config.chapter_index)) and (not bit_test(level.flags, k_level_flag_cinematic))) then
					disabled= true
				end
				--]]
				
				disabled= not campaign.level_is_unlocked(config.chapter_index, config.level_index)
				
				if (disabled) then
					var.enabled= false
					use_color= k_disabled
					force_color= true
					disabled= true;
					
					-- disable my button
					local button= object.button_get()
					if (button) then
						button.disable()
					end

					local closed_spawner= object.spawn_get("nubbin_closed")
					if (closed_spawner) then
						local closed_nubbin= closed_spawner.spawn()
						if (closed_nubbin) then
							local tint= closed_nubbin.tint_get()
							if (tint) then
								tint.tint.red= use_color.red
								tint.tint.green= use_color.green
								tint.tint.blue= use_color.blue
							end
						end
					end
				else
					local last_played= false
					local show_as_beaten= level_record or (not level.winnable())

					-- we are either red (last played) green (won) or yellow (unlocked)

					if (profile.last_level_valid()) then
						local last_campaign= profile.last_campaign_get()
						if (last_campaign == current_campaign) then
							if ((profile.last_chapter_index==config.chapter_index) and (profile.last_level_index==config.level_index)) then
								last_played= true
							end
						end
					end

					if (last_played) then
						if (show_as_beaten) then
							-- current level, beaten
							use_color= k_current_beaten
						else
							-- current level, NOT beaten
							use_color= k_current_not_beaten
						end

						-- also play the animation
						object.tween_animation_play("pulse")

					elseif show_as_beaten then
						-- k_beaten
						use_color= k_beaten
					end
				end

				if (tint and use_color) then
					-- level is still locked
					tint.tint.red= use_color.red
					tint.tint.green= use_color.green
					tint.tint.blue= use_color.blue
				end

				if (level) then
					spawn_shields(object, disabled, level, level_record, use_color, force_color)
				end

			end
		end
	end

	-- spawn my counter text
	if (var.enabled and (not config.dont_spawn_counter)) then
		local index= config.level_index
		if (index > 0) then
			local counter= object_place("nubbin_counter")
			local marker= object.marker_get("counter")
			if (counter and marker) then
				local pos= marker.world_position_get(object)
				counter.position_set(pos.x, pos.y)

				object_attach(object, marker, counter, nil)

				local text_component= counter.text_get()
				if (text_component) then
				
					local text= tostring(index)
					if (index < 10) then
						text=text.." "
					end

					text_component.text_set(text)
				end
			end
		end
	end
end

function attach(object)
	--update_state(object)
end

function update(object)
	if (var.refresh) then
		update_state(object)
		var.refresh= false
		--disable script forever
	end
end

component_register(attach, update, nil, nil)
