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 = 2
})

local L2Position = {
  Identifier = "L2Position",
  Parent = transforms.SolarSystemBarycenter.Identifier,
  Transform = {
    Translation = {
      Type = "SpiceTranslation",
      Target = 392, -- L2
      Observer = "SSB",
      Kernels = kernels .. "L2_de431.bsp"
    }
  },
  Tag = { "lagrange_points_earth", "lagrange_points_earth_l2" },
  GUI = {
    Name = "L2 Position",
    Path = "/Solar System/Planets/Earth/Lagrange points",
    Hidden = true
  }
}

-- This ref. frame co-revolves with L2 and is needed for a more intuitive trail of JWST in
-- relation to L2
local L2CoRevFrame = {
  Identifier = "L2CoRevFrame",
  Parent = L2Position.Identifier,
  Transform = {
    Rotation = {
      Type = "SpiceRotation",
      SourceFrame = "L2_COREV",
      DestinationFrame = 'GALACTIC',
      Kernels = {
        kernels .. "L2_de431.bsp",
        kernels .. "L2_corev.tf"
      }
    }
  },
  Tag = { "lagrange_points_earth", "lagrange_points_earth_l2" },
  GUI = {
    Name = "L2 Co-revolving Reference Frame",
    Path = "/Solar System/Planets/Earth/Lagrange points",
    Hidden = true
  }
}

local L2Small = {
  Identifier = "L2Small",
  Parent = L2Position.Identifier,
  Renderable = {
    Type = "RenderablePlaneImageLocal",
    RenderBinMode = "Opaque",
    Billboard = true,
    Size = 400E4,
    Texture = circle .. "circle.png",
    BlendMode = "Additive"
  },
  Tag = { "lagrange_points_earth_l2_small" },
  GUI = {
    Name = "L2 Small",
    Path = "/Solar System/Planets/Earth/Lagrange points",
  }
}

local L2 = {
  Identifier = "L2",
  Parent = L2Position.Identifier,
  Renderable = {
    Type = "RenderablePlaneImageLocal",
    RenderBinMode = "Opaque",
    Billboard = true,
    Size = 700E5,
    Texture = circle .. "circle.png",
    BlendMode = "Additive"
  },
  Tag = { "lagrange_points_earth", "lagrange_points_earth_l2" },
  GUI = {
    Name = "L2",
    Path = "/Solar System/Planets/Earth/Lagrange points",
  }
}

local L2SmallLabel = {
  Identifier = "L2SmallLabel",
  Parent = L2Position.Identifier,
  Renderable = {
    Type = "RenderableLabels",
    Text = "L2",
    FontSize = 50.0,
    Size = 6.0,
    MinMaxSize = { 1.0, 100.0 },
    OrientationOption = "Camera View Direction",
    BlendMode = "Normal",
    EnableFading = false
  },
  Tag = { "lagrange_points_earth_l2_small" },
  GUI = {
    Name = "L2 Small Label",
    Path = "/Solar System/Planets/Earth/Lagrange points",
    Description = "Small label for L2"
  }
}

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

local L2SunLine = {
  Identifier = "L2SunLine",
  Parent = transforms.SolarSystemBarycenter.Identifier,
  Renderable = {
    Type = "RenderableNodeLine",
    StartNode = "Sun",
    EndNode = "L2Small",
    Color = { 0.5, 0.5, 0.5 },
    LineWidth = 2
  },
  Tag = { "lagrange_points_earth_l2_small" },
  GUI = {
    Name = "Sun to L2 Line",
    Path = "/Solar System/Planets/Earth/Lagrange points",
  }
}

local nodes = {
  L2Position,
  L2CoRevFrame,
  L2Small,
  L2,
  L2SunLine,
  L2SmallLabel,
  L2Label
}

asset.onInitialize(function()
  for i, node in ipairs(nodes) do
    openspace.addSceneGraphNode(node)
  end
end)

asset.onDeinitialize(function()
  for i = #nodes, 1, -1 do
    openspace.removeSceneGraphNode(nodes[i])
  end
end)

for i, node in ipairs(nodes) do
  asset.export(node)
end


asset.meta = {
  Name = "Lagrange point L2",
  Version = "1.0",
  Description = [[Lagrange point L2 large and small nodes, sun line, and large and small
    labels]],
  Author = "OpenSpace Team",
  URL = "http://openspaceproject.com",
  License = "MIT license"
}
