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 = "Deimos Model",
  Type = "HttpSynchronization",
  Identifier = "deimos_model",
  Version = 1
})

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


local Deimos = {
  Identifier = "Deimos",
  Parent = transforms.MarsBarycenter.Identifier,
  InteractionSphere = 8000,
  Transform = {
    Rotation = {
      Type = "SpiceRotation",
      SourceFrame = "IAU_DEIMOS",
      DestinationFrame = "GALACTIC"
    },
    Translation = {
      Type = "SpiceTranslation",
      Target = "DEIMOS",
      Observer = "MARS BARYCENTER"
    }
  },
  Renderable = {
    Type = "RenderableModel",
    GeometryFile = model .. "Deimos_1_1000.glb",
    ModelScale = "Kilometer",
    RotationVector = { 180, 0, 180 },
    AmbientIntensity = 0.02,
    SpecularIntensity = 0.0,
    LightSources = {
      sun.LightSource
    },
    PerformShading = true
  },
  Tag = { "moon_solarSystem", "moon_terrestrial", "moon_mars" },
  GUI = {
    Name = "Deimos",
    Path = "/Solar System/Planets/Mars/Moons/Deimos",
    Description = "One of two moons of Mars"
  }
}

local DeimosTrail = {
  Identifier = "DeimosTrail",
  Parent = transforms.MarsBarycenter.Identifier,
  Renderable = {
    Type = "RenderableTrailOrbit",
    Translation = {
      Type = "SpiceTranslation",
      Target = "DEIMOS",
      Observer = "MARS BARYCENTER"
    },
    Color = { 1.0, 0.605, 0.420 },
    Period = 1.263,
    Resolution = 1000
  },
  Tag = { "moonTrail_solarSystem", "moonTrail_terrestrial", "moonTrail_mars" },
  GUI = {
    Name = "Deimos Trail",
    Path = "/Solar System/Planets/Mars/Moons/Deimos",
    Description = "Trail for Deimos"
  }
}

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


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

  openspace.addSceneGraphNode(Deimos)
  openspace.addSceneGraphNode(DeimosTrail)
  openspace.addSceneGraphNode(DeimosLabel)
end)

asset.onDeinitialize(function()
  openspace.removeSceneGraphNode(DeimosLabel)
  openspace.removeSceneGraphNode(DeimosTrail)
  openspace.removeSceneGraphNode(Deimos)

  openspace.spice.unloadKernel(kernels .. "mar097.bsp")
end)

asset.export(Deimos)
asset.export(DeimosTrail)



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