local luaCutscenesUtils = require("#Celeste.Mod.MaxHelpingHand.Module.LuaCutscenesUtils")
local luaCutscenesUtils2 = require("#ExtendedVariants.Module.LuaCutscenesUtils")
local bettertext = require("#Celeste.Mod.CrossoverCollab.Code.PointsStuff.BetterTextbox")
local save = require("#Celeste.Mod.CrossoverCollab.Code.PointsStuff.PointsModule")
local greeting = 0
local newEggsBroken = 0
local missingEggs = 15

-- Adds a fish with any name, if that fish already exists, adds that many of it
function AddFish(name, amount)
    save.SaveData:AddFish(name, tonumber(amount))
 end
-- Returns how many fish of that type you have (or coins)
function GetFishamount(name)
    return tonumber(save.SaveData:GetFishAmount(name))
end

function bettersay(dialog)
    coroutine.yield(bettertext.Say(tostring(dialog)))
end

function onTalk()
	disableMovement()
	makeUnskippable()
	disableRetry()
	math.randomseed(os.time())
	playSound("event:/CC/CC_KoseiDiamond/ui_pause")
	greeting = math.random(0,2) -- says hi
	if getFlag("cryoGoldenRun") then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurse_golden"))
	else
		if getFlag("PartyCenter") then
				coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurse_party"))
			else
			if getFlag("weatherSandstorm") then
			if greeting == 0 then
				coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurse_greetingD"))
			elseif greeting == 1 then
				coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurse_greetingE"))
			elseif greeting == 2 then
				coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurse_greetingF"))
			end
			elseif greeting == 0 then
				coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurse_greeting"))
			elseif greeting == 1 then
				coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurse_greetingB"))
			elseif greeting == 2 then
				coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurse_greetingC"))
			end
		end
	end
	
	coroutine.yield(luaCutscenesUtils.ChoiceWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurseYes", "CC_KoseiDiamond_nurseNo", "CC_KoseiDiamond_nurseAsk", "CC_KoseiDiamond_nurseQuestAsk"))
	if luaCutscenesUtils.GetChoice() == 1 then -- Remove all permabuffs
		setFlag("healing", true)
		playSound("event:/CC/CC_KoseiDiamond_sounds/LifeHeart")
		wait(0.25)
		setFlag("fishingState", false)
		setFlag("Sponge", false)
		setFlag("Clairvoyance", false)
		setFlag("UltrabrightHelmet", false)
		luaCutscenesUtils2.TriggerBooleanVariant("DisableMadelineSpotlight", true, false)
		setFlag("FrogLegs", false)
		luaCutscenesUtils2.TriggerFloatVariant("JumpDuration", 1, false)
		luaCutscenesUtils2.TriggerFloatVariant("JumpHeight", 1, false)
		setFlag("RocketBoots", false)
		luaCutscenesUtils2.TriggerFloatVariant("SpeedX", 1, false)
		luaCutscenesUtils2.TriggerBooleanVariant("EveryJumpIsUltra", false, false)
		setFlag("BandOfRegeneration", false)
		luaCutscenesUtils2.TriggerBooleanVariant("DisableDashCooldown", false, false)
		luaCutscenesUtils2.TriggerBooleanVariant("NoFreezeFrames", false, false)
		setFlag("CoyoteMask", false)
		luaCutscenesUtils2.TriggerFloatVariant("CoyoteTime", 1, false)
		setFlag("PeaceCandle", false)
		setFlag("catTalkedE", false) -- Dynamite Kitten
		setFlag("Midas", false)
		setFlag("Camera", false)
		setFlag("healing", false)
		setFlag("FallThrough", false)
		wait(0.05)
	elseif luaCutscenesUtils.GetChoice() == 3 then -- Permabuff info
		coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurse_checkingpermabuffs"))
		if getFlag("BandOfRegeneration") then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurse_BandOfRegeneration"))
		end
		if getFlag("CoyoteMask") then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurse_CoyoteMask"))
		end
		if getFlag("PeaceCandle") then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurse_PeaceCandle"))
		end
		if getFlag("catTalkedE") then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurse_DynamiteKitten"))
		end
		if getFlag("Midas") then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurse_Midas"))
		end
		if getFlag("Camera") then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurse_Camera"))
		end
		if getFlag("VitalCrystal") then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurse_VitalCrystal"))
		end
		if getFlag("Sponge") then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurse_Sponge"))
		end
		if getFlag("RocketBoots") then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurse_RocketBoots"))
		end
		if getFlag("FrogLegs") then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurse_FrogLegs"))
		end
		if getFlag("UltrabrightHelmet") then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurse_UltrabrightHelmet"))
		end
		if getFlag("Clairvoyance") then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurse_Clairvoyance"))
		end
		if getFlag("fishingState") then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurse_fishingState"))
		end
		if getFlag("FallThrough") then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurse_FallThrough"))
		end
		if getFlag("GravitationPotion") then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurse_GravitationPotion"))
		end
		if getFlag("PermanentShadowDash") then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurse_ShadowCrystal"))
		end
		if (getFlag("fishingState") or getFlag("Sponge") or getFlag("Clairvoyance") or getFlag("UltrabrightHelmet") or getFlag("FrogLegs") or getFlag("RocketBoots") or
			getFlag("BandOfRegeneration") or getFlag("CoyoteMask") or getFlag("PeaceCandle") or getFlag("catTalkedE") or getFlag("Midas") or getFlag("VitalCrystal")
			or getFlag("FallThrough") or getFlag("Camera") or getFlag("PermanentShadowDash")) == false then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurse_nopermabuffs"))
		end
	elseif luaCutscenesUtils.GetChoice() == 4 then -- Quest
		countNewEggs()
		if not getFlag("antQuestIntro") then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurseQuestIntro"))
			setFlag("antQuestIntro",true)
		elseif newEggsBroken == 0 then
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurseQuestNoEggs"))
			bettersay("You are still missing {F94A4A}" ..tostring(missingEggs).. "{#} ant eggs.")
		else
			coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("ANDYB", "CC_KoseiDiamond_nurseQuestPartial"))
			playSound("event:/CC/CC_KoseiDiamond_sounds/terracoin")
			if getFlag("Midas") then
				AddFish("terracoins", newEggsBroken * 40)
			else
				AddFish("terracoins", newEggsBroken * 20)
			end
			bettersay("You received {#F94A4A}" ..tostring(newEggsBroken * 20).. "{#} terracoins.{n}You have {#F94A4A}"..tostring(GetFishamount("terracoins")).. "{#} terracoins now.")
			bettersay("You are still missing {F94A4A}" ..tostring(missingEggs).. "{#} ant eggs.")
		end
	end
	enableMovement()
	enableRetry()
