local moon_transforms = asset.require("scene/solarsystem/planets/earth/moon/moon")
local sun = asset.require("scene/solarsystem/sun/sun")
asset.require("spice/base")

local models = asset.syncedResource({
  Name = "Apollo Models",
  Type = "HttpSynchronization",
  Identifier = "apollo_models",
  Version = 4
})

local kernels = asset.require("scene/solarsystem/missions/apollo/15/kernels").kernels


-- local models = asset.syncedResource({
--   Name = "Apollo 15 Models",
--   Type = "HttpSynchronization",
--   Identifier = "apollo_models",
--   Version = 1
-- })

local Apollo15 = {
  Identifier = "Apollo15",
  Parent = moon_transforms.Moon.Identifier,
  Transform = {
    Translation = {
      Type = "SpiceTranslation",
      Target = "APOLLO 15",
      Observer = "MOON",
      Frame = "IAU_MOON",
      Kernels = kernels
    },
    Rotation = {
      Type = "SpiceRotation",
      SourceFrame = "A15_METRIC",
      DestinationFrame = "GALACTIC"
    }
  },
  Renderable = {
    Type = "RenderableModel",
    GeometryFile = models .. "ApolloCSM.osmodel",
    ModelScale = 0.0001,
    LightSources = {
      sun.LightSource
    },
    PerformShading = true,
    DisableFaceCulling = true
  },
  TimeFrame = {
    Type = "TimeFrameInterval",
    Start = "1971-07-30T02:22:00.00",
    End = "1971-08-01T18:05:00.00"
  },
  GUI = {
    Name = "Apollo 15",
    Path = "/Solar System/Missions/Apollo/15"
  }
}

local Apollo15Trail = {
  Identifier = "Apollo15Trail",
  Parent = moon_transforms.Moon.Identifier,
  Renderable = {
    Type = "RenderableTrailTrajectory",
    Translation = {
      Type = "SpiceTranslation",
      Target = "APOLLO 15",
      Observer = "MOON",
      Frame = "IAU_MOON",
      Kernels = kernels
    },
    Color = { 0.70, 0.50, 0.20 },
    StartTime = "1971 JUL 26",
    EndTime = "1971 AUG 01 14:30:41.680",
    SampleInterval = 2
  },
  GUI = {
    Name = "Apollo 15 Trail",
    Path = "/Solar System/Missions/Apollo/15"
  }
}

asset.onInitialize(function()
  openspace.addSceneGraphNode(Apollo15)
  openspace.addSceneGraphNode(Apollo15Trail)
end)
    
asset.onDeinitialize(function()
  openspace.removeSceneGraphNode(Apollo15Trail)
  openspace.removeSceneGraphNode(Apollo15)
end)
    
asset.export(Apollo15)
asset.export(Apollo15Trail)
