local ToggleTrail = {
  Identifier = "os.ToggleTrail",
  Name = "Toggle trail",
  Command = [[
    local node
    if is_declared("args") then
      node = args.Node
    else
      node = openspace.navigation.getNavigationState().Anchor
    end

    local trail
    if openspace.hasSceneGraphNode(node .. "Trail") then
      trail = node .. "Trail"
    elseif openspace.hasSceneGraphNode(node .. "_trail") then
      trail = node .. "_trail"
    else
      -- No trail found, so nothing more to do here
      return
    end

    local visibility
    if is_declared("args") then
      if args.Transition == "Approaching" then
        visibility = false
      elseif args.Transition == "Exiting" then
        visibility = true
      else
        return
      end
    else
      visibility = not openspace.getPropertyValue("Scene." .. trail .. ".Renderable.Enabled")
    end

    if visibility then
      openspace.setPropertyValueSingle("Scene." .. trail .. ".Renderable.Fade", 1.0, 1.0)
    else
      openspace.setPropertyValueSingle("Scene." .. trail .. ".Renderable.Fade", 0.0, 1.0)
    end
  ]],
  Documentation = [[Toggles the visibility of the associated trail of a scene graph node.
    This action takes optional arguments to 1) determine which trail to hide (as the
    'Node') and 2) the transition direction (as 'After' and 'Before')]],
  GuiPath = "/Trails",
  IsLocal = true
}

local HideTrail = {
  Identifier = "os.HideTrail",
  Name = "Hide trail",
  Command = [[
    local node
    if is_declared("args") then
      node = args.Node
    else
      node = openspace.navigation.getNavigationState().Anchor
    end

    if openspace.hasSceneGraphNode(node .. "Trail") then
      openspace.setPropertyValueSingle("Scene." .. node .. "Trail.Renderable.Fade", 0.0, 1.0)
    elseif openspace.hasSceneGraphNode(node .. "_trail") then
      openspace.setPropertyValueSingle("Scene." .. node .. "_trail.Renderable.Fade", 0.0, 1.0)
    end
  ]],
  Documentation = [[Hides the associated trail of a scene graph node. This action takes an
    optional argument to determine whose trail to hide. If no argument is provided, the
    current focus node is used instead]],
  GuiPath = "/Trails",
  IsLocal = true
}

local ShowTrail = {
  Identifier = "os.ShowTrail",
  Name = "Show trail",
  Command = [[
    local node
    if is_declared("args") then
      node = args.Node
    else
      node = openspace.navigation.getNavigationState().Anchor
    end

    if openspace.hasSceneGraphNode(node .. "Trail") then
      openspace.setPropertyValueSingle("Scene." .. node .. "Trail.Renderable.Fade", 1.0, 1.0)
    elseif openspace.hasSceneGraphNode(node .. "_trail") then
      openspace.setPropertyValueSingle("Scene." .. node .. "_trail.Renderable.Fade", 1.0, 1.0)
    end
  ]],
  Documentation = [[Shows the associated trail of a scene graph node. This action takes an
    optional argument to determine whose trail to hide. If no argument is provided, the
    current focus node is used instead]],
  GuiPath = "/Trails",
  IsLocal = true
}


asset.onInitialize(function()
  openspace.action.registerAction(ToggleTrail)
  openspace.action.registerAction(HideTrail)
  openspace.action.registerAction(ShowTrail)
end)

asset.onDeinitialize(function()
  openspace.action.removeAction(ShowTrail)
  openspace.action.removeAction(HideTrail)
  openspace.action.removeAction(ToggleTrail)
end)

asset.export("ToggleTrail", ToggleTrail.Identifier)
asset.export("HideTrail", HideTrail.Identifier)
asset.export("ShowTrail", ShowTrail.Identifier)



asset.meta = {
  Name = "Actions - Toggle current Trails",
  Version = "1.0",
  Description = "Asset providing actions to toggle trails",
  Author = "OpenSpace Team",
  URL = "http://openspaceproject.com",
  License = "MIT license"
}
