local proceduralGlobes = asset.require("util/procedural_globe")
local transforms = asset.require("../transforms")

local syncedKernels = asset.syncedResource({
  Name = "Saturn Shepherd Moons Spice Kernels",
  Type = "HttpSynchronization",
  Identifier = "saturn_shepherd_kernels",
  Version = 1
})

local kernels = {
  syncedKernels .. "sat415.bsp",
  syncedKernels .. "sat393_daphnis.bsp"
}

local parentIdentifier = transforms.SaturnBarycenter.Identifier
local parentSpice = "SATURN BARYCENTER"
local guiPath = "/Solar System/Planets/Saturn/Moons/Shepherd Moons"
local tags = { "moon_solarSystem", "moon_giants", "moon_saturn", "moon_shepherd", "moon_minor" }
local trailColor = { 0.5, 0.3, 0.3 }
local trailTags = { 
  "moonTrail_solarSystem",
  "moonTrail_giants",
  "moonTrail_saturn",
  "moonTrail_minor"
}

local shepherdGroup = {
  {
    Identifier = "Prometheus",
    Parent = {
      Identifier = parentIdentifier,
      Spice = parentSpice
    },
    Spice = "PROMETHEUS",
    Radii = { 135600, 79400, 59400 },
    Tags = tags,
    TrailTags = trailTags,
    GUI = {
      Path = guiPath
    },
    TrailColor = trailColor,
    OrbitPeriod = 0.612990038,
    Kernels = kernels
  },
  {
    Identifier = "Pandora",
    Parent = {
      Identifier = parentIdentifier,
      Spice = parentSpice
    },
    Spice = "PANDORA",
    Radii = { 104000, 81000, 64000 },
    Tags = tags,
    TrailTags = trailTags,
    GUI = {
      Path = guiPath
    },
    TrailColor = trailColor,
    OrbitPeriod = 0.628504213,
    Kernels = kernels
  },
  {
    Identifier = "Atlas",
    Parent = {
      Identifier = parentIdentifier,
      Spice = parentSpice
    },
    Spice = "ATLAS",
    Radii = { 40800, 35400, 18800 },
    Tags = tags,
    TrailTags = trailTags,
    GUI = {
      Path = guiPath
    },
    TrailColor = trailColor,
    OrbitPeriod = 0.6016947883,
    Kernels = kernels
  },
  {
    Identifier = "Janus",
    Parent = {
      Identifier = parentIdentifier,
      Spice = parentSpice
    },
    Spice = "JANUS",
    Radii = { 203000, 185000, 152600 },
    Tags = tags,
    TrailTags = trailTags,
    GUI = {
      Path = guiPath
    },
    TrailColor = trailColor,
    OrbitPeriod = 0.694660342,
    Kernels = kernels
  },
  {
    Identifier = "Epimetheus",
    Parent = {
      Identifier = parentIdentifier,
      Spice = parentSpice
    },
    Spice = "EPIMETHEUS",
    Radii = { 129800, 114000, 106200 },
    Tags = tags,
    TrailTags = trailTags,
    GUI = {
      Path = guiPath
    },
    TrailColor = trailColor,
    OrbitPeriod = 0.694333517,
    Kernels = kernels
  },
  {
    Identifier = "Daphnis",
    Parent = {
      Identifier = parentIdentifier,
      Spice = parentSpice
    },
    Spice = "DAPHNIS",
    Radii = { 8600, 8200, 6400 },
    Tags = tags,
    TrailTags = trailTags,
    GUI = {
      Path = guiPath
    },
    TrailColor = trailColor,
    OrbitPeriod = 0.5940798,
    Kernels = kernels
  },
  {
    Identifier = "Pan",
    Parent = {
      Identifier = parentIdentifier,
      Spice = parentSpice
    },
    Spice = "PAN",
    Radii = { 34400, 31400, 20800 },
    Tags = tags,
    TrailTags = trailTags,
    GUI = {
      Path = guiPath
    },
    TrailColor = trailColor,
    OrbitPeriod = 0.575050718,
    Kernels = kernels
  }
}

local nodes = proceduralGlobes.createGlobes(shepherdGroup)

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

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

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


asset.meta = {
  Name = "Saturn Shepherd Moons",
  Version = "1.0",
  Description = [[Procedural Globe asset containing Saturn's Shepherd moons: Prometheus,
    Pandora, Atlas, Janus, Epimetheus, Daphnis and Pan. Blank globes and SPICE trails are
    generated for each moon]],
  Author = "OpenSpace Team",
  URL = "http://openspaceproject.com",
  License = "MIT license"
}
