:NAME store

:TILE c_object*		object
:TILE c_object*		marker
:TILE string		bundle_id

:COUNTER c_object*	object
:COUNTER int32		consumable_index
:COUNTER uint16		current_count
:COUNTER s_plate*	previous_plate

:GLOBAL float32		unused
:GLOBAL tile[]		tiles
:GLOBAL counter[]	counters
:GLOBAL boolean		was_paused
:GLOBAL float32		dismiss_delay

k_ui_time_mode= e_value("e_time_mode", "_time_mode_ui")


k_max_tile_count= 5

k_tile_placement_names= {}
k_tile_placement_names[0]= "tile0"
k_tile_placement_names[1]= "tile1"
k_tile_placement_names[2]= "tile2"
k_tile_placement_names[3]= "tile3"
k_tile_placement_names[4]= "tile4"


k_freeze_counter={}
k_freeze_counter.placement_name= "freeze_icon"
k_freeze_counter.item_id= "freeze"

k_shield_counter={}
k_shield_counter.placement_name= "shield_icon"
k_shield_counter.item_id= "shield"

k_distract_counter={}
k_distract_counter.placement_name= "distract_icon"
k_distract_counter.item_id= "distract"

k_counters={}
k_counters[0]= k_freeze_counter
k_counters[1]= k_shield_counter
k_counters[2]= k_distract_counter



k_item_animate_delay= 0.1
k_dismiss_delay= 0.5


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


function global_initialize(g)
end


-- ------------------- system ------------------- 

function place_plate(plate_name)
	local plate= world_get().get_plate(plate_name)
	if (plate) then
		world_state_get().place_plate(plate)
	else
		print("ERROR: could not find plate named "..plate_name)
	end
end


function place_text(ws, tile_object, placement, marker, text_string)
	local object= ws.place(placement)
	if (object) then
		object_attach(tile_object, marker, object, nil)
		
		-- set the title itself
		local text= object.text_get()
		if (text) then
			text.text_set(text_string)
		end
	end

end


function place_tiles()
	local store= store_get()
	if (store) then
		
		local ws= world_state_get()
		local w= world_get()
		local plate= w.get_plate("tiles")
		local plate_state= plate.plate_state_get()
		
		local template_placement= plate.placement_get("tile_template")
		local title_placement= plate.placement_get("tile_title")
		local description_placement= plate.placement_get("tile_description")
		local price_placement= plate.placement_get("tile_price")
		
		local bundle_count= store.bundles.count
		bundle_count= math.min(bundle_count, k_max_tile_count)
		
		local icon_marker= nil
		local title_marker= nil
		local description_marker= nil
		
		if (template_placement) then
			local model= template_placement.object_definition.model
			if (model) then
				icon_marker= model.marker_get("icon")
				title_marker= model.marker_get("title")
				description_marker= model.marker_get("description")
			end
		end
		
		for index= 0, bundle_count-1 do
		
			local marker_name= k_tile_placement_names[index]
			local marker_object= plate_state.object_get(marker_name)
			local bundle= store.bundles[index]
			local tile_object= ws.place(template_placement)
			local tile_struct= global.tiles.create()
			
			-- MEGA-HACK MEGA-HACK MEGA-HACK MEGA-HACK MEGA-HACK
			-- the fifth one down is on the black part of the screen
			-- so spawn the white version of the title
			if (index==4) then
				title_placement= plate.placement_get("tile_title_white")
			end
			
			if (marker_object and bundle and tile_object and tile_struct) then
				
				local pos= marker_object.world_position_get()
				marker_object.position_set(pos.x, -2)
				
				object_attach(marker_object, nil, tile_object, nil)
				
				tile_struct.object= tile_object
				tile_struct.marker= marker_object
				tile_struct.bundle_id= bundle.id
				
				-- place icon, title and description?
				if (title_marker and title_placement) then
					place_text(ws, tile_object, title_placement, title_marker, bundle.name)
				end

				if (description_marker) then
					place_text(ws, tile_object, description_placement, description_marker, bundle.description)
				end
				
				-- look for a placement that has the same name as our bundle id
				if (icon_marker) then
					local icon= nil
					local icon_placement= plate.placement_get(bundle.id)
					if (icon_placement) then
						icon= ws.place(icon_placement)
						if (icon) then
							object_attach(tile_object, icon_marker, icon, nil)
						end
					end
					
					if (price_placement) then
						local price_object= ws.place(price_placement)
						if (price_object) then
							object_attach(tile_object, icon_marker, price_object, nil)
							
							-- then offset to move out of the way of the icon
							if (icon) then
								local icon_pos= icon.world_position_get()
								local to_left_edge= icon_pos.x - icon.bounding_box.minx
								local offset= price_object.transform.offset
								price_object.position_set(offset.x-to_left_edge-0.04, offset.y)
								
								store.display_price(bundle.id, price_object)
							end
						end
					end
				end

				-- finally animate the tile object
				marker_object.tween_animation_play_after_delay("start", index*k_item_animate_delay);
				
			end
			
		end
	else
		-- kill myself! There is no store!
	end
end

