------------------------------------------------Variables------------------------------------------------

require( "package/quests/q_variables_define.lua" );
DebugOut( "generate main quests (file : package/quests/q_golden_syrup.lua)" );

--N.B: callbacks pour KillFX 
--Dans le cas d'un QuestElement qui ne possède qu'une seule action, le callback contenant le KillFX doit être
--le callback du QuestElement, pour éviter qu'à chaque rebouclage de l'action le FX soit tué puis relancé
----------------------------------------------
--Dans le cas d'un QuestElement qui possède plusieurs actions, le callback contenant le KillFX doit être
--le callback de l'action, pour que le FX soit bien tué entre chaque action
--(on a pas de pb de rebouclage du FX puisqu'on change d'action)

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

--####################################################################################################
--################################## QUETE 5 : LE SIROP D'OR #########################################
--####################################################################################################
------------------------------------------------------------------------------------------------------
---------------------Functions Part 1 : Libérer la reine des abeilles --------------------------------
------------------------------------------------------------------------------------------------------

function F_beeflower_fixing_inprogress( sOwnerName )
	PlayBrushSound( sOwnerName );
	return true
end

function F_beeflower_fixing_retrieve_inprogress( sOwnerName )
	PlayExplorationFX( sOwnerName );
	return true
end

function F_beeflower_fixing_cancel( sOwnerName )
	KillBrushSound( sOwnerName );
	KillExplorationFX( sOwnerName );
	return true
end

function F_beeflower_fixing_done( sOwnerName )
	KillBrushSound( sOwnerName );
	KillExplorationFX( sOwnerName );
	return true
end

function F_beeflower_fixing_first_time()
	DisplayHelpInformation( "TIP_nb_character_on_quest", "backgrounds/dialog/vignette_fee", "", 0, "", true )
end

function F_beeflower_fixing_end()
	SetAnim("LD_beeflower_anim", 2);
	CreateTimer( "T_beeflower_anim_end", 0.40, "F_beeflower_anim_end" )
	PlaySound( "sounds/pnj/abeille/bee_flower_1.ogg", false, FXSound, "LD_beeflower_anim" );
	PopElementTask("TASK_free_bee_queen");
	DisplayInformation( "free_bee_end", "backgrounds/dialog/vignette_abeille", "", 0, "F_bee_end_new_task", true)
	SetVisible("Bee_00", true);
	PlayFX(Fogs, "fx/abeilles.lua", "Bee_00", 0, 0);
	SetCleanable( "SD_masque", true);
  SetTipToShow( "TIP_banquet" )

	F_check_honey_gathering_ready()
	return true
end

function F_bee_end_new_task()
	if not IsQuestElementCompleted("Q_alambic_fixing") and not IsQuestElementBegin("Q_alambic_fixing") and GetNumberOfUsersUsingQuestElement("Q_alambic_fixing") == 0 then
		PushTask("TASK_find_alambic","TASK_find_alambic",11)
		PushTask("TASK_repair_alambic","TASK_repair_alambic",12)
		PlayFX(Air, "fx/fx_boutique_4.lua", "LD_alambic", 0, 0)
		SetFirstClickable( "LD_alambic",true )
	end
	
	if IsQuestElementCompleted( "Q_honey_spoon_fixing" ) then
		MoveQueenBee( -150, 1900 )
	else
		PlayFX(Air, "fx/fx_boutique_4.lua", "LD_honey_spoon", 0, 0)
		PushTask("TASK_find_spoon","TASK_find_spoon",13)
		PushTask("TASK_repair_spoon","TASK_repair_spoon",14)
		SetFirstClickable( "LD_honey_spoon",true )
	end
	
end

function F_beeflower_anim_end()
	SetAnim("LD_beeflower_anim", 3);
	return true
end

------------------------------------------------------------------------------------------------------
-----------------------------Functions Part 2 : Réparer la ruche -------------------------------------
------------------------------------------------------------------------------------------------------
function F_honey_spoon_fixing_first_click()
	PopElementTask("TASK_find_spoon")
	if IsFXPlaying(Air, "fx/fx_boutique_4.lua", "LD_honey_spoon") then
		KillFX(Air, "fx/fx_boutique_4.lua", "LD_honey_spoon")
	end
end

function F_honey_spoon_begin()
	PushTask("TASK_repair_spoon_ongoing","TASK_repair_spoon_ongoing",13.9)
end

function F_honey_spoon_cancel()
	if GetNumberOfUsersUsingQuestElement("Q_honey_spoon_fixing") <= 0 then
		PopElementTask("TASK_repair_spoon_ongoing")
		MoveFairyToTaskBar()
	end
	return true
end

function F_honey_spoon_fixing_begin( sOwnerName )
	PlayMillRepairSound( sOwnerName );
	return true
end

function F_honey_spoon_fixing_retrieve( sOwnerName )
	PlayRepairFX( sOwnerName );
	return true;
end

function F_honey_spoon_fixing_stop( sOwnerName )
	KillMillRepairSound( sOwnerName );
	KillRepairFX( sOwnerName );
	return true;
end

function F_honey_spoon_fixing_done( sOwnerName )
	F_honey_spoon_fixing_stop( sOwnerName )
	return true;
end

function F_honey_spoon_end( sOwnerName )
	
	SetVisible( "LD_honey_spoon", false )
	SetVisible( "LD_honey_spoon_repaired_bg", true )
	
	if IsQuestElementCompleted( "Q_beeflower_fixing" ) then
		MoveQueenBee( -150, 1900 )
		F_check_honey_gathering_ready()
	else
		SetVisible("LD_honey_spoon_repaired",true);
	end
	
	SetVisible("LD_honey_spoon_broken",false);
	-- if IsQuestElementCompleted("Q_tree_seed_planting") and IsQuestElementCompleted("Q_alambic_fixing") and IsQuestElementCompleted("Q_beeflower_fixing") then
		-- SetVisible("LD_honey_spoon_with_honey",true);
		-- SetVisible("O_alambic_red_bowl",false);
		-- 
		-- SetVisible("O_alambic_yellow_bowl",true);
		-- SetVisible("LD_alambic_red",false);
		-- PushFrontTask("TASK_firefly_syrup","TASK_firefly_syrup");
	-- else
		-- SetVisible("LD_honey_spoon_repaired",true);
	-- end
	PopElementTask("TASK_repair_spoon_ongoing")
	PopElementTask("TASK_repair_spoon")
	return true
end
------------------------------------------------------------------------------------------------------
------------------------------Functions Part 3 : Réparer l'alambic -----------------------------------
------------------------------------------------------------------------------------------------------
function F_alambic_fixing_first_click()
	PopElementTask("TASK_find_alambic")
	if IsFXPlaying(Air, "fx/fx_boutique_4.lua", "LD_alambic") then
		KillFX(Air, "fx/fx_boutique_4.lua", "LD_alambic")
	end
end

function F_alambic_begin()
	PushTask("TASK_repair_alambic_ongoing","TASK_repair_alambic_ongoing",11.9)
end

function F_alambic_cancel()
	if GetNumberOfUsersUsingQuestElement("Q_alambic_fixing") <= 0 then
		PopElementTask("TASK_repair_alambic_ongoing")
		MoveFairyToTaskBar()
	end
	return true
end

function F_alambic_fixing_begin( sOwnerName )
	PlayRepairSound( sOwnerName );
	return true
end

function F_alambic_fixing_retrieve( sOwnerName )
	PlayRepairFX( sOwnerName );
	return true
end

function F_alambic_fixing_stop( sOwnerName )
	KillRepairSound( sOwnerName );
	KillRepairFX( sOwnerName );
	return true
end

function F_alambic_end()
	SetVisible( "LD_waterploc_alambic", true);
	SetVisible( "LD_alambic", false );
	SetVisible( "LD_alambic_red", true );
	PopElementTask("TASK_repair_alambic")
	PopElementTask("TASK_repair_alambic_ongoing")
	F_check_honey_gathering_ready()
	F_check_flower_gathering_ready()
	
	return true
end

------------------------------------------------------------------------------------------------------
-------------------------Functions Part 4 : Apporter le miel à l'alambic -----------------------------
------------------------------------------------------------------------------------------------------
function F_check_honey_gathering_ready()
	if IsQuestElementCompleted("Q_beeflower_fixing") and IsQuestElementCompleted("Q_honey_spoon_fixing") then
		SetVisible("LD_honey_spoon_with_honey",true)
		SetCleanable("SD_confiture",true)
		
		if IsQuestElementCompleted("Q_alambic_fixing") then
			--SetVisible("LD_alambic_deposit_honey",true)
			DisplayQuestElementInTome( "Q_golden_syrup_honey_gathering", true )
			PushTask("TASK_prepare_syrup","TASK_prepare_syrup",15)
			SetTipToShow( "TIP_train" )
		end
	end
end

function F_golden_syrup_honey_gathering_end()
	SetVisible("LD_alambic_deposit_honey",false)
	F_check_syrup_distilling_ready()
end

function F_golden_syrup_honey_gathering_condition_end()
	check = false
	if GetFilledPercent( "LD_alambic_deposit_honey" ) >= 100 then
		check = true
	end	
	return check
end

function F_golden_syrup_honey_gathering_getprogress()
	return 100 - GetFilledPercent( "LD_alambic_deposit_honey" )
end

function F_golden_syrup_honey_gathering_condition_begin()
	return not IsQuestElementCompleted("Q_golden_syrup_honey_gathering")
end

function F_alambic_ingredient_gathering_begin()
	PushTask("TASK_prepare_syrup_collect_ongoing","TASK_prepare_syrup_collect_ongoing",14.8)
end

function F_alambic_ingredient_gathering_cancel()
	if GetNumberOfUsersUsingQuestElement("Q_golden_syrup_honey_gathering") + GetNumberOfUsersUsingQuestElement("Q_golden_syrup_flower_gathering") <= 0 then
		PopElementTask("TASK_prepare_syrup_collect_ongoing")
		MoveFairyToTaskBar()
	end
	return true
end
------------------------------------------------------------------------------------------------------
-------------------------Functions Part 5 : Apporter les fleurs à l'alambic -----------------------------
------------------------------------------------------------------------------------------------------
function F_check_flower_gathering_ready()
	if IsQuestElementCompleted("Q_alambic_fixing") then
		if IsQuestElementCompleted("Q_tree_seed_planting") then
			DisplayQuestElementInTome( "Q_golden_syrup_flower_gathering", true )
			--SetVisible("LD_alambic_deposit_flower",true)
			SetVisible("LD_swamp_with_flowers",true)
			PushTask("TASK_prepare_syrup","TASK_prepare_syrup",15)
		else
			PushTask("TASK_plant_swamp_seed","TASK_plant_swamp_seed",14.5)
		end
	end
end

function F_golden_syrup_flower_gathering_end()
	SetVisible("LD_alambic_deposit_flower",false)
	SetVisible("LD_swamp_with_flowers",false)
	F_check_syrup_distilling_ready()
end

function F_golden_syrup_flower_gathering_condition_end()
	check = false
	if GetFilledPercent( "LD_alambic_deposit_flower" ) >= 100 then
		check = true
	end	
	return check
end

function F_golden_syrup_flower_gathering_getprogress()
	return 100 - GetFilledPercent( "LD_alambic_deposit_flower" )
end

function F_golden_syrup_flower_gathering_condition_begin()
	return not IsQuestElementCompleted("Q_golden_syrup_flower_gathering")
end
------------------------------------------------------------------------------------------------------
-------------------------Functions Part 6 : Distiller le sirop d'or -----------------------------
------------------------------------------------------------------------------------------------------

function F_alambic_brewing_begin( sOwnerName )
	PlayBrushSound( sOwnerName )
	return true
end

function F_alambic_brewing_retrieve( sOwnerName )
	PlayInspectFX( sOwnerName )
	return true
end

function F_alambic_brewing_stop( sOwnerName )
	KillBrushSound( sOwnerName )
	KillInspectFX( sOwnerName )
	return true
end

function F_check_syrup_distilling_ready()
	if IsQuestElementCompleted("Q_golden_syrup_honey_gathering") and IsQuestElementCompleted("Q_golden_syrup_flower_gathering") then
		PopElementTask("TASK_prepare_syrup_collect_ongoing")
		SetVisible("LD_alambic_distillation",true)
		DisplayQuestElementInTome( "Q_golden_syrup_distilling", true )
		DisplayHelpInformation( "TIP_nb_character_on_quest", "backgrounds/dialog/vignette_fee", "", 0, "", true )
	else
		F_alambic_ingredient_gathering_cancel()
	end
end

function F_alambic_distilling_begin()
	PushTask("TASK_prepare_syrup_ongoing","TASK_prepare_syrup_ongoing",14.9)
end

function F_alambic_distilling_cancel()
	if GetNumberOfUsersUsingQuestElement("Q_golden_syrup_distilling") <= 0 then
		PopElementTask("TASK_prepare_syrup_ongoing")
		MoveFairyToTaskBar()
	end
	return true
end

function F_golden_syrup_distilling_end()
	SetVisible("O_alambic_red_bowl",false)
	SetVisible("O_alambic_yellow_bowl",true)
	SetVisible("LD_alambic_red",false)
	SetVisible("LD_alambic_distillation",false)
	DisplayQuestElementInTome( "Q_firefly_kingdom_golden_syrup_feeding", true )
	PopElementTask("TASK_prepare_syrup")
	PopElementTask("TASK_prepare_syrup_ongoing")
	PushTask("TASK_firefly_syrup","TASK_firefly_syrup",16)
