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,
  InteractionSphere = 15000,
  Transform = {
    Rotation = {
      Type = "SpiceRotation",
      SourceFrame = "IAU_PHOBOS",
      DestinationFrame = "GALACTIC"
    },
    Translation = {
      Type = "SpiceTranslation",
      Target = "PHOBOS",
      Observer = "MARS BARYCENTER"
    }
  },
  Renderable = {
    Type = "RenderableModel",
    GeometryFile = model .. "Phobos_1_1000.glb",
    ModelScale = "Kilometer",
    RotationVector = { 90.0, 0.0, 90.0 },
    AmbientIntensity = 0.02,
    SpecularIntensity = 0.0,
    LightSources = {
      sun.LightSource
    },
    PerformShading = true
  },
  Tag = { "moon_solarSystem", "moon_terrestrial", "moon_mars" },
  GUI = {
    Name = "Phobos",
    Path = "/Solar System/Planets/Mars/Moons/Phobos",
    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"
    },
    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/Moons/Phobos",
    Description = "Trail for Phobos"
  }
}

local PhobosLabel = {
  Identifier = "PhobosLabel",
  Parent = Phobos.Identifier,
  Renderable = {
    Enabled = false,
    Type = "RenderableLabel",
    Text = "Phobos",
    FontSize = 70.0,
    Size = 5.0,
    MinMaxSize = { 1, 25 },
    OrientationOption = "Camera View Direction",
    BlendMode = "Additive",
    EnableFading = true,
    FadeUnit = "Mm",
    FadeDistances = { 30.0, 1000.0 },
    FadeWidths = { 30.0, 2000.0 }
  },
  Tag = { "solarsystem_labels", "moon_labels", "major_moon_labels" },
  GUI = {
    Name = "Phobos Label",
    Path = "/Solar System/Planets/Mars/Moons/Phobos",
    Description = "Label for Mars' moon Phobos"
  }
}


asset.onInitialize(function()
  openspace.spice.loadKernel(kernels .. "mar097.bsp")

  openspace.addSceneGraphNode(Phobos)
  openspace.addSceneGraphNode(PhobosTrail)
  openspace.addSceneGraphNode(PhobosLabel)
end)

asset.onDeinitialize(function()
  openspace.removeSceneGraphNode(PhobosLabel)
  openspace.removeSceneGraphNode(PhobosTrail)
  openspace.removeSceneGraphNode(Phobos)

  openspace.spice.unloadKernel(kernels .. "mar097.bsp")
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"
}