function place_counters()
	local store= store_get()
	local player_profile= player_profile_get()
	if (store and player_profile) then
	
		local purchases= player_profile.purchases
		local ws= world_state_get()
		local w= world_get()
		local plate= w.get_plate("counters")
		--local ps= ws.plate_state_get("counters")
		for i=0, #k_counters do
		
			local counter_def= k_counters[i]
			local previous_plate= nil
			--local counter_object= ps.object_get(counter_def.placement_name)
			
			-- see if these icons already exist
			local counter_object= object_get(counter_def.placement_name)
			
			if (counter_object) then
				-- it exists already! Just move it to our current plate if necessary
			else
				-- spawn an icon here
			end

			if (counter_object) then
				local counter_struct= global.counters.create()
				if (counter_struct) then
					--:COUNTER c_object*	object
					--:COUNTER uint16		consumable_index
					--:COUNTER uint16		current_count
					
					counter_struct.object= counter_object
					counter_struct.consumable_index= -1
					counter_struct.current_count= 0
					counter_struct.previous_plate= nil
					
					if (counter_object.plate~=plate) then
						counter_struct.previous_plate= counter_object.plate
						ws.object_move_to_plate(counter_object, plate)
					end
					
					local item= store.item_get_by_id(counter_def.item_id)
					if (item) then
						counter_struct.consumable_index= item.consumable_index
					end

				end
			end
		end
	end
end

function place_scrim()
	local ws= world_state_get()
	local ps= ws.plate_state_get("scrim")
	
	if (ps) then
		local object= ps.objects
		while true do
			if (object) then
				object.tween_animation_play("start")
				object= object.plate_object_next
			else
				break
			end
		end
	end
end

function remove_scrim()
	local ws= world_state_get()
	local ps= ws.plate_state_get("scrim")
	
	if (ps) then
		local object= ps.objects
		while true do
			if (object) then
				if (not object.tween_animation_play_direction("start", false, false)) then
					object_delete(object)
				end
				object= object.plate_object_next
			else
				break
			end
		end
	end
end

function start()

	local ws= world_state_get()

	-- place everything and populate it!
	
	place_scrim()
	
	place_tiles()
	place_counters()
	
	global.was_paused= ws.game_is_paused()
	if (not global.was_paused) then
		ws.game_time_pause()
	end
	
	-- tell the "done" button to do it's thing
	local ps= ws.plate_state_get("tiles")
	if (ps) then
		local done_object= ps.object_get("done")
		if (done_object) then
			done_object.tween_animation_play_direction("start", true, false)
		end
	end
	
	ws.sound_manager.play_sound_by_name("forward", k_ui_time_mode)
	
end


function tile_from_tile_object(tile_object)
	local iterator= global.tiles.iterator_get()
	local result= nil

	while (true) do
		local tile= iterator.next()
		if (tile) then
			if (tile.object==tile_object) then
				result= tile
				break
			end
		else
			break
		end
	end
	
	return result
end

function start_dismiss()

	local ws= world_state_get()
	local iterator= global.tiles.iterator_get()
	while (true) do
		local tile_struct= iterator.next()
		if (tile_struct) then
			local marker_object= tile_struct.marker
			if (marker_object) then
				if (not marker_object.tween_animation_play_direction("start", false, false)) then
					object_delete(marker_object)
				end
			end
		else
			break
		end
	end
	
	iterator= global.counters.iterator_get()
	while (true) do
		local counter_struct= iterator.next()
		if (counter_struct) then
			if (not counter_struct.previous_plate) then
				if (not counter_struct.object.tween_animation_play_direction("slide_in", false, false)) then
					object_delete(counter_struct.object)
				end
			end
		else
			break
		end
	end
	
	-- tell the "done" button to go away as well
	local ps= ws.plate_state_get("tiles")
	if (ps) then
		local done_object= ps.object_get("done")
		if (done_object) then
			done_object.tween_animation_play_direction("start", false, false)
		end
	end
	
	remove_scrim()

	global.dismiss_delay= k_dismiss_delay
	
	-- play sound
	ws.sound_manager.play_sound_by_name("back", k_ui_time_mode)
end

function unload_zone()
	local ws= world_state_get()

	--if (global.sound_back) then
	--	global.sound_back.play(k_ui_time_mode)
	--end
	
	if (not global.was_paused) then
		ws.game_time_play()
	end
	
	-- finally move appropriate icons back to their plate
	local iterator= global.counters.iterator_get()
	while (true) do
		local counter_struct= iterator.next()
		if (counter_struct) then
			if (counter_struct.previous_plate) then
				ws.object_move_to_plate(counter_struct.object, counter_struct.previous_plate)
			end
		else
			break
		end
	end

	world_get().zone_unload("store")
end


function update()

	if (global.dismiss_delay>0) then
		local ws= world_state_get()
		local dt= ws.master_time.delta_t()
		global.dismiss_delay = global.dismiss_delay-dt
		if (global.dismiss_delay<=0) then
			unload_zone()
		end
	else
		local touch_interface= touch_get()
		if (touch_interface and touch_interface.touch_started()) then
			local touch_index= touch_interface.touch_start_index()
			local touch= touch_interface.touch[touch_index]
			local touched_object= touch.touch_handler_object
			local placement= touched_object.placement
			if (placement) then
				local touched_name= placement.name
				if (touched_name=="tile_template") then
					-- we touched a purchase tile
					local tile= tile_from_tile_object(touched_object)
					if (tile) then
					
						local store= store_get()
						local player_profile= player_profile_get()
						
						if (store.test_purchase(tile.bundle_id, player_profile)) then
							store.submit_purchase(tile.bundle_id, player_profile)
							
							-- play sound
							local ws= world_state_get()
							ws.sound_manager.play_sound_by_name("forward", k_ui_time_mode)
							
						else
							print("purchase failed!")
						end
					end
				elseif (touched_name=="done") then
					start_dismiss()
				end
			end
		end
	end
end

-- callback from the store
function purchase_complete(bundle, successful)
	if (successful) then
		-- success
	else
		-- TODO: generate a real error message?
		print("purchase failed")
	end
end


system_register(start, update, nil, nil)

event_register(purchase_complete, e_value("e_script_event", "_script_event_purchase_complete"))
