local monocle = require("#monocle")
local coroutineEntity = monocle.Entity()
local celeste = require("#celeste")

local badeline
local Akko
local AkkoWalk
local AkkoWalkR
local Lotte
local LotteL
local broomempty

function onBegin()
	if not getFlag("introWatched") then
		local level = getLevel()
		local luanet = _G.luanet
	Akko = celeste.Mod.MaxHelpingHand.Entities.MoreCustomNPC.GetNPC("Akko")
	AkkoWalk = celeste.Mod.MaxHelpingHand.Entities.MoreCustomNPC.GetNPC("AkkoWalk")
	AkkoWalkR = celeste.Mod.MaxHelpingHand.Entities.MoreCustomNPC.GetNPC("AkkoWalkR")
	Lotte = celeste.Mod.MaxHelpingHand.Entities.MoreCustomNPC.GetNPC("Lotte")
	LotteL = celeste.Mod.MaxHelpingHand.Entities.MoreCustomNPC.GetNPC("LotteL")
	broomempty = celeste.Mod.MaxHelpingHand.Entities.MoreCustomNPC.GetNPC("broomempty")


		disableMovement()
		helpers.walkTo(600,false,1,true)
		wait(0.2)
		getLevel():add(coroutineEntity)
		coroutine.yield(getLevel():ZoomTo(vector2(180, 85), 1.5, 0.5))
		level.Displacement:AddBurst(player.Position, 0.5, 8, 32, 0.5)
		coroutine.yield(player:CreateSplitParticles())
		badeline = celeste.BadelineDummy(player.Position + vector2(-2, -8))
		badeline.Sprite.Scale = vector2(1, 1)
		level:Add(badeline)
		playSound("event:/char/badeline/maddy_split", player.Position)
		coroutine.yield(badeline:FloatTo(player.Position + vector2(-20, -6), 1, false, false, true))
		wait(0.5)
		helpers.say("ROOM2_DIALOGStart")		

		LotteL.Position = vector2(Lotte.Position.X, Lotte.Position.Y)
		Lotte.Position = vector2(Lotte.X, Lotte.Y + 300)

		AkkoWalk.Position = vector2(Akko.Position.X, Akko.Position.Y)
		Akko.Position = vector2(Akko.X, Akko.Y + 300)
		coroutineEntity:add(monocle.Coroutine(AkkoWalk:MoveTo(vector2(AkkoWalk.Position.X - 40, AkkoWalk.Y))))

		wait(0.8)
		Akko.Position = vector2(AkkoWalk.Position.X, AkkoWalk.Position.Y)
		AkkoWalk.Position = vector2(AkkoWalk.X, AkkoWalk.Y + 300)

		disableMovement()
		helpers.say("ROOM2_DIALOGAkko1")
		player.DummyAutoAnimate = false
		player.Sprite:Play("lookUp")
		
		coroutine.yield(getLevel():ZoomBack(0.3))
		wait(0.3)

		coroutine.yield(celeste.CutsceneEntity.CameraTo(vector2(getLevel().Camera.Position.X + 80, getLevel().Camera.Position.Y - 500), 1.5))		
		helpers.say("ROOM2_DIALOG2")
		player.DummyAutoAnimate = true

		coroutine.yield(celeste.CutsceneEntity.CameraTo(vector2(getLevel().Camera.Position.X - 60, getLevel().Camera.Position.Y + 500), 0.5))
		helpers.say("ROOM2_DIALOG3")

		AkkoWalkR.Position = vector2(Akko.Position.X, Akko.Position.Y)
		Akko.Position = vector2(Akko.X, Akko.Y + 300)
		coroutineEntity:add(monocle.Coroutine(AkkoWalkR:MoveTo(vector2(AkkoWalkR.Position.X + 40, AkkoWalkR.Y))))
		wait(0.7)
		Akko.Position = vector2(AkkoWalkR.Position.X, AkkoWalkR.Position.Y)
		AkkoWalkR.Position = vector2(AkkoWalkR.X - 1000, AkkoWalkR.Y + 300)
		
		helpers.say("ROOM2_DIALOG_END")
		coroutine.yield(badeline:Vanish())
		coroutine.yield(getLevel():ZoomBack(0.3))
		Lotte.Position = vector2(LotteL.Position.X, LotteL.Position.Y)
		LotteL.Position = vector2(LotteL.X, LotteL.Y + 300)
		enableMovement()
		setFlag("introWatched", true)
	end
end

--- Callback for when the cutscene ends.
-- Function, no yielding actions allowed.
-- That means no walking, waiting etc.
-- Only "clean up" actions.
-- @tparam #Celeste.Level room Current room.
-- @bool wasSkipped If the cutscene was skipped.
function onEnd(room, wasSkipped)
    if wasSkipped then
	Akko = celeste.Mod.MaxHelpingHand.Entities.MoreCustomNPC.GetNPC("Akko")
	AkkoWalk = celeste.Mod.MaxHelpingHand.Entities.MoreCustomNPC.GetNPC("AkkoWalk")
	AkkoWalkR = celeste.Mod.MaxHelpingHand.Entities.MoreCustomNPC.GetNPC("AkkoWalkR")
	Lotte = celeste.Mod.MaxHelpingHand.Entities.MoreCustomNPC.GetNPC("Lotte")
	LotteL = celeste.Mod.MaxHelpingHand.Entities.MoreCustomNPC.GetNPC("LotteL")
	broomempty = celeste.Mod.MaxHelpingHand.Entities.MoreCustomNPC.GetNPC("broomempty")
    	enableMovement()
	player.DummyAutoAnimate = true
    	setFlag("introWatched", true)
	if badeline then
            badeline:RemoveSelf()
        end
	AkkoWalk.Position = vector2(AkkoWalk.Position.X, AkkoWalk.Position.Y + 1000)
	AkkoWalkR.Position = vector2(AkkoWalkR.Position.X, AkkoWalkR.Position.Y + 1000)
	Akko.Position = vector2(broomempty.X, broomempty.Y - 16)
	Lotte.Position = vector2(broomempty.X - 16, broomempty.Y - 15)
	LotteL.Position = vector2(LotteL.X, LotteL.Y + 300)
	getLevel():ZoomBack(0.3)
        getLevel():remove(coroutineEntity)
    end
end