-- Create a state machine with a few different states. The state machine can be controlled through
-- the scripting commands from the state machine module.

local function targetNode(nodeIdentifier)
  return [[
    openspace.setPropertyValueSingle("NavigationHandler.OrbitalNavigator.RetargetAnchor", nil)
    openspace.setPropertyValueSingle(
      "NavigationHandler.OrbitalNavigator.Anchor",
      "]] .. nodeIdentifier ..  [["
    )
    openspace.setPropertyValueSingle("NavigationHandler.OrbitalNavigator.Aim", "")
  ]]
end

local States = {
  {
    Identifier = "Constellations",
    Enter = [[
      openspace.setPropertyValueSingle("Scene.Constellations.Renderable.Opacity", 1.0, 1.0)
    ]],
    Exit = [[
      openspace.setPropertyValueSingle("Scene.Constellations.Renderable.Opacity", 0.0, 1.0)
    ]]
  },
  {
    Identifier = "Earth",
    Enter = [[openspace.setPropertyValueSingle("Scene.EarthLabel.Renderable.Enabled", true)]],
    Exit = [[openspace.setPropertyValueSingle("Scene.EarthLabel.Renderable.Enabled", false)]]
  },
  {
    Identifier = "Moon"
  }
}

local Transitions = {
  {
    From = "Earth",
    To = "Moon",
    Action = targetNode("Moon")
  },
  {
    From = "Moon",
    To = "Earth",
    Action = targetNode("Earth")
  },
  {
    From = "Earth",
    To = "Constellations"
  },
  {
    From = "Constellations",
    To = "Earth"
  },
  {
    From = "Moon",
    To = "Constellations",
    Action = targetNode("Earth")
  },
  {
    From = "Constellations",
    To = "Moon",
    Action = targetNode("Moon")
  }
}


asset.onInitialize(function()
  -- Setup (could be done in the profile)
  openspace.setPropertyValueSingle("Scene.Constellations.Renderable.Enabled", true)
  openspace.setPropertyValueSingle("Scene.Constellations.Renderable.Opacity", 0.0)

  openspace.statemachine.createStateMachine(States, Transitions, "Earth")
end)
