-------------------------------------------------------------------------------------------------
-- содержит функции и константы для настройки и обработки событий в игре
-------------------------------------------------------------------------------------------------

-- сдвиг описания бога относительно центра бога
local g_GodHintOffset = {}
g_GodHintOffset.x = 50
g_GodHintOffset.y = 50

-- сдвиг описания амулета относительно центра амулета
local g_AmuletHintOffset = {}
g_AmuletHintOffset.x = 25
g_AmuletHintOffset.y = -60

-------------------------------------------------------------------------------------------------

function init_game_events()
	g_GameVars.events = {}
end

-------------------------------------------------------------------------------------------------

function update_game_events()
	if (not g_GameVars.events.on_begin_level) and g_StripeArea:IsPlaying() then
		g_GameVars.events.on_begin_level = true
		event_on_begin_level()
	end
end

-------------------------------------------------------------------------------------------------
-- константы

local g_GemInfoLevel = 2
local g_RevealerInfoLevel = 5
local g_PauseInfoLevel = 23
local g_BonusScoreInfoLevel = 11
local g_FillShapeInfoLevel = 41
local g_SwitchShapeInfoLevel = 60

g_Flash = { 
	gameplay_info= "images/flash/frame1.swf",
	discard_info	= "images/flash/frame2.swf",
	goal_reached	= "images/flash/frame3.swf",
	gift_info	= "images/flash/frame4.swf",
	god_info	= "images/flash/god.swf",
	amulet_info	= "images/flash/amulet.swf",
	combo_info	= "images/flash/combo.swf",


	revealer_info	= "images/flash/revealer.swf",
	gem_info	= "images/flash/gem.swf",
	pause_info	= "images/flash/pause.swf",

	bonusscore_info	= "images/flash/bonusscore.swf",
	fillshape_info		= "images/flash/fillshape.swf",
	switchshape_info	= "images/flash/switchshape.swf",

      shop_gift_info    = "images/flash/shop_gift.swf",
      shop_amulet_info	= "images/flash/shop_amulet.swf"   
}

-------------------------------------------------------------------------------------------------
function event_on_init_level()
	g_GameVars.events.on_begin_level = false

	if g_Player:GetLevel()==g_GemInfoLevel then
		g_GameVars.enable_gem_bonus = true
		if not g_GameVars.events.gem_info then
			create_hole_bonus("Gem")
		end
	elseif g_Player:GetLevel()==g_RevealerInfoLevel and not g_GameVars.events.revealer_info then
		create_hole_bonus("Revealer")
	elseif g_Player:GetLevel()==g_PauseInfoLevel and not g_GameVars.events.pause_info then
		create_hole_bonus("Pause")
	end
end

-------------------------------------------------------------------------------------------------

function event_on_start_play()
	if g_Player:GetLevel()==g_BonusScoreInfoLevel and not g_GameVars.events.bonus_score_info then
		g_Pipeline:RemoveFirstShape()
		add_shape(g_Pipeline, 3)
	elseif g_Player:GetLevel()==g_FillShapeInfoLevel and not g_GameVars.events.fill_shape_info then
		g_Pipeline:RemoveFirstShape()
		add_shape(g_Pipeline, 2)
	elseif g_Player:GetLevel()==g_SwitchShapeInfoLevel and not g_GameVars.events.switch_shape_info then
		g_Pipeline:RemoveFirstShape()
		add_shape(g_Pipeline, 1)
	end
end

-------------------------------------------------------------------------------------------------

function show_hint_on_bouns_in_hole(id, flash)
	local x = nil
	local y = nil
	local stripe_num = 0
	while stripe_num< g_StripeArea:EnumStripes() do
		local stripe = g_StripeArea:GetStripe(stripe_num)
		if not stripe then 
			log("Some error in event_on_begin_level gem_info event!") 
			return 
		end

		local cur_hole = 0
		while cur_hole < stripe:EnumHoles()  do
			local h = stripe:GetHole(cur_hole)
			if h and h:GetBonus():IsEnabled() and h:GetBonus():GetBonusID()==id
			then 
				x = h:GetAbsPos().x + h:GetRect().mWidth/2
				y = h:GetAbsPos().y + h:GetRect().mHeight/2
				break 
			end
			cur_hole = cur_hole + 1
		end
		if x and y then break end

		stripe_num = stripe_num + 1
	end
	if x and y then create_hint(flash,  x, y) end
end

-------------------------------------------------------------------------------------------------

