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



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 L2 = 392

local L2Position = {
  Identifier = "L2Position",
  Parent = transforms.SolarSystemBarycenter.Identifier,
  Transform = {
    Translation = {
      Type = "SpiceTranslation",
      Target = L2,
      Observer = "SSB"
    }
  },
  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"
    }
  },
  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",
    Billboard = true,
    Size = 4000000,
    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",
    Billboard = true,
    Size = 70000000,
    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 = "RenderableLabel",
    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 = "RenderableLabel",
    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"
  }
}


asset.onInitialize(function()
  openspace.spice.loadKernel(kernels .. "L2_de431.bsp")
  openspace.spice.loadKernel(kernels .. "L2_corev.tf")

  openspace.addSceneGraphNode(L2Position)
  openspace.addSceneGraphNode(L2CoRevFrame)
  openspace.addSceneGraphNode(L2Small)
  openspace.addSceneGraphNode(L2)
  openspace.addSceneGraphNode(L2SunLine)
  openspace.addSceneGraphNode(L2SmallLabel)
  openspace.addSceneGraphNode(L2Label)
end)

asset.onDeinitialize(function()
  openspace.removeSceneGraphNode(L2Label)
  openspace.removeSceneGraphNode(L2SmallLabel)
  openspace.removeSceneGraphNode(L2SunLine)
  openspace.removeSceneGraphNode(L2)
  openspace.removeSceneGraphNode(L2Small)
  openspace.removeSceneGraphNode(L2CoRevFrame)
  openspace.removeSceneGraphNode(L2Position)

  openspace.spice.unloadKernel(kernels .. "L2_corev.tf")
  openspace.spice.unloadKernel(kernels .. "L2_de431.bsp")
end)

asset.export(L2Position)
asset.export(L2CoRevFrame)
asset.export(L2Small)
asset.export(L2)
asset.export(L2SunLine)
asset.export(L2SmallLabel)
asset.export(L2Label)



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"
}
