-- Apollo mission insignias on their locations on the Lunar surface.
-- The insignias are invisible by default, but can be enabled using shown or hidden using
-- the exported functions `showInsignias(interpolationDuration)` and `hideInsignias(interpolationDuration)`.

local insigniasPath =  asset.syncedResource({
  Name = "Apollo Insignias",
  Type = "HttpSynchronization",
  Identifier = "apollo_insignias",
  Version = 1
})
local moon = asset.require("scene/solarsystem/planets/earth/moon/moon")

local landingData = {
  {
    Identifier = "Apollo11",
    Name = "Apollo 11",
    Name = "Apollo 11",
    Texture = "apollo11.png",
    LunarModule = { 0.67409, 23.47298, 0.0 }
  },
  {
    Identifier = "Apollo12",
    Name = "Apollo 12",
    Texture = "apollo12.png",
    LunarModule = { -3.01381, -23.41930, 0.0 }
  },
  {
    Identifier = "Apollo14",
    Name = "Apollo 14",
    Texture = "apollo14.png",
    LunarModule = { -3.64544, -17.47139, 0.0 }
  },
  {
    Identifier = "Apollo15",
    Name = "Apollo 15",
    Texture = "apollo15.png",
    LunarModule = { 26.13224, 3.63400, 0.0 }
  },
  {
    Identifier = "Apollo16",
    Name = "Apollo 16",
    Texture = "apollo16.png",
    LunarModule = { -8.97341, 15.49859, 0.0 }
  },
  {
    Identifier = "Apollo17",
    Name = "Apollo 17",
    Texture = "apollo17.png",
    LunarModule = { 20.18809, 30.77475, 0.0 }
  }
}

local nodes = {}
local size = 100000;

for i = 1, #landingData do
  local entry = landingData[i]
  nodes[i] = {
    Identifier = entry.Identifier .. "Insignia",
    Parent = moon.Moon.Identifier,
    Transform = {
      Translation = {
        Type = "GlobeTranslation",
        Globe = moon.Moon.Identifier,
        Latitude = entry.LunarModule[1],
        Longitude = entry.LunarModule[2],
        Altitude = entry.LunarModule[3] + size * 1.1,
        UseHeightmap = false
      }
    },
    Renderable = {
      Type = "RenderablePlaneImageLocal",
      Size = size,
      Origin = "Center",
      Billboard = true,
      Texture = insigniasPath .. entry.Texture,
      Opacity = 0.0
    },
    GUI = {
      Path = "/Other/Labels",
      Name = entry.Name .. " Insignia"
    }
  }
end

local show_apollo_labels = {
  Identifier = "apollo_insignias.show_insignias",
  Name = "Show Apollo Landing Labels",
  Command = [[openspace.setPropertyValue("Scene.Apollo*Insignia.Renderable.Opacity", 1, 0.5)]],
  Documentation = "Show patches of the Apollo missions on their respective landing sites",
  GuiPath = "/Missions/Apollo",
  IsLocal = true
}

local hide_apollo_labels = {
  Identifier = "apollo_insignias.hide_insignias",
  Name = "Hide Apollo Landing Labels",
  Command = [[openspace.setPropertyValue("Scene.Apollo*Insignia.Renderable.Opacity", 0, 0.5)]],
  Documentation = "Hide patches of the Apollo missions on their respective landing sites",
  GuiPath = "/Missions/Apollo",
  IsLocal = true
}

asset.onInitialize(function () 
  openspace.action.registerAction(show_apollo_labels)
  openspace.action.registerAction(hide_apollo_labels)
  for _, node in ipairs(nodes) do
    openspace.addSceneGraphNode(node)
  end
end)

asset.onDeinitialize(function ()
  for i = #nodes, 1, -1 do
    local node = nodes[i]
    openspace.removeSceneGraphNode(node)
  end

  openspace.action.removeAction(show_apollo_labels)
  openspace.action.removeAction(hide_apollo_labels)
end)

for _, node in ipairs(nodes) do
  asset.export(node)
end

asset.export("showInsignia", function (missionNumber, interpolationDuration)
  openspace.setPropertyValue("Scene.Apollo" .. missionNumber .. "Insignia.Renderable.Opacity", 1, interpolationDuration)
end)

asset.export("hideInsignia", function (missionNumber, interpolationDuration)
  openspace.setPropertyValue("Scene.Apollo" .. missionNumber .. "Insignia.Renderable.Opacity", 0, interpolationDuration)
end)

asset.export("showInsignias", function (interpolationDuration)
  openspace.setPropertyValue("Scene.Apollo*Insignia.Renderable.Opacity", 1, interpolationDuration)
end)

asset.export("hideInsignias", function (interpolationDuration)
  openspace.setPropertyValue("Scene.Apollo*Insignia.Renderable.Opacity", 0, interpolationDuration)
end)