function event_on_begin_level()
	if (not g_GameVars.events.gameplay_info) and g_Player:GetLevel()==1 then
		g_GameVars.events.gameplay_info = true
		local shape = g_Pipeline:GetShape(3)
		local x = shape:GetAbsPos().x + shape:GetRect().mWidth/2
		local y = shape:GetAbsPos().y + shape:GetRect().mHeight/2
		create_hint(g_Flash.gameplay_info,  x, y)
	elseif (g_Player:GetLevel()==g_GemInfoLevel and not g_GameVars.events.gem_info)
	or      (g_Player:GetLevel()==g_RevealerInfoLevel and not g_GameVars.events.revealer_info)
	or      (g_Player:GetLevel()==g_PauseInfoLevel and not g_GameVars.events.pause_info) 
	then
		local flash = ""
		local id = ""
		if g_Player:GetLevel()==g_GemInfoLevel then
			g_GameVars.events.gem_info = true
			id = "Gem"
			flash = g_Flash.gem_info
		elseif g_Player:GetLevel()==g_RevealerInfoLevel then
			g_GameVars.events.revealer_info = true
			id = "Revealer"
			flash = g_Flash.revealer_info
		elseif g_Player:GetLevel()==g_PauseInfoLevel then
			g_GameVars.events.pause_info = true
			id = "Pause"
			flash = g_Flash.pause_info
		end
		show_hint_on_bouns_in_hole(id, flash)

	elseif (g_Player:GetLevel()==g_BonusScoreInfoLevel and not g_GameVars.events.bonus_score_info)
	or      (g_Player:GetLevel()==g_FillShapeInfoLevel and not g_GameVars.events.fill_shape_info)
	or      (g_Player:GetLevel()==g_SwitchShapeInfoLevel and not g_GameVars.events.switch_shape_info) 
	then
		local flash = ""
		if g_Player:GetLevel()==g_BonusScoreInfoLevel then
			g_GameVars.events.bonus_score_info = true
			flash = g_Flash.bonusscore_info
		elseif g_Player:GetLevel()==g_FillShapeInfoLevel then
			g_GameVars.events.fill_shape_info = true
			flash = g_Flash.fillshape_info
		elseif g_Player:GetLevel()==g_SwitchShapeInfoLevel then
			g_GameVars.events.switch_shape_info = true
			flash = g_Flash.switchshape_info
		end
		local shape = g_Pipeline:GetShape(g_Pipeline:EnumShapes()-1)
		if not shape 
		then 
			log("! ERROR in event_on_begin_level")
			return 
		end
		local x = shape:GetAbsPos().x + shape:GetRect().mWidth/2
		local y = shape:GetAbsPos().y + shape:GetRect().mHeight/2
		if x and y then create_hint(flash,  x, y) end
	end
end

-------------------------------------------------------------------------------------------------

function event_on_score_changed()
	-- событие на достижение Goal Score
	if (not g_GameVars.events.goal_reached) and (not g_GameVars.goal_reached) and g_GameVars.level_score >= g_GameVars.goal_score then
		g_GameVars.events.goal_reached = true
		local x = g_ScoreView:GetAbsPos().x + g_ScoreView:GetRect().mWidth/2
		local y = g_ScoreView:GetAbsPos().y + g_ScoreView:GetRect().mHeight/2
		create_hint(g_Flash.goal_reached,  x, y)
	end

	if g_Player:GetLevel()==1 and g_AmuletPlace:IsValidAmulet() then
		-- описание бога на 1 уровне
		if (not g_GameVars.events.god_info) and g_AmuletPlace:GetCompletePercent()>0.25 and g_GameVars.current_amulet==2 then
			g_GameVars.events.god_info = true
			local x = g_GodPlace:GetAbsPos().x + g_GodPlace:GetRect().mWidth/2 + g_GodHintOffset.x
			local y = g_GodPlace:GetAbsPos().y + g_GodPlace:GetRect().mHeight/2 + g_GodHintOffset.y
			create_hint(g_Flash.god_info,  x, y)
		-- описание амулета на 1 уровне
		elseif (not g_GameVars.events.amulet_info) and g_AmuletPlace:GetCompletePercent()>=0.50 and g_GameVars.current_amulet==1 then
			g_GameVars.events.amulet_info = true
			local x = g_AmuletPlace:GetAbsPos().x + 59 + g_AmuletHintOffset.x
			local y = g_AmuletPlace:GetAbsPos().y + 63 + g_AmuletHintOffset.y

			create_hint(g_Flash.amulet_info,  x, y)
		end

		if not g_GameVars.events.gem_info and g_AmuletPlace:GetCompletePercent()>=0.75 and g_GameVars.current_amulet==2 then
			if not create_hole_bonus("Gem") then return end

			g_GameVars.enable_gem_bonus = true
			g_GameVars.events.gem_info = true

			local life = get_number("bonuses", "show_time")

 			local hint = CParticleNode()
			hint:SetLifetime(life + 50)

			local show_hint = CLuaStringAction(g_Lua, "show_hint_on_bouns_in_hole('Gem', g_Flash.gem_info)")
			show_hint:OnceActionTime(life)
			hint:AddAction(show_hint)
	

			local w = CParticleWidget("", 0)
			w:AddParticleNode(hint)
			w:Start()
			g_Game:AddWidget(w)
		end
	end
end

-------------------------------------------------------------------------------------------------


function event_on_pickup_shape(shape)
	if  g_GameVars.events.discard_info 
	or g_GameVars.current_amulet > g_GameVars.amulets_num
	or (g_AmuletPlace:IsValidAmulet() and g_AmuletPlace:GetCompletePercent()>=1.0)
	or shape:GetType()=="CFillShape"
	or g_StripeArea:HasFreeHoles(shape:GetShapeID(), false) 
	then 
		return 
	end

	-- игрок взял фигуру, которую некуда поставить: описание сброса
	g_GameVars.events.discard_info = true
	local x = g_Discard:GetAbsPos().x + g_Discard:GetRect().mWidth/2
	local y = g_Discard:GetAbsPos().y + g_Discard:GetRect().mHeight/2
	create_hint(g_Flash.discard_info,  x, y)
end

-------------------------------------------------------------------------------------------------

function event_on_insert_shape(shape, combo)
	if g_GameVars.events.combo_info 
	or combo<=0
	or g_GameVars.current_amulet > g_GameVars.amulets_num 
	then 
		return 
	end

	-- игрок собрал комбу
	g_GameVars.events.combo_info = true
	
	local x = shape:GetAbsPos().x + shape:GetRect().mWidth/2
	local y = shape:GetAbsPos().y + shape:GetRect().mHeight/2
	create_hint(g_Flash.combo_info,  x, y)
	g_Cursor:GetShape():Hide(true)
end

-------------------------------------------------------------------------------------------------

function event_on_satisfaction_changed()
	if g_GameVars.events.gifts_info_prelude
	or g_GameVars.satisfaction > 63 then return end

	-- низкая сатисфакция, описать дары
	CreateGiftFlashAndHintEffect(g_Game, "image_game_gui_food", g_Game:GetWidget("food"))
	g_GameVars.events.gifts_info_prelude = true
end

-------------------------------------------------------------------------------------------------

function event_show_gift_info()
	if g_GameVars.events.gifts_info then return end

	-- описать дары
	local food = g_Game:GetWidget("food")
	if not food then return end
	local x = food:GetAbsPos().x + food:GetRect().mWidth/2
	local y = food:GetAbsPos().y + food:GetRect().mHeight/2
	create_hint(g_Flash.gift_info,  x, y)

	g_Game:RemoveWidget("gift_flash_particle_widget")

	g_GameVars.events.gifts_info = true
	g_GameVars.events.gifts_info_prelude = true
end

-------------------------------------------------------------------------------------------------

function event_on_shop_window_open(shop)

	if profile_manager():GetCurrentProfile():IsHintsEnabled() then 
		if not g_GameVars.events.shop_info then
			create_dialog(shop, "shop_info_dialog")
		end
	end

	g_GameVars.events.use_shop = true
	g_GameVars.events.shop_info = true
end

-------------------------------------------------------------------------------------------------

function event_on_show_statistics(stat)
	if g_Player:GetLevel()<3 then return end

	if profile_manager():GetCurrentProfile():IsHintsEnabled() then 
		if not g_GameVars.events.use_shop then
			create_dialog(stat, "shop_is_good_dialog")
		end
	end

	g_GameVars.events.use_shop = true
end

-------------------------------------------------------------------------------------------------

function event_on_amulet_buy(amulet)
	if g_GameVars.events.shop_amulet_info then return end

	-- описать кнопку покупки амулета
	local x = amulet:GetAbsPos().x + amulet:GetRect().mWidth/2
	local y = amulet:GetAbsPos().y + amulet:GetRect().mHeight/2
	create_hint(g_Flash.shop_amulet_info,  x, y)

	g_GameVars.events.shop_amulet_info = true
end

-------------------------------------------------------------------------------------------------

function event_on_gift_buy(gift)
	if g_GameVars.events.shop_gift_info then return end

	-- описать кнопку покупки амулета
	local x = gift:GetAbsPos().x + gift:GetRect().mWidth/2
	local y = gift:GetAbsPos().y + gift:GetRect().mHeight/2
	create_hint(g_Flash.shop_gift_info,  x, y)

	g_GameVars.events.shop_gift_info = true
end

-------------------------------------------------------------------------------------------------

function create_hint(flash, x, y)
	if not profile_manager():GetCurrentProfile():IsHintsEnabled() then return end

	local hint = CFlashHintWidget(g_Loader:GetTemplate("bubble_help"))
	hint:PlayFlash(flash)
	hint:PointHintTo(x, y)
	g_State:AddWidget(hint)
	pause_game(true)
end

-------------------------------------------------------------------------------------------------
