local transforms = asset.require('scene/solarsystem/planets/mars/transforms')
local sunTransforms = asset.require('scene/solarsystem/sun/transforms')
local sun = asset.require("scene/solarsystem/sun/sun")

local model = asset.syncedResource({
  Name = "Phobos Model",
  Type = "HttpSynchronization",
  Identifier = "phobos_model",
  Version = 1
})

local kernels = asset.syncedResource({
  Name = "Mars Spice Kernels",
  Type = "HttpSynchronization",
  Identifier = "mars_kernels",
  Version = 1
})


local Phobos = {
  Identifier = "Phobos",
  Parent = transforms.MarsBarycenter.Identifier,
  Transform = {
    Rotation = {
      Type = "SpiceRotation",
      SourceFrame = "IAU_PHOBOS",
      DestinationFrame = "GALACTIC",
      Kernels = kernels .. "mar097.bsp"
    },
    Translation = {
      Type = "SpiceTranslation",
      Target = "PHOBOS",
      Observer = "MARS BARYCENTER",
      Kernels = kernels .. "mar097.bsp"
    }
  },
  Renderable = {
    Type = "RenderableModel",
    GeometryFile = model .. "/Phobos_1_1000.glb",
    ModelScale = 'Kilometer',
    RotationVector = { 90, 0, 90 },
    AmbientIntensity = 0.02,
    SpecularIntensity = 0.0,
    LightSources = {
      sun.LightSource
    },
    PerformShading = true,
    DisableFaceCulling = true
  },
  Tag = { "moon_solarSystem", "moon_terrestrial", "moon_mars" },
  GUI = {
    Name = "Phobos",
    Path = "/Solar System/Planets/Mars",
    Description = "One of two moons of Mars"
  }
}

local PhobosTrail = {
  Identifier = "PhobosTrail",
  Parent = transforms.MarsBarycenter.Identifier,
  Renderable = {
    Type = "RenderableTrailOrbit",
    Translation = {
      Type = "SpiceTranslation",
      Target = "PHOBOS",
      Observer = "MARS BARYCENTER",
      Kernels = kernels .. "mar097.bsp"
    },
    Color = { 1.0, 0.605, 0.420 },
    Period = 0.31891023,
    Resolution = 1000
  },
  Tag = { "moonTrail_solarSystem", "moonTrail_terrestrial", "moonTrail_mars" },
  GUI = {
    Name = "Phobos Trail",
    Path = "/Solar System/Planets/Mars",
    Description = "Trail for Phobos"
  }
}

asset.onInitialize(function()
  openspace.addSceneGraphNode(Phobos)
  openspace.addSceneGraphNode(PhobosTrail)
end)
    
asset.onDeinitialize(function()
  openspace.removeSceneGraphNode(PhobosTrail)
  openspace.removeSceneGraphNode(Phobos)
end)
    
asset.export(Phobos)
asset.export(PhobosTrail)



asset.meta = {
  Name = "Phobos",
  Version = "1.0",
  Description = "RenderableModel and Trail for Phobos",
  Author = "OpenSpace Team",
  URL = "http://openspaceproject.com",
  License = "MIT license"
}
