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

local toggle_native_ui = {
  Identifier = "os_default.toggle_native_ui",
  Name = "Show Native GUI",
  Command = propertyHelper.invert("Modules.ImGUI.Enabled"),
  Documentation = "Shows or hides the native UI",
  GuiPath = "/System/GUI",
  IsLocal = true,

  Key = "F1"
}

local toggle_shutdown = {
  Identifier = "os_default.toggle_shutdown",
  Name = "Toggle Shutdown",
  Command = "openspace.toggleShutdown()",
  Documentation = "Toggles the shutdown that will stop OpenSpace after a grace period. Press again to cancel the shutdown during this period",
  IsLocal = true,

  Key = "ESC"
}

local take_screenshot = {
  Identifier = "os_default.take_screenshot",
  Name = "Take Screenshot",
  Command = "openspace.takeScreenshot()",
  Documentation = "Saves the contents of the screen to a file in the ${SCREENSHOTS} directory",
  GuiPath = "/System/Rendering",
  IsLocal = true,

  Key = "F12"
}

local toggle_pause_interpolated = {
  Identifier = "os_default.toggle_pause_interpolated",
  Name = "Toggle Pause (Interpolated)",
  Command = "openspace.time.pauseToggleViaKeyboard()",
  Documentation = "Smoothly starts and stops the simulation time",
  GuiPath = "/Time/Simulation Speed",
  IsLocal = true,

  Key = "SPACE"
}

local toggle_pause_immediate = {
  Identifier = "os_default.toggle_pause_immediate",
  Name = "Toggle Pause (Immediate)",
  Command = "openspace.time.togglePause()",
  Documentation = "Immediately starts and stops the simulation time",
  GuiPath = "/Time/Simulation Speed",
  IsLocal = true,

  Key = "Shift+SPACE"
}

local toggle_rotation_friction = {
  Identifier = "os_default.toggle_rotation_friction",
  Name = "Toggle Rotation friction",
  Command = propertyHelper.invert("NavigationHandler.OrbitalNavigator.Friction.RotationalFriction"),
  Documentation = "Toggles the rotational friction of the camera. If it is disabled, the camera rotates around the focus object indefinitely",
  GuiPath = "/Navigation",
  IsLocal = false,

  Key = "f"
}

local toggle_zoom_friction = {
  Identifier = "os_default.toggle_zoom_friction",
  Name = "Toggle Zoom Friction",
  Command = propertyHelper.invert("NavigationHandler.OrbitalNavigator.Friction.ZoomFriction"),
  Documentation = "Toggles the zoom friction of the camera. If it is disabled, the camera rises up from or closes in towards the focus object indefinitely",
  GuiPath = "/Navigation",
  IsLocal = false,

  Key = "Shift+f"
}

local toggle_roll_friction = {
  Identifier = "os_default.toggle_roll_friction",
  Name = "Toggle Roll Friction",
  Command = propertyHelper.invert("NavigationHandler.OrbitalNavigator.Friction.RollFriction"),
  Documentation = "Toggles the roll friction of the camera. If it is disabled, the camera rolls around its own axis indefinitely",
  GuiPath = "/Navigation",
  IsLocal = false,
  
  Key = "Ctrl+f"
}

local fade_to_black = {
  Identifier = "os_default.fade_to_black",
  Name = "Fade to/from black",
  Command = [[
    if openspace.getPropertyValue("RenderEngine.BlackoutFactor") > 0.5 then
      openspace.setPropertyValueSingle("RenderEngine.BlackoutFactor", 0.0, 3)
    else
      openspace.setPropertyValueSingle("RenderEngine.BlackoutFactor", 1.0, 3)
    end
  ]],
  Documentation = "Toggles the fade to black within 3 seconds or shows the rendering after 3 seconds",
  GuiPath = "/Rendering",
  IsLocal = false,
  
  Key = "w"
}

local toggle_main_gui = {
  Identifier = "os_default.toggle_main_gui",
  Name = "Toggle main GUI",
  Command = propertyHelper.invert("Modules.CefWebGui.Visible"),
  Documentation = "Toggles the main GUI",
  GuiPath = "/System/GUI",
  IsLocal = true,
  
  Key = "Tab"
}

local toggle_overlays = {
  Identifier = "os_default.toggle_overlays",
  Name = "Toggle dashboard and overlays",
  Command =
[[local isEnabled = openspace.getPropertyValue("Dashboard.IsEnabled");
openspace.setPropertyValueSingle("Dashboard.IsEnabled", not isEnabled);
openspace.setPropertyValueSingle("RenderEngine.ShowLog", not isEnabled);
openspace.setPropertyValueSingle("RenderEngine.ShowVersion", not isEnabled);
openspace.setPropertyValueSingle("RenderEngine.ShowCamera", not isEnabled)]],
  Documentation = "Toggles the dashboard and overlays",
  GuiPath = "/System/GUI",
  IsLocal = true,
  
  Key = "Shift+Tab"
}

local toggle_master_rendering = {
  Identifier = "os_default.toggle_master_rendering",
  Name = "Toggle rendering on master",
  Command = propertyHelper.invert("RenderEngine.DisableMasterRendering"),
  Documentation = "Toggles the rendering on master",
  GuiPath = "/System/Rendering",
  IsLocal = true,
  
  Key = "Alt+R"
}

local next_delta_step_interpolate = {
  Identifier = "os_default.next_delta_step_interpolate",
  Name = "Next Delta Time Step (Interpolate)",
  Command = "openspace.time.interpolateNextDeltaTimeStep()",
  Documentation = "Smoothly interpolates the simulation speed to the next delta time step, if one exists",
  GuiPath = "/Time/Simulation Speed",
  IsLocal = true,
  
  Key = "Right"
}

local next_delta_step_immediate = {
  Identifier = "os_default.next_delta_step_immediate",
  Name = "Next Delta Time Step (Immediate)",
  Command = "openspace.time.setNextDeltaTimeStep()",
  Documentation = "Immediately set the simulation speed to the next delta time step, if one exists",
  GuiPath = "/Time/Simulation Speed",
  IsLocal = true,
  
  Key = "Shift+Right"
}

local previous_delta_step_interpolate = {
  Identifier = "os_default.previous_delta_step_interpolate",
  Name = "Previous Delta Time Step (Interpolate)",
  Command = "openspace.time.interpolatePreviousDeltaTimeStep()",
  Documentation = "Smoothly interpolates the simulation speed to the previous delta time step, if one exists",
  GuiPath = "/Time/Simulation Speed",
  IsLocal = true,
  
  Key = "Left"
}

local previous_delta_step_immediate = {
  Identifier = "os_default.previous_delta_step_immediate",
  Name = "Previous Delta Time Step (Immediate)",
  Command = "openspace.time.setPreviousDeltaTimeStep()",
  Documentation = "Immediately set the simulation speed to the previous delta time step, if one exists",
  GuiPath = "/Time/Simulation Speed",
  IsLocal = true,
  
  Key = "Shift+Left"
}

local toggle_planet_labels = {
  Identifier = "os_default.toggle_planet_labels",
  Name = "Toggle planet labels",
  Command = [[
    local list = openspace.getProperty("{solarsystem_labels}.Renderable.Enabled");
    for _,v in pairs(list) do openspace.setPropertyValueSingle(v, not openspace.getPropertyValue(v)) end
  ]],
  Documentation = "Turns on visibility for all solar system labels",
  GuiPath = "/Solar System",
  IsLocal = false,
  Key = "l"
}



local Actions = {
  toggle_native_ui,toggle_shutdown,take_screenshot,toggle_pause_interpolated,toggle_pause_immediate,
  toggle_rotation_friction,toggle_zoom_friction,toggle_roll_friction,fade_to_black,
  toggle_main_gui,toggle_overlays,toggle_master_rendering,next_delta_step_interpolate,
  next_delta_step_immediate,previous_delta_step_interpolate,previous_delta_step_immediate
}

asset.onInitialize(function()
  for _, action in ipairs(Actions) do
    openspace.action.registerAction(action)
    openspace.bindKey(action.Key, action.Identifier)
  end
  -- The take screenshot function is a bit special since we want to bind two keys to that action
  openspace.bindKey("PRINT_SCREEN", take_screenshot.Identifier)
end)

asset.onDeinitialize(function ()
  openspace.clearKey("PRINT_SCREEN")

  for _, action in ipairs(Actions) do
    openspace.action.removeAction(action)
    openspace.clearKey(action.Key)
  end
end)
