local lua_helper = celeste.Mod.LuaCutscenes.LuaHelper
local monocle = require("#monocle")

local function makeCoroutine(func)
    return monocle.Coroutine(lua_helper.LuaCoroutineToIEnumerator(coroutine.create(func)))
end

local function approachNPC()
    player.ForceCameraUpdate = true
    walkTo(12404)
    player.Facing = getEnum("Celeste.Facings", "Right")
    player.Speed = vector2(0, 0)
end

local approachNPCCoroutine

function onTalk()
    disableMovement()
    approachNPCCoroutine = makeCoroutine(approachNPC)
    talker:Add(approachNPCCoroutine)
    if not getFlag("spoken_to_buffy") then
        setFlag("spoken_to_buffy", true)
        say("Spooooky_SlipperiestNook_BuffyWelcome")
        setFlag("PlayingClassic", true)
    else
        if getFlag("FunkyUnlocked") and getFlag("JazzyUnlocked") and getFlag("ChillUnlocked") and not getFlag("Buffy_Berry") then
            say("Spooooky_SlipperiestNook_BuffyBerry")
            setFlag("Buffy_Berry", true)
        else
            if getFlag("FunkyUnlocked") or getFlag("JazzyUnlocked") or getFlag("ChillUnlocked") then
                say("Spooooky_SlipperiestNook_BuffyChoice")
                choiceDialog({
                    {
                        "Spooooky_SlipperiestNook_BuffyClassic",
                        requires = function ()
                            return not getFlag("PlayingClassic")
                        end, 
                        onEnd = function ()
                        setFlag("PlayingClassic", true)
                        setFlag("PlayingFunky", false)
                        setFlag("PlayingJazzy", false)
                        setFlag("PlayingChill", false)
                        playMusic("event:/Spooooky_SlipperiestNook")
                        closeChoiceDialog()
                        end
                    },
                    {
                        "Spooooky_SlipperiestNook_BuffyFunky",
                        requires = function ()
                            return getFlag("FunkyUnlocked") and not getFlag("PlayingFunky")
                        end, 
                        onEnd = function ()
                        setFlag("PlayingClassic", false)
                        setFlag("PlayingFunky", true)
                        setFlag("PlayingJazzy", false)
                        setFlag("PlayingChill", false)
                        playMusic("event:/Spooooky_SlipperiestNook_Funky")
                        closeChoiceDialog()
                        end
                    },
                    {
                        "Spooooky_SlipperiestNook_BuffyJazzy",
                        requires = function ()
                            return getFlag("JazzyUnlocked") and not getFlag("PlayingJazzy")
                        end, 
                        onEnd = function ()
                        setFlag("PlayingClassic", false)
                        setFlag("PlayingFunky", false)
                        setFlag("PlayingJazzy", true)
                        setFlag("PlayingChill", false)
                        playMusic("event:/Spooooky_SlipperiestNook_Jazzy")
                        closeChoiceDialog()
                        end
                    },
                    {
                        "Spooooky_SlipperiestNook_BuffyChill",
                        requires = function ()
                            return getFlag("ChillUnlocked") and not getFlag("PlayingChill")    
                        end, 
                        onEnd = function ()
                        setFlag("PlayingClassic", false)
                        setFlag("PlayingFunky", false)
                        setFlag("PlayingJazzy", false)
                        setFlag("PlayingChill", true)
                        playMusic("event:/Spooooky_SlipperiestNook_Chill")
                        closeChoiceDialog()
                        end
                    },
                    {
                        "Spooooky_SlipperiestNook_BuffyClose",
                        onEnd = function ()
                        closeChoiceDialog()
                        end
                    },
                })
            else
                say("Spooooky_SlipperiestNook_BuffyNoTunes")
            end
        end
    end
    enableMovement()
end

function onEnd(room, wasSkipped)
    if approachNPCCoroutine then
        approachNPCCoroutine:Cancel()
        talker:Remove(approachNPCCoroutine)
    end
    enableMovement()
end