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 X"
-- "LocalRoll Y"
-- "GlobalRoll X"
-- "GlobalRoll Y"
-- "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.
-- Seventh parameter is the sensitivity for the axis

local XBoxController = {
  LeftThumbStick = { 0 , 1 },
  RightThumbStick = { 2, 3 },
  LeftTrigger = 4,
  RightTrigger = 5,
  A = 0,
  B = 1,
  X = 2,
  Y = 3,
  LB = 4,
  RB = 5,
  Select = 6,
  Start = 7,
  LeftStickButton = 8,
  RightStickButton = 9,
  DPad = {
    Up = 10,
    Right = 11,
    Down = 12,
    Left = 13
  }
}

asset.onInitialize(function()
  local controller = XBoxController;
  local name = "Wireless Xbox Controller";

  local deadzoneJoysticks = 0.15
  local deadzoneTriggers = 0.05
  openspace.navigation.setAxisDeadZone(name, controller.LeftThumbStick[1], deadzoneJoysticks)
  openspace.navigation.setAxisDeadZone(name, controller.LeftThumbStick[2], deadzoneJoysticks)
  openspace.navigation.setAxisDeadZone(name, controller.RightThumbStick[1], deadzoneJoysticks)
  openspace.navigation.setAxisDeadZone(name, controller.RightThumbStick[2], deadzoneJoysticks)
  openspace.navigation.setAxisDeadZone(name, controller.LeftTrigger, deadzoneTriggers)
  openspace.navigation.setAxisDeadZone(name, controller.RightTrigger, deadzoneTriggers)

  openspace.navigation.bindJoystickAxis(name, controller.LeftThumbStick[1], "Orbit X");
  openspace.navigation.bindJoystickAxis(name, controller.LeftThumbStick[2], "Orbit Y", true);
  openspace.navigation.bindJoystickAxis(name, controller.RightThumbStick[1], "Pan X", true);
  openspace.navigation.bindJoystickAxis(name, controller.RightThumbStick[2], "Pan Y", true);
  openspace.navigation.bindJoystickAxis(name, controller.LeftTrigger, "Zoom Out", false, "TriggerLike");
  openspace.navigation.bindJoystickAxis(name, controller.RightTrigger, "Zoom In", false, "TriggerLike");

  openspace.navigation.bindJoystickButton(
    name,
    controller.LB,
    joystickHelper.bindLocalRoll(name, controller.RightThumbStick[1]),
    "Switch to local roll mode"
  )
  openspace.navigation.bindJoystickButton(
    name,
    controller.LB,
    joystickHelper.unbindRoll(name, controller.RightThumbStick[1]),
    "Switch back to normal mode",
    "Release"
  )
  openspace.navigation.bindJoystickButton(
    name,
    controller.RB,
    joystickHelper.bindGlobalRoll(name, controller.RightThumbStick[1]),
    "Switch to global roll mode"
  )
  openspace.navigation.bindJoystickButton(
    name,
    controller.RB,
    joystickHelper.unbindRoll(name, controller.RightThumbStick[1]),
    "Switch back to normal mode",
    "Release"
  )

  openspace.navigation.bindJoystickButton(
    name,
    controller.A,
    propertyHelper.invert("NavigationHandler.OrbitalNavigator.Friction.ZoomFriction"),
    "Toggle zoom friction"
  )
  openspace.navigation.bindJoystickButton(
    name,
    controller.B,
    propertyHelper.invert("NavigationHandler.OrbitalNavigator.Friction.RotationalFriction"),
    "Toggle rotational friction"
  )
  openspace.navigation.bindJoystickButton(
    name,
    controller.DPad.Left,
    propertyHelper.invert("NavigationHandler.OrbitalNavigator.Friction.RollFriction"),
    "Toggle roll friction"
  )

  openspace.navigation.bindJoystickButton(
    name,
    controller.X,
    [[
      openspace.setPropertyValueSingle("NavigationHandler.OrbitalNavigator.Aim", "");
      openspace.setPropertyValueSingle("NavigationHandler.OrbitalNavigator.Anchor", "Earth");
      openspace.setPropertyValueSingle("NavigationHandler.OrbitalNavigator.RetargetAnchor", nil);
    ]],
    "Switch target to Earth"
  )
  openspace.navigation.bindJoystickButton(
    name,
    controller.Y,
    [[
      openspace.setPropertyValueSingle("NavigationHandler.OrbitalNavigator.Aim", "");
      openspace.setPropertyValueSingle("NavigationHandler.OrbitalNavigator.Anchor", "Mars");
      openspace.setPropertyValueSingle("NavigationHandler.OrbitalNavigator.RetargetAnchor", nil);
    ]],
    "Switch target to Mars"
  )
end)
