local minput = require("#Monocle.MInput")

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

local function dashDown()
  player.ForceCameraUpdate = true
  celeste.Input.Rumble(celeste.RumbleStrength.Strong, celeste.RumbleLength.Medium)
  minput.Disabled = true
  player.OverrideDashDirection = vector2(0, 2)
  player.StateMachine.Locked = false
  player.StateMachine.State = player:StartDash()
  player.Dashes = 0
  wait(0.1)
    while (player.Speed.x < 0) do
      player.Dashes = 0
      celeste.Input.MoveY.Value = 1
      celeste.Input.MoveX.Value = 0
      wait()
    end
  player.OverrideDashDirection = nil
  player.StateMachine.State = 11
  minput.Disabled = false
  player.ForceCameraUpdate = true
end


function onTalk()

  if getFlag("catdead") then

    disablePause()
    disableMovement()
    player.ForceCameraUpdate = true

    coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("OMORI_GAME", "CBR_BlackSpace_door1_room6_dialog8"))
    wait()
    enableMovement()
    enablePause()

  end

  if getFlag("hit6") then

    disablePause()
    disableMovement()
    player.ForceCameraUpdate = true

    coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("OMORI_GAME", "CBR_BlackSpace_door1_room6_dialog7"))

    coroutine.yield(luaCutscenesUtils.ChoiceWithDifferentFont("OMORI_GAME", "CBR_BlackSpace_door1_room6_choice1", "CBR_BlackSpace_door1_room6_choice2"))
    local multipla = luaCutscenesUtils.GetChoice()

    if multipla == 1 then

      disableMovement()
      wait(0.5)

      jump()
      player.Speed = vector2(0, -250)
      run(17)
      wait(0.05)

      dashDown()
      waitUntilOnGround()
      celeste.Mod.MaxHelpingHand.Entities.MoreCustomNPC.GetNPC("catdissecation_blackspace"):PlayAnimation("dead")
      playSound("event:/char/badeline/jump_special", player.Position)
      setFlag("catblood", true)
      jump(0.05)
      player.Speed = vector2(-350, -10)
      wait(0.5)

      setFlag("catblood", false)
      setFlag("hit6", false)
      setFlag("catdead", true)
      setFlag("istraique1", false)
      setFlag("istraique2", false)
      setFlag("istraique3", false)
      setFlag("istraicado", false)
      enableMovement()
      enablePause()

    else

      walk(-20)
      enableMovement()
      enablePause()

    end

  end

  if getFlag("hit5") then

    disablePause()
    disableMovement()
    player.ForceCameraUpdate = true

    coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("OMORI_GAME", "CBR_BlackSpace_door1_room6_dialog6"))

    coroutine.yield(luaCutscenesUtils.ChoiceWithDifferentFont("OMORI_GAME", "CBR_BlackSpace_door1_room6_choice1", "CBR_BlackSpace_door1_room6_choice2"))
    local multipla = luaCutscenesUtils.GetChoice()

    if multipla == 1 then

      disableMovement()
      wait(0.5)

      jump()
      player.Speed = vector2(0, -250)
      run(17)
      wait(0.05)

      dashDown()
      waitUntilOnGround()
      celeste.Mod.MaxHelpingHand.Entities.MoreCustomNPC.GetNPC("catdissecation_blackspace"):PlayAnimation("hit")
      playSound("event:/char/badeline/jump_special", player.Position)
      setFlag("catblood", true)
      jump(0.05)
      player.Speed = vector2(-350, -10)
      wait(0.5)

      setFlag("catblood", false)
      setFlag("hit5", false)
      setFlag("hit6", true)
      enableMovement()
      enablePause()

    else

      walk(-20)
      enableMovement()
      enablePause()

    end

  end

  if getFlag("hit4") then

    disablePause()
    disableMovement()
    player.ForceCameraUpdate = true

    coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("OMORI_GAME", "CBR_BlackSpace_door1_room6_dialog5"))

    coroutine.yield(luaCutscenesUtils.ChoiceWithDifferentFont("OMORI_GAME", "CBR_BlackSpace_door1_room6_choice1", "CBR_BlackSpace_door1_room6_choice2"))
    local multipla = luaCutscenesUtils.GetChoice()

    if multipla == 1 then

      disableMovement()
      wait(0.5)

      jump()
      player.Speed = vector2(0, -250)
      run(17)
      wait(0.05)

      dashDown()
      waitUntilOnGround()
      celeste.Mod.MaxHelpingHand.Entities.MoreCustomNPC.GetNPC("catdissecation_blackspace"):PlayAnimation("hit")
      playSound("event:/char/badeline/jump_special", player.Position)
      setFlag("catblood", true)
      jump(0.05)
      player.Speed = vector2(-350, -10)
      wait(0.5)

      setFlag("catblood", false)
      setFlag("hit4", false)
      setFlag("hit5", true)
      enableMovement()
      enablePause()

    else

      walk(-20)
      enableMovement()
      enablePause()

    end

  end

  if getFlag("hit3") then

    disablePause()
    disableMovement()
    player.ForceCameraUpdate = true

    coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("OMORI_GAME", "CBR_BlackSpace_door1_room6_dialog4"))

    coroutine.yield(luaCutscenesUtils.ChoiceWithDifferentFont("OMORI_GAME", "CBR_BlackSpace_door1_room6_choice1", "CBR_BlackSpace_door1_room6_choice2"))
    local multipla = luaCutscenesUtils.GetChoice()

    if multipla == 1 then

      disableMovement()
      wait(0.5)

      jump()
      player.Speed = vector2(0, -250)
      run(17)
      wait(0.05)

      dashDown()
      waitUntilOnGround()
      celeste.Mod.MaxHelpingHand.Entities.MoreCustomNPC.GetNPC("catdissecation_blackspace"):PlayAnimation("hit")
      playSound("event:/char/badeline/jump_special", player.Position)
      setFlag("catblood", true)
      jump(0.05)
      player.Speed = vector2(-350, -10)
      wait(0.5)

      setFlag("catblood", false)
      setFlag("hit3", false)
      setFlag("hit4", true)
      enableMovement()
      enablePause()

    else

      walk(-20)
      enableMovement()
      enablePause()

    end

  end

  if getFlag("hit2") then

    disablePause()
    disableMovement()
    player.ForceCameraUpdate = true

    coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("OMORI_GAME", "CBR_BlackSpace_door1_room6_dialog3"))

    coroutine.yield(luaCutscenesUtils.ChoiceWithDifferentFont("OMORI_GAME", "CBR_BlackSpace_door1_room6_choice1", "CBR_BlackSpace_door1_room6_choice2"))
    local multipla = luaCutscenesUtils.GetChoice()

    if multipla == 1 then

      disableMovement()
      wait(0.5)

      jump()
      player.Speed = vector2(0, -250)
      run(17)
      wait(0.05)

      dashDown()
      waitUntilOnGround()
      celeste.Mod.MaxHelpingHand.Entities.MoreCustomNPC.GetNPC("catdissecation_blackspace"):PlayAnimation("hit")
      playSound("event:/char/badeline/jump_special", player.Position)
      setFlag("catblood", true)
      jump(0.05)
      player.Speed = vector2(-350, -10)
      wait(0.5)

      setFlag("catblood", false)
      setFlag("hit2", false)
      setFlag("hit3", true)

        if getFlag("istraique2") then
          setFlag("istraique3")
        end

        if getFlag("istraique1") then
          setFlag("istraique2")
        end

        setFlag("istraique1")

      enableMovement()
      enablePause()

    else

      walk(-20)

        if getFlag("istraique2") then
          setFlag("istraique3")
        end

        if getFlag("istraique1") then
          setFlag("istraique2")
        end

        setFlag("istraique1")

      enableMovement()
      enablePause()

    end

  end

  if getFlag("hit1") then

    disablePause()
    disableMovement()
    player.ForceCameraUpdate = true

    coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("OMORI_GAME", "CBR_BlackSpace_door1_room6_dialog2"))

    coroutine.yield(luaCutscenesUtils.ChoiceWithDifferentFont("OMORI_GAME", "CBR_BlackSpace_door1_room6_choice1", "CBR_BlackSpace_door1_room6_choice2"))
    local multipla = luaCutscenesUtils.GetChoice()

    if multipla == 1 then

      disableMovement()
      wait(0.5)

      jump()
      player.Speed = vector2(0, -250)
      run(17)
      wait(0.05)

      dashDown()
      waitUntilOnGround()
      celeste.Mod.MaxHelpingHand.Entities.MoreCustomNPC.GetNPC("catdissecation_blackspace"):PlayAnimation("hit")
      playSound("event:/char/badeline/jump_special", player.Position)
      setFlag("catblood", true)
      jump(0.05)
      player.Speed = vector2(-350, -10)
      wait(0.5)

      setFlag("catblood", false)
      setFlag("hit1", false)
      setFlag("hit2", true)

        if getFlag("istraique2") then
          setFlag("istraique3")
        end

        if getFlag("istraique1") then
          setFlag("istraique2")
        end

        setFlag("istraique1")

      enableMovement()
      enablePause()

    else

      walk(-20)

        if getFlag("istraique2") then
          setFlag("istraique3")
        end

        if getFlag("istraique1") then
          setFlag("istraique2")
        end

        setFlag("istraique1")

      enableMovement()
      enablePause()

    end

  end

  if getFlag("catinteract") then

    disablePause()
    disableMovement()
    player.ForceCameraUpdate = true

    coroutine.yield(luaCutscenesUtils.SayWithDifferentFont("OMORI_GAME", "CBR_BlackSpace_door1_room6_dialog1"))

    coroutine.yield(luaCutscenesUtils.ChoiceWithDifferentFont("OMORI_GAME", "CBR_BlackSpace_door1_room6_choice1", "CBR_BlackSpace_door1_room6_choice2"))
    local multipla = luaCutscenesUtils.GetChoice()

    if multipla == 1 then

      disableMovement()
      wait(0.5)

      jump()
      player.Speed = vector2(0, -250)
      run(17)
      wait(0.05)

      dashDown()
      waitUntilOnGround()
      celeste.Mod.MaxHelpingHand.Entities.MoreCustomNPC.GetNPC("catdissecation_blackspace"):PlayAnimation("hit")
      playSound("event:/char/badeline/jump_special", player.Position)
      setFlag("catblood", true)
      jump(0.05)
      player.Speed = vector2(-350, -10)
      wait(0.5)

      setFlag("catblood", false)
      setFlag("catinteract", false)
      setFlag("hit1", true)

        if getFlag("istraique2") then
          setFlag("istraique3")
        end

        if getFlag("istraique1") then
          setFlag("istraique2")
        end

        setFlag("istraique1")

      enableMovement()
      enablePause()

    else

      walk(-20)

        if getFlag("istraique2") then
          setFlag("istraique3")
        end

        if getFlag("istraique1") then
          setFlag("istraique2")
        end

        setFlag("istraique1")

      enableMovement()
      enablePause()

    end

  end

end