local luaCutscenesUtils = require("#Celeste.Mod.MaxHelpingHand.Module.LuaCutscenesUtils")
local guide = getEntity("DialogNPC", "Celeste.Mod.DoonvHelper.Entities.")
local dogBack = getFlag("dogIsBack")
local dogBackQuest = getFlag("dogBackQuestDone")
local talkedWithDryad = getFlag("dryadIsOk")
local worriedQuest = getFlag("worriedQuestDone")
local randomTip = 0
local greeting = 0

function onBegin()
	disableMovement()
	makeUnskippable()
	disableRetry()
	guide.CutsceneModeEnabled = true
	math.randomseed(os.time())
	playSound("event:/CC/CC_KoseiDiamond/ui_pause")
	if not getFlag("firstGreeting",true) and not getFlag("cryoGoldenRun") then
		coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_firstgreeting"))
		setFlag("firstGreeting",true)
		enableMovement()
		enableRetry()
		guide.CutsceneModeEnabled = false
		endCutscene()
	end
	if dogBack and not dogBackQuest then -- gives reward for dog
		coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_dialogue_5_1_2"))
		playSound("event:/game/general/seed_complete_berry")
		setFlag("dogBackQuestDone")
		enableMovement()
		enableRetry()
		guide.CutsceneModeEnabled = false
		endCutscene()
	end
	if talkedWithDryad and not worriedQuest then -- gives reward for dryad
		coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_dialogue_5_1_3"))
		playSound("event:/game/general/seed_complete_berry")
		setFlag("worriedQuestDone")
		enableMovement()
		enableRetry()
		guide.CutsceneModeEnabled = false
		endCutscene()
	end
-- The guide says hi and you start a dialogue if no quests have been recently completed
	greeting = math.random(0,2)
	if getFlag("cryoGoldenRun") then
		coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_golden"))
	else
		if getFlag("PartyCenter") then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_party"))
		else
			if getFlag("weatherRain") then
				if greeting == 0 then
					coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_greetingsD"))
				elseif greeting == 1 then
					coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_greetingsE"))
				elseif greeting == 2 then
					coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_greetingsF"))
				end
			elseif greeting == 0 then
				coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_greetings"))
			elseif greeting == 1 then
				coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_greetingsB"))
			elseif greeting == 2 then
				coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_greetingsC"))
			end
		end
	end
	
	coroutine.yield(luaCutscenesUtils.ChoiceWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_dialogue1_1", "CC_KoseiDiamond_guide_dialogue1_5", "CC_KoseiDiamond_guide_dialogue1_3", "CC_KoseiDiamond_guide_dialogue1_4", "CC_KoseiDiamond_guide_dialogue1_2"))
	if luaCutscenesUtils.GetChoice() == 1 then -- HOW TO PROGRESS
		if getFlag("boss_dead") then 																				-- killed wall of seekers
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_progress1_8"))
		elseif getFlag("Upgrade_DroneTeleport") then 																-- found rod of discord, now final boss
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_progress1_7"))
			setFlag("hint_wallofseekers",true)
		elseif getFlag("Upgrade_LightningDash") then 														-- found lightning dash, can you teleport tho?
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_progress1_6b"))
			setFlag("upgrade_hint_rodofdiscord",true)
		elseif getFlag("Upgrade_VariaJacket") then 															-- found molten charm, get lightning dash now
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_progress1_6"))
			setFlag("upgrade_hint_lightningdash",true)
		elseif getFlag("Upgrade_GoldenFeather",true) or getFlag("Upgrade_WaveBeam") then 					-- found scaly truffle or phaser beam, go get molten charm
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_progress1_5"))
			setFlag("upgrade_hint_moltencharm",true)
		elseif getFlag("Upgrade_MegaBombs") then 															-- found dynamite, go to the sky
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_progress1_4"))
			setFlag("upgrade_hint_sky",true)
		elseif getFlag("Upgrade_EtherealDash") then 															-- got aethereal cloak, get dynamite
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_progress1_3"))
			setFlag("upgrade_hint_dynamite",true)
		elseif getFlag("Upgrade_SpiderMagnet") then 															-- found celestial magnet, go to jungle
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_progress1_2"))
			setFlag("upgrade_hint_jungle",true)
			if not getFlag("Upgrade_MegaBombs") then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_progress1_2c"))
			setFlag("upgrade_hint_dynamite",true)
			end
		elseif getFlag("Upgrade_Bombs") then 																-- found bombs, go to dungeon
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_progress1_2b"))
			setFlag("upgrade_hint_dungeon",true)
		elseif getFlag("Upgrade_RemoteDrone") then 															-- found kwad racer drone, go get celestial magnet in caves
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_progress1_1"))
			setFlag("upgrade_hint_celestialmagnet",true)
		elseif getFlag("Upgrade_DashBoots") then 															-- found tabi, get drone or bombs
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_dialogue2_1d")) 
			setFlag("upgrade_hint_droneorbombs",true)
		elseif getFlag("Upgrade_ClimbingKit") then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_dialogue2_1c"))
		else
			if not getFlag("guideHasSaidProgressionIntro") then
				coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_dialogue2_1"))-- progression intro
				setFlag("guideHasSaidProgressionIntro",true)
			end
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_dialogue2_1b")) 	-- go get power grip and tabi
		end
	elseif luaCutscenesUtils.GetChoice() == 5 then -- bye stupid ass
		coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_dialogue2_2"))
	elseif luaCutscenesUtils.GetChoice() == 3 then -- wanna go somewhere
		coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_dialogue2_3"))
		coroutine.yield(luaCutscenesUtils.ChoiceWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_dialogue3_1", "CC_KoseiDiamond_guide_dialogue3_2", "CC_KoseiDiamond_guide_dialogue3_3",
		"CC_KoseiDiamond_guide_dialogue3_4", "CC_KoseiDiamond_guide_dialogue3_5", "CC_KoseiDiamond_guide_dialogue3_7", "CC_KoseiDiamond_guide_dialogue3_8",
		"CC_KoseiDiamond_guide_dialogue3_9", "CC_KoseiDiamond_guide_dialogue3_10", "CC_KoseiDiamond_guide_dialogue3_11", "CC_KoseiDiamond_guide_dialogue3_12"))
		if luaCutscenesUtils.GetChoice() == 1 then -- snow
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_dialogue4_1"))
		elseif luaCutscenesUtils.GetChoice() == 2 then -- desert
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_dialogue4_2"))
		elseif luaCutscenesUtils.GetChoice() == 3 then -- crimson
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_dialogue4_3"))
		elseif luaCutscenesUtils.GetChoice() == 4 then -- dungeon
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_dialogue4_4"))
		elseif luaCutscenesUtils.GetChoice() == 5 then -- underworld
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_dialogue4_5"))
			-- there's no dialogue4_6 because it was "more options" and it was deleted
		elseif luaCutscenesUtils.GetChoice() == 6 then -- beach
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_dialogue4_7"))
		elseif luaCutscenesUtils.GetChoice() == 7 then -- forest
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_dialogue4_8"))
		elseif luaCutscenesUtils.GetChoice() == 8 then -- space
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_dialogue4_9"))
		elseif luaCutscenesUtils.GetChoice() == 9 then -- caverns
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_dialogue4_10"))
		elseif luaCutscenesUtils.GetChoice() == 10 then -- jungle
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_dialogue4_11"))
		elseif luaCutscenesUtils.GetChoice() == 11 then -- home
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_dialogue4_12"))
		end
	elseif luaCutscenesUtils.GetChoice() == 4 then -- who lives here?
		if dogBack and not talkedWithDryad then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_dialogue_5_1_dog"))
		elseif dogBack and talkedWithDryad then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_dialogue_5_1_dryaddog"))
		elseif talkedWithDryad and not dogBack then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_dialogue_5_1_dryad"))
		else
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_dialogue_5_1"))
		end
	elseif luaCutscenesUtils.GetChoice() == 2 then -- Random tip
		randomTip = math.random(0,27)
		if randomTip == 0 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_0"))
		elseif randomTip == 1 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_1"))
		elseif randomTip == 2 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_2"))
		elseif randomTip == 3 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_3"))
		elseif randomTip == 4 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_4"))
		elseif randomTip == 5 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_5"))
		elseif randomTip == 6 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_6"))
		elseif randomTip == 7 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_7"))
		elseif randomTip == 8 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_8"))
		elseif randomTip == 9 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_9"))
		elseif randomTip == 10 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_10"))
		elseif randomTip == 11 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_11"))
		elseif randomTip == 12 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_12"))
		elseif randomTip == 13 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_13"))
		elseif randomTip == 14 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_14"))
		elseif randomTip == 15 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_15"))
		elseif randomTip == 16 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_16"))
		elseif randomTip == 17 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_17"))
		elseif randomTip == 18 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_18"))
		elseif randomTip == 19 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_19"))
		elseif randomTip == 20 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_20"))
		elseif randomTip == 21 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_21"))
		elseif randomTip == 22 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_22"))
		elseif randomTip == 23 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_23"))
		elseif randomTip == 24 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_24"))
		elseif randomTip == 25 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_25"))
		elseif randomTip == 26 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_26"))
		elseif randomTip == 27 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_guide_chat1_27"))
		end
	end
	enableMovement()
	enableRetry()
	guide.CutsceneModeEnabled = false
end