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 Apollo8 = {
  Identifier = "Apollo8",
  Parent = earth_transforms.EarthBarycenter.Identifier,
  TimeFrame = {
    Type = "TimeFrameInterval",
    Start = "1968 DEC 21",
    End = "1968 DEC 28"
  },
  Transform = {
    Translation = {
      Type = "SpiceTranslation",
      Target = ApolloSpiceId,
      Observer = "EARTH BARYCENTER",
      Frame = "GALACTIC"
    },
    Rotation = {
      Type = "SpiceRotation",
      SourceFrame = "A8_EARTHRISE",
      DestinationFrame = "GALACTIC",
      TimeFrame = {
        -- The orientation of Apollo 8 is only available during the few minutes
        -- when the Earthrise picture was taken.
        Type = "TimeFrameInterval",
        Start = "1968 DEC 24 16:37:19",
        End = "1968 DEC 24 16:40:15"
      }
    }
  },
  GUI = {
    Name = "Apollo 8",
    Path = "/Solar System/Missions/Apollo/8"
  }
}

local Apollo8Model = {
  Identifier = "Apollo8Model",
  Parent = Apollo8.Identifier,
  TimeFrame = {
    Type = "TimeFrameInterval",
    Start = "1968 DEC 22",
    End = "1968 DEC 28"
  },
  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 Model",
    Path = "/Solar System/Missions/Apollo/8"
  }
}

-- The pivot node is used for navigation inside the spacecraft
local Apollo8Pivot = {
  Identifier = "Apollo8Pivot",
  Parent = Apollo8.Identifier,
  Transform = {
    Translation = {
      Type = "StaticTranslation",
      Position = { 0, 2.5, 0 }
    }
  },
  GUI = {
    Name = "Apollo 8 Pivot",
    Path = "/Solar System/Missions/Apollo/8"
  }
}


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

  openspace.addSceneGraphNode(Apollo8)
  openspace.addSceneGraphNode(Apollo8Model)
  openspace.addSceneGraphNode(Apollo8Pivot)
end)

asset.onDeinitialize(function()
  openspace.removeSceneGraphNode(Apollo8Pivot)
  openspace.removeSceneGraphNode(Apollo8Model)
  openspace.removeSceneGraphNode(Apollo8)

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

asset.export(Apollo8)
asset.export(Apollo8Model)
asset.export(Apollo8Pivot)
