local luaCutscenesUtils = require("#Celeste.Mod.MaxHelpingHand.Module.LuaCutscenesUtils")

local Audio = require("#celeste.Audio")

function playSprite(sprite, duration)
  player.DummyAutoAnimate = false
  player.Sprite:Play(sprite, false, false)
  if (duration) then
    wait(duration)
    player.DummyAutoAnimate = true
  end
end

local function elevatormoving()
  player.ForceCameraUpdate = false
  local contagem = 0
  while contagem < 1 do
    coroutine.yield(celeste.CutsceneEntity.CameraTo(vector2(4168, 5936), 0, monocle.Ease.Linear, 0))
    wait()
    coroutine.yield(celeste.CutsceneEntity.CameraTo(vector2(4168, 5938), 0, monocle.Ease.Linear, 0))
    wait()
    contagem = contagem + 0.5
  end
end


function onBegin()

  if getFlag("elevatoractive") then

    disableMovement()
    coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("OMORI_GAME", "CBR_BlackSpace_door2_elevator_active"))

    coroutine.yield(luaCutscenesUtils.ChoiceWithDifferentFont("OMORI_GAME", "CBR_BlackSpace_door2_elevator_choose1", "CBR_BlackSpace_door2_elevator_choose2", "CBR_BlackSpace_door2_elevator_choose3", "CBR_BlackSpace_door2_elevator_choose4"))
    local multipla = luaCutscenesUtils.GetChoice()
    if multipla == 1 then

      wait(0.25)
      sfxbutton = Audio.Play("event:/game/WanderBDay/omori_elevatorbutton", player.Position)
      wait(0.5)

      sfxelevatoractive = Audio.Play("event:/env/amb/wanderbirthdaymap/blackspace_elevator", player.Position)
      wait(1.5)

      elevatormoving()
      setFlag("waittime")
      setFlag("useelevator", false)

    elseif multipla == 2 then

      wait(0.25)
      sfxbutton = Audio.Play("event:/game/WanderBDay/omori_elevatorbutton", player.Position)
      wait(0.5)

      sfxelevatoractive = Audio.Play("event:/env/amb/wanderbirthdaymap/blackspace_elevator", player.Position)
      wait(1.5)

      elevatormoving()
      setFlag("waittime")
      setFlag("useelevator", false)

    elseif multipla == 3 then

      wait(0.25)
      sfxbutton = Audio.Play("event:/game/WanderBDay/omori_elevatorbutton", player.Position)
      wait(0.5)

      sfxelevatoractive = Audio.Play("event:/env/amb/wanderbirthdaymap/blackspace_elevator", player.Position)
      wait(1.5)

      elevatormoving()
      setFlag("waittime")
      setFlag("useelevator", false)

    elseif multipla == 4 then

      wait(0.25)
      sfxbutton = Audio.Play("event:/game/WanderBDay/omori_elevatorbutton", player.Position)
      wait(0.5)

      sfxelevatoractive = Audio.Play("event:/env/amb/wanderbirthdaymap/blackspace_elevator", player.Position)
      wait(1.5)

      elevatormoving()
      setFlag("waittime")
      setFlag("useelevator", false)

    end

    enableMovement()


    if getFlag("waittime") then

      wait(15)

      disableMovement()
      waitUntilOnGround()
      wait(0.5)

      coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("OMORI_GAME", "CBR_BlackSpace_door2_elevator_basil2"))
      wait(2)

      if player.Facing == getEnum("Celeste.Facings", "Left") then
        playSprite("lookback", 3)
      elseif player.Facing == getEnum("Celeste.Facings", "Right") then
        playSprite("lookback", 3)
      end

      wait(2)

      Audio.Stop(sfxelevatoractive, false)
      sfxelevatording = Audio.Play("event:/game/WanderBDay/omori_elevatording", player.Position)
      wait(1)

      elevator = celeste.Mod.MaxHelpingHand.Entities.MoreCustomNPC.GetNPC("elevator")
      elevator:PlayAnimation("open")
      wait(2)

      setFlag("waittime", false)
      setFlag("elevatoractive", false)
      setFlag("finishelevator")
      enableMovement()

    end

  end

end