local earth_transforms = asset.require("scene/solarsystem/planets/earth/transforms")
local sun = asset.require("scene/solarsystem/sun/sun")
local kernels = asset.require("./kernels")



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


local ApolloSpiceId = -908

local Apollo8Launch = {
  Identifier = "Apollo8Launch",
  Parent = earth_transforms.EarthIAU.Identifier,
  TimeFrame = {
    Type = "TimeFrameInterval",
    Start = "1968 DEC 21",
    End = "1968 DEC 28"
  },
  Transform = {
    Translation = {
      Type = "SpiceTranslation",
      Target = ApolloSpiceId,
      Observer = "EARTH",
      Frame = "IAU_EARTH"
    }
  },
  GUI = {
    Name = "Apollo 8 Launch Capsule",
    Path = "/Solar System/Missions/Apollo/8"
  }
}

local Apollo8LaunchModel = {
  Identifier = "Apollo8LaunchModel",
  Parent = Apollo8Launch.Identifier,
  TimeFrame = {
    Type = "TimeFrameInterval",
    Start = "1968 DEC 21",
    End = "1968 DEC 22"
  },
  Transform = {
    Rotation = {
      Type = "StaticRotation",
      Rotation = { 0.0, 0.0, -math.pi / 2 }
    }
  },
  Renderable = {
    Type = "RenderableModel",
    GeometryFile = models .. "ApolloCSM.osmodel",
    ModelScale = 0.0001,
    LightSources = {
      sun.LightSource
    },
    PerformShading = true
  },
  GUI = {
    Hidden = true,
    Name = "Apollo 8 Launch Model",
    Path = "/Solar System/Missions/Apollo/8"
  }
}


asset.onInitialize(function()
  openspace.spice.loadKernel(kernels.kernels)

  openspace.addSceneGraphNode(Apollo8Launch)
  openspace.addSceneGraphNode(Apollo8LaunchModel)
end)

asset.onDeinitialize(function()
  openspace.removeSceneGraphNode(Apollo8LaunchModel)
  openspace.removeSceneGraphNode(Apollo8Launch)

  openspace.spice.unloadKernel(kernels.kernels)
end)

asset.export(Apollo8Launch)
asset.export(Apollo8LaunchModel)
