local propertyHelper = asset.require("../property_helper")
local joystickHelper = asset.require("./joystick_helper")



-- Allowed values for the third parameter of bindJoystickAxis:
-- "None"
-- "Orbit X"
-- "Orbit Y"
-- "Zoom" -- both in and out
-- "Zoom In"
-- "Zoom Out"
-- "LocalRoll"
-- "GlobalRoll"
-- "Pan X"
-- "Pan Y"
-- Fourth parameter determines whether the axis should be inverted
-- Fifth parameter determines whether the axis behaves like a joystick or a Trigger.
--       Allowed values are "JoystickLike" and "TriggerLike", the first one is the default
-- Sixth parameters determins if the axis should be "Sticky" or not.
--       The axis values can either go back to 0 when the joystick is released or it can
--       stay at the value it was before the joystick was released.
--       The latter is called a sticky axis, when the values don't go back to 0.
--       This version of the SpaceMouse IS Sticky.
-- Seventh parameter can be used to reverse the camera movement for the axis
-- Eighth parameter is the sensitivity for the axis

local SpaceMouse = {
  -- Axes
  Push = {
    LeftRight = 0,
    BackForth = 1,
    UpDown = 2
  },
  Twist = 5,
  Tilt = {
    LeftRight = 4,
    BackForth = 3
  },

  -- Buttons
  LeftButton = 0,
  RightButton = 1
}


asset.onInitialize(function()
  local controller = SpaceMouse
  local name = "SpaceNavigator"

  openspace.navigation.bindJoystickAxis(name, controller.Push.LeftRight, "Orbit X", false, "JoystickLike", true, false, 40.0)
  openspace.navigation.bindJoystickAxis(name, controller.Push.BackForth, "Orbit Y", false, "JoystickLike", true, false, 40.0)
  openspace.navigation.bindJoystickAxis(name, controller.Twist, "Pan X", true, "JoystickLike", true, false, 40.0)
  openspace.navigation.bindJoystickAxis(name, controller.Tilt.BackForth, "Pan Y", false, "JoystickLike", true, false, 35.0)
  openspace.navigation.bindJoystickAxis(name, controller.Push.UpDown, "Zoom", false, "JoystickLike", true, false, 40.0)
  openspace.navigation.bindJoystickAxis(name, controller.Tilt.LeftRight, "LocalRoll", false, "JoystickLike", true, false, 35.0)

  openspace.navigation.bindJoystickButton(
    name,
    controller.LeftButton,
    joystickHelper.permaBindLocalRoll(name, controller.Tilt.LeftRight),
    "Switch to local roll mode"
  )

  openspace.navigation.bindJoystickButton(
    name,
    controller.RightButton,
    joystickHelper.permaBindGlobalRoll(name, controller.Tilt.LeftRight),
    "Switch to global roll mode"
  )
end)
