local transforms = asset.require("scene/solarsystem/planets/earth/transforms")



local omm = asset.syncedResource({
  Name = "Satellite OMM Data (Terra)",
  Type = "UrlSynchronization",
  Identifier = "satellite_omm_data_terra",
  Url = "https://www.celestrak.com/NORAD/elements/gp.php?CATNR=25994&FORMAT=kvn",
  Filename = "Terra.txt",
  Override = true
})


local Terra = {
  Identifier = "Terra",
  Parent = transforms.EarthInertial.Identifier,
  InteractionSphere = 30,
  Transform = {
    Translation = {
      Type = "GPTranslation",
      Observer = transforms.EarthInertial.Identifier,
      File = omm .. "Terra.txt",
      Format = "OMM"
    },
    Rotation = {
      Type = "SpiceRotation",
      SourceFrame = "GALACTIC",
      DestinationFrame = "J2000",
    }
  },
  Tag = { "earth_satellite", "Terra" },
  GUI = {
    Name = "Terra",
    Path = "/Solar System/Planets/Earth/Satellites/Terra"
  }
}

local TerraTrail = {
  Identifier = "Terra_trail",
  Parent = transforms.EarthInertial.Identifier,
  Renderable = {
    Type = "RenderableTrailOrbit",
    Translation = {
      Type = "GPTranslation",
      Observer = transforms.EarthInertial.Identifier,
      File = omm .. "Terra.txt",
      Format = "OMM",
      RenderBinMode = "PostDeferredTransparent"
    },
    Color = { 0.9, 0.6715, 0.0 },
    Fade = 1.5,
    Resolution = 320
  },
  Tag = { "earth_satellite", "Terra" },
  GUI = {
    Name = "Terra Trail",
    Path = "/Solar System/Planets/Earth/Satellites/Terra"
  }
}

-- @TODO (emmbr, 2021-05-27) this label is not visible. Too large fade distances?
-- Might also be affected by the rendering issues for labels
local TerraLabel = {
  Identifier = "TerraLabel",
  Parent = Terra.Identifier,
  Renderable = {
    Enabled = false,
    Type = "RenderableLabel",
    Text = "Terra",
    FontSize = 70.0,
    Size = 4.0,
    MinMaxSize = { 1, 100 },
    OrientationOption = "Camera View Direction",
    BlendMode = "Additive",
    EnableFading = true,
    FadeUnit = "au",
    FadeDistances = { 1.5, 15.0 },
    FadeWidths = { 1.0, 25.0 }
  },
  Tag = { "solarsystem_labels" },
  GUI = {
    Name = "Terra Label",
    Path = "/Solar System/Planets/Earth"
  }
}


asset.onInitialize(function()
  local t = openspace.space.readKeplerFile(omm .. "Terra.txt", "OMM")
  TerraTrail.Renderable.Period = t[1].Period / (60 * 60 * 24)

  openspace.addSceneGraphNode(Terra)
  openspace.addSceneGraphNode(TerraTrail)
  openspace.addSceneGraphNode(TerraLabel)
end)

asset.onDeinitialize(function()
  openspace.removeSceneGraphNode(TerraLabel)
  openspace.removeSceneGraphNode(TerraTrail)
  openspace.removeSceneGraphNode(Terra)
end)

asset.export(Terra)
asset.export(TerraTrail)
asset.export(TerraLabel)



asset.meta = {
  Name = "Terra",
  Version = "1.0",
  Description = [[Trail for Terra. trail from Celestrak. Can be used in conjuntion with
    Terra Modis (Temporal) layer for Earth]],
  Author = "OpenSpace Team",
  URL = "https://celestrak.com/",
  License = "NASA"
}