end
--======================================================================================================
--=========================================Quest Description============================================
--======================================================================================================
Quest
{
	name = "Q_golden_syrup",
	description = "Q_golden_syrup_desc",
	callback_bestiary = {"bestiary_page5"},
	---------------------Part 1 : Libérer la reine des abeilles --------------------------------
	Quest_Element
	{
		name = "Q_beeflower_fixing",
		description_done = "Q_beeflower_fixing_done",
		zone = "Z_honey_queen",
		
		prerequisites = {"Q_swamp_watering_can_fixing"},
		
		Check
		{
			value_type = "nature",
			level = 3 * G_check_factor,
		},
		
		Check
		{
			is_for_knowledge_tree = true,
			value_type = "nature",
			level = 1 * G_check_factor,
		},
		
		gain_feerie = G_bee_flower_faerie_gain,
	  
		
		interaction = "LD_beeflower",
		callback_end = "F_beeflower_fixing_end",
		callback_first_time = "F_beeflower_fixing_first_time",
		
			
		max_users = 3,
		loop = true,

		Clean
		{
			stay_where_dropped = false,
			interaction = "LD_beeflower",
			label = "fixing",
			XpImproved = Nature,
			XpValue = G_xp_standard_gain_per_action,
			fixTime = 5,
			fix_factor = 10,
			ratio_bonus_feerie = G_faerie_standard_gain_per_action,	
			callback_inprogress = "F_beeflower_fixing_inprogress",	  
			callback_done = "F_beeflower_fixing_done",
			callback_cancel = "F_beeflower_fixing_cancel",
			callback_retrieve_inprogress = "F_beeflower_fixing_retrieve_inprogress",
		},
	},
	
	-----------------------------Part 2 : Réparer la ruche -------------------------------------

	Quest_Element
	{
		name = "Q_honey_spoon_fixing",
		description_done = "Q_honey_spoon_fixing_done",
		zone = "Z_honey_spoon",
		Check
		{
			value_type = "knowledge",
			level = 3 * G_check_factor,
		},
	
		Check
		{
			is_for_knowledge_tree = true,
			value_type = "knowledge",
			level = 1 * G_check_factor,
		},
		
		gain_feerie = G_spoon_fix_faerie_gain,

		interaction = "LD_honey_spoon",
		callback_firstclic = "F_honey_spoon_fixing_first_click",
		callback_end = "F_honey_spoon_end",
		callback_begin = "F_honey_spoon_begin",
		callback_cancel = "F_honey_spoon_cancel",
		
		

		max_users = 5,
		min_users = 2 * G_check_factor,
		loop = true,

		Fix
		{
			stay_where_dropped = false,
			interaction = "LD_honey_spoon",
			label = "fixing",
			XpImproved = Knowledge,
			XpValue = G_xp_standard_gain_per_action,
			fixTime = 5,
			fix_factor = 10,
			ratio_bonus_feerie = G_faerie_standard_gain_per_action,
			callback_inprogress = "F_honey_spoon_fixing_begin",
			callback_done = "F_honey_spoon_fixing_done",
			callback_cancel = "F_honey_spoon_fixing_stop",
			callback_retrieve_inprogress = "F_honey_spoon_fixing_retrieve",
		},
	},
	------------------------------Part 3 : Réparer l'alambic -----------------------------------
	

	Quest_Element
	{
		name = "Q_alambic_fixing",
		description_done = "Q_alambic_fixing_done",
		
		Check
		{
			value_type = "knowledge",
			level = 2 * G_check_factor,
		},

		Check
		{
			is_for_knowledge_tree = true,
			value_type = "knowledge",
			level = 0 * G_check_factor,
		},

		callback_end = "F_alambic_end",
		callback_firstclic = "F_alambic_fixing_first_click",
		callback_begin = "F_alambic_begin",
		callback_cancel = "F_alambic_cancel",
		
		gain_feerie = G_alambic_fix_faerie_gain,
		interaction = "LD_alambic",
	  
		max_users = 5,
		min_users = 2 * G_check_factor,
		loop = true,



		Fix
		{
			stay_where_dropped = false,
			interaction = "LD_alambic",
			label = "fixing",
			XpImproved = Knowledge,
			XpValue    = G_xp_standard_gain_per_action,
			fixTime = 5,
			fix_factor = 10,
			ratio_bonus_feerie = G_faerie_standard_gain_per_action,
			callback_inprogress = "F_alambic_fixing_begin",
			callback_done = "F_alambic_fixing_stop",
			callback_cancel = "F_alambic_fixing_stop",
			callback_retrieve_inprogress = "F_alambic_fixing_retrieve",
		},
	},
	-------------------------Part 4 : Apporter le miel à l'alambic -----------------------------
	Quest_Element
	{
		name = "Q_golden_syrup_honey_gathering",
		
		prerequisites = {"Q_honey_spoon_fixing","Q_beeflower_fixing","Q_alambic_fixing"},
		
		Check
		{
			value_type = "nature",
			level = 2 * G_check_factor,
		},

		Check
		{
			is_for_knowledge_tree = true,
			value_type = "nature",
			level = 1 * G_check_factor,
		},
		
		
		callback_end = "F_golden_syrup_honey_gathering_end",
		condition_begin = "F_golden_syrup_honey_gathering_condition_begin",
		condition_end = "F_golden_syrup_honey_gathering_condition_end",
		callback_getprogress = "F_golden_syrup_honey_gathering_getprogress",
		callback_begin = "F_alambic_ingredient_gathering_begin",
		callback_cancel = "F_alambic_ingredient_gathering_cancel",
		-- callback_first_time = "F_alambic_first_time",
		
		gain_feerie = G_honey_gather_faerie_gain,
		interaction = "LD_honey_spoon_with_honey",
		
		loop = true,
		stay_where_dropped = false,
		
		min_users = 2* G_check_factor,
		max_users = 5,
		
		see_x = 792,
		see_y = 1107,
		
		Gather
		{
			stay_where_dropped = false,
			source = "LD_honey_spoon_with_honey",
			XpImproved = Nature,
			XpValue    = G_xp_standard_gain_per_action,
			collectTime = 5, 
			gather_factor  = 10,
			ratio_bonus_feerie = G_faerie_standard_gain_per_action,
		},

		Deposite
		{
			label = "feeding",
			stock = "LD_alambic_deposit_honey",
			fixTime = 5,
		},
	},
	-------------------------Part 5 : Apporter les fleurs à l'alambic -----------------------------
	Quest_Element
	{
		name = "Q_golden_syrup_flower_gathering",
		
		prerequisites = {"Q_alambic_fixing","Q_tree_seed_planting"},
		
		Check
		{
			value_type = "nature",
			level = 3 * G_check_factor,
		},

		Check
		{
			is_for_knowledge_tree = true,
			value_type = "nature",
			level = 2 * G_check_factor,
		},
		
		condition_begin = "F_golden_syrup_flower_gathering_condition_begin",
		callback_end = "F_golden_syrup_flower_gathering_end",
		condition_end = "F_golden_syrup_flower_gathering_condition_end",
		callback_getprogress = "F_golden_syrup_flower_gathering_getprogress",
		callback_begin = "F_alambic_ingredient_gathering_begin",
		callback_cancel = "F_alambic_ingredient_gathering_cancel",
		-- callback_first_time = "F_alambic_first_time",
		
		gain_feerie = G_flower_gather_faerie_gain,
		interaction = "LD_swamp_with_flowers",
		
		loop = true,
		stay_where_dropped = false,
		
		min_users = 2* G_check_factor,
		max_users = 4,
		
		see_x = 792,
		see_y = 1107,
		
		Gather
		{
			stay_where_dropped = false,
			source = "LD_swamp_with_flowers",
			XpImproved = Nature,
			XpValue    = G_xp_standard_gain_per_action,
			collectTime = 5, 
			gather_factor  = 10,
			ratio_bonus_feerie = G_faerie_standard_gain_per_action,
		},

		Deposite
		{
			label = "feeding",
			stock = "LD_alambic_deposit_flower",
			fixTime = 5,
		},
	},
	-------------------------Part 6 : Distiller le sirop d'or -----------------------------
	Quest_Element
	{
		name = "Q_golden_syrup_distilling",
		
		prerequisites = {"Q_golden_syrup_honey_gathering","Q_golden_syrup_flower_gathering"},
		
		Check
		{
			value_type = "knowledge",
			level = 3 * G_check_factor,
		},

		Check
		{
			is_for_knowledge_tree = true,
			value_type = "knowledge",
			level = 1 * G_check_factor,
		},
		
		callback_end = "F_golden_syrup_distilling_end",
		callback_begin = "F_alambic_distilling_begin",
		callback_cancel = "F_alambic_distilling_cancel",
		
		gain_feerie = G_alambic_brewing_faerie_gain,
		interaction = "LD_alambic_distillation",
		
		loop = true,
		stay_where_dropped = false,
		
		min_users = 2* G_check_factor,
		max_users = 3,
		
		Inspect
		{
			stay_where_dropped = false,
			interaction = "LD_alambic_distillation",
			label = "fixing",
			XpImproved = Knowledge,
			XpValue    = G_xp_standard_gain_per_action,
			fixTime = 5,
			fix_factor = 10,
			ratio_bonus_feerie = G_faerie_standard_gain_per_action,
			callback_inprogress = "F_alambic_brewing_begin",
			callback_done = "F_alambic_brewing_stop",
			callback_cancel = "F_alambic_brewing_stop",
			callback_retrieve_inprogress = "F_alambic_brewing_retrieve",
		},
	},
}
