local transforms = asset.require("scene/solarsystem/sun/transforms")
asset.require("spice/base")

local circle = asset.syncedResource({
  Name = "Circle",
  Type = "HttpSynchronization",
  Identifier = "circle_image",
  Version = 1
})

local kernels = asset.syncedResource({
  Name = "Lagrange Kernels",
  Type = "HttpSynchronization",
  Identifier = "earth_lagrange_kernels",
  Version = 1
})

local L4Position = {
  Identifier = "L4Position",
  Parent = transforms.SolarSystemBarycenter.Identifier,
  Transform = {
    Translation = {
      Type = "SpiceTranslation",
      Target = 394, -- L4
      Observer = "SSB",
      Kernels = kernels .. "L4_de431.bsp"
    }
  },
  Tag = { "lagrange_points_earth", "lagrange_points_earth_l4" },
  GUI = {
    Name = "L4 Position",
    Path = "/Solar System/Planets/Earth/Lagrange points",
    Hidden = true
  }
}

local L4 = {
  Identifier = "L4",
  Parent = L4Position.Identifier,
  Renderable = {
    Type = "RenderablePlaneImageLocal",
    RenderBinMode = "Opaque",
    Billboard = true,
    Size = 800E6,
    Texture = circle .. "circle.png",
    BlendMode = "Additive"
  },
  Tag = { "lagrange_points_earth", "lagrange_points_earth_l4" },
  GUI = {
    Name = "L4",
    Path = "/Solar System/Planets/Earth/Lagrange points",
  }
}

local L4Label = {
  Identifier = "L4Label",
  Parent = L4Position.Identifier,
  Renderable = {
    Type = "RenderableLabels",
    Text = "L4",
    FontSize = 50,
    Size = 8.5,
    MinMaxSize = { 1.0, 100.0 },
    OrientationOption = "Camera View Direction",
    BlendMode = "Normal",
    EnableFading = false
  },
  Tag = { "lagrange_points_earth", "lagrange_points_earth_l4" },
  GUI = {
    Name = "L4 Label",
    Path = "/Solar System/Planets/Earth/Lagrange points",
    Description = "Main label for L4"
  }
}

asset.onInitialize(function()
  openspace.addSceneGraphNode(L4Position)
  openspace.addSceneGraphNode(L4)
  openspace.addSceneGraphNode(L4Label)
end)

asset.onDeinitialize(function()
  openspace.removeSceneGraphNode(L4Label)
  openspace.removeSceneGraphNode(L4)
  openspace.removeSceneGraphNode(L4Position)
end)

asset.export(L4Position)
asset.export(L4)
asset.export(L4Label)


asset.meta = {
  Name = "Lagrange point L4",
  Version = "1.0",
  Description = "Lagrange point L4 node and label",
  Author = "OpenSpace Team",
  URL = "http://openspaceproject.com",
  License = "MIT license"
}