end

function countNewEggs()
	if getFlag("ant00") then
		if not getFlag("ant00Paid") and (getFlag("antQuestIntro")) then
			newEggsBroken = newEggsBroken + 1
			setFlag("ant00Paid",true)
		end
		missingEggs = missingEggs -1
	end
	if getFlag("ant01") then
		if (not getFlag("ant01Paid")) and (getFlag("antQuestIntro")) then
			newEggsBroken = newEggsBroken + 1
			setFlag("ant01Paid",true)
		end
		missingEggs = missingEggs -1
	end
	if getFlag("ant02") then
		if (not getFlag("ant02Paid")) and (getFlag("antQuestIntro")) then
			newEggsBroken = newEggsBroken + 1
			setFlag("ant02Paid",true)
		end
		missingEggs = missingEggs -1
	end
	if getFlag("ant03") then
		if (not getFlag("ant03Paid")) and (getFlag("antQuestIntro")) then
			newEggsBroken = newEggsBroken + 1
			setFlag("ant03Paid",true)
		end
		missingEggs = missingEggs -1
	end
	if getFlag("ant04") then
		if (not getFlag("ant04Paid")) and (getFlag("antQuestIntro")) then
			newEggsBroken = newEggsBroken + 1
			setFlag("ant04Paid",true)
		end
		missingEggs = missingEggs -1
	end
	if getFlag("ant05") then
		if (not getFlag("ant05Paid")) and (getFlag("antQuestIntro")) then
			newEggsBroken = newEggsBroken + 1
			setFlag("ant05Paid",true)
		end
		missingEggs = missingEggs -1
	end
	if getFlag("ant06") then
		if (not getFlag("ant06Paid")) and (getFlag("antQuestIntro")) then
			newEggsBroken = newEggsBroken + 1
			setFlag("ant06Paid",true)
		end
		missingEggs = missingEggs -1
	end
	if getFlag("ant07") then
		if (not getFlag("ant07Paid")) and (getFlag("antQuestIntro")) then
			newEggsBroken = newEggsBroken + 1
			setFlag("ant07Paid",true)
		end
		missingEggs = missingEggs -1
	end
	if getFlag("ant08") then
		if (not getFlag("ant08Paid")) and (getFlag("antQuestIntro")) then
			newEggsBroken = newEggsBroken + 1
			setFlag("ant08Paid",true)
		end
		missingEggs = missingEggs -1
	end
	if getFlag("ant09") then
		if (not getFlag("ant09Paid")) and (getFlag("antQuestIntro")) then
			newEggsBroken = newEggsBroken + 1
			setFlag("ant09Paid",true)
		end
		missingEggs = missingEggs -1
	end
	if getFlag("ant10") then
		if (not getFlag("ant10Paid")) and (getFlag("antQuestIntro")) then
			newEggsBroken = newEggsBroken + 1
			setFlag("ant10Paid",true)
		end
		missingEggs = missingEggs -1
	end
	if getFlag("ant11") then
		if (not getFlag("ant11Paid")) and (getFlag("antQuestIntro")) then
			newEggsBroken = newEggsBroken + 1
			setFlag("ant11Paid",true)
		end
		missingEggs = missingEggs -1
	end
	if getFlag("ant12") then
		if (not getFlag("ant12Paid")) and (getFlag("antQuestIntro")) then
			newEggsBroken = newEggsBroken + 1
			setFlag("ant12Paid",true)
		end
		missingEggs = missingEggs -1
	end
	if getFlag("ant13") then
		if (not getFlag("ant13Paid")) and (getFlag("antQuestIntro")) then
			newEggsBroken = newEggsBroken + 1
			setFlag("ant13Paid",true)
		end
		missingEggs = missingEggs -1
	end
	if getFlag("ant14") then
		if (not getFlag("ant14Paid")) and (getFlag("antQuestIntro")) then
			newEggsBroken = newEggsBroken + 1
			setFlag("ant14Paid",true)
		end
		missingEggs = missingEggs -1
	end
	if getFlag("ant00") and getFlag("ant01") and getFlag("ant02") and getFlag("ant03") and getFlag("ant04") and getFlag("ant05") and getFlag("ant06") and getFlag("ant07")
	and getFlag("ant08") and getFlag("ant09") and getFlag("ant10") and getFlag("ant11") and getFlag("ant12") and getFlag("ant13") and getFlag("ant14") then
		setFlag("antQuestDone",true)
	end

end