local sunTransforms = asset.require("scene/solarsystem/sun/transforms")
local model = asset.require("scene/solarsystem/missions/pioneer/pioneermodel")



local kernelsFolder = asset.syncedResource({
  Name = "Pioneer Kernels",
  Type = "HttpSynchronization",
  Identifier = "pioneer_11_spice",
  Version = 1
})


local kernel = {
  kernelsFolder .. "p11-a.bsp",
  kernelsFolder .. "p11_sat336.bsp"
}

local Pioneer11ID = -24

local Pioneer11 = {
  Identifier = "Pioneer_11",
  Parent = sunTransforms.SolarSystemBarycenter.Identifier,
  Transform = {
    Translation = {
      Type = "SpiceTranslation",
      Target = Pioneer11ID,
      Observer = "SSB"
    }
  },
  Renderable = model.PioneerModel,
  GUI = {
    Name = "Pioneer 11 Spacecraft",
    Path = "/Solar System/Missions/Pioneer",
    Description = "Pioneer 11 Spacecraft. Positioned by SPICE data"
  }
}

local Pioneer11Trail = {
  Identifier = "Pioneer_11Trail",
  Parent = sunTransforms.SolarSystemBarycenter.Identifier,
  Renderable = {
    Type = "RenderableTrailTrajectory",
    Translation = {
      Type = "SpiceTranslation",
      Target = Pioneer11ID,
      Observer = "SSB"
    },
    Color = { 0.70, 0.50, 0.20 },
    StartTime = "1973 APR 06 02:25:00.000",
    EndTime = "1990 JAN 02 00:00:00.000",
    EnableFade = false,
    -- 6087 is the number of days between the Start and End time (so sample every 2d)
    SampleInterval = 6087 * 2
  },
  GUI = {
    Name = "Pioneer 11 Trail (SPICE)",
    Path = "/Solar System/Missions/Pioneer",
    Description = [[Pioneer 11 Trail, spanning April 6th, 1973 to January 2nd, 1990.
      Data from SPICE]]
  }
}


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

  openspace.addSceneGraphNode(Pioneer11)
  openspace.addSceneGraphNode(Pioneer11Trail)
end)

asset.onDeinitialize(function()
  openspace.removeSceneGraphNode(Pioneer11Trail)
  openspace.removeSceneGraphNode(Pioneer11)

  openspace.spice.unloadKernel(kernel)
end)

asset.export(Pioneer11)
asset.export(Pioneer11Trail)



asset.meta = {
  Name = "Pioneer 11",
  Version = "1.0",
  Description = [[Pioneer 11 Model and Trail. Driven by SPICE data for high accuracy from
    April 6th, 1973 to January 2nd, 1990]],
  Author = "OpenSpace Team",
  URL = "http://openspaceproject.com",
  License = "MIT license"
}
