asset.require("spice/base")
local transforms = asset.require("scene/solarsystem/planets/earth/transforms")
local sunAsset = asset.require("scene/solarsystem/sun/sun")
local earthAsset = asset.require("../earth")



local labelsPath = asset.syncedResource({
  Name = "Moon Labels",
  Type = "HttpSynchronization",
  Identifier = "moon_labels",
  Version = 1
})


local Moon = {
  Identifier = "Moon",
  Parent = transforms.EarthBarycenter.Identifier,
  Transform = {
    Translation = {
      Type = "SpiceTranslation",
      Target = "MOON",
      Observer = "EARTH BARYCENTER"
    },
    Rotation = {
      Type = "SpiceRotation",
      SourceFrame = "IAU_MOON",
      DestinationFrame = "GALACTIC"
    }
  },
  Renderable = {
    Type = "RenderableGlobe",
    Radii = 1738140,
    SegmentsPerPatch = 64,
    Layers = {},
    ShadowGroup = {
      Sources = {
        { Name = sunAsset.Sun.Identifier, Radius = 696300000 },
      },
      Casters = {
        { Name = earthAsset.Earth.Identifier, Radius = 6371000 },
      }
    },
    Labels = {
      Enabled = false,
      FileName = labelsPath .. "moon.labels",
      AlignmentOption = "Horizontally", -- or Circularly
      FontSize = 20.0,
      Size = 7.6,
      FadeInEnabled = true,
      FadeOutEnabled = true,
      FadeDistances = { 100000.0, 500000.0 },
      DistanceEPS = 1350000.0,
      Color = { 1.0, 1.0, 0.0 }
    }
  },
  GUI = {
    Path = "/Solar System/Planets/Earth/Moon"
  }
}

local MoonLabel = {
  Identifier = "MoonLabel",
  Parent = Moon.Identifier,
  Renderable = {
    Type = "RenderableLabel",
    Enabled = false,
    Text = "Moon",
    FontSize = 70.0,
    Size = 6.0,
    MinMaxSize = { 1, 40 },
    OrientationOption = "Camera View Direction",
    BlendMode = "Additive",
    EnableFading = true,
    FadeUnit = "Gm",
    FadeDistances = { 0.4, 10.0 },
    FadeWidths = { 0.1, 10.0 }
  },
  Tag = { "solarsystem_labels", "moon_labels", "major_moon_labels" },
  GUI = {
    Name = "Moon Label",
    Path = "/Solar System/Planets/Earth/Moon",
    Description = "Label for Earth's Moon"
  }
}

local FocusMoon = {
  Identifier = "os.earth.FocusMoon",
  Name = "Focus on Moon",
  Command = [[
    openspace.setPropertyValueSingle("NavigationHandler.OrbitalNavigator.Aim", "")
    openspace.setPropertyValueSingle("NavigationHandler.OrbitalNavigator.Anchor", "Moon")
    openspace.setPropertyValueSingle("NavigationHandler.OrbitalNavigator.RetargetAnchor", nil)
  ]],
  Documentation = "Set camera focus on the Moon",
  GuiPath = "/Solar System/Earth/Moon",
  isLocal = false
}

local ToggleMoonShading = {
  Identifier = "os.earth.ToggleMoonShading",
  Name = "Toggle Moon shading",
  Command = [[
    openspace.setPropertyValueSingle(
      "Scene.Moon.Renderable.PerformShading",
      not openspace.getPropertyValue("Scene.Moon.Renderable.PerformShading")
    )
  ]],
  Documentation = "Toggles the shading of the Moon",
  GuiPath = "/Solar System/Earth/Moon",
  isLocal = false
}


asset.onInitialize(function()
  openspace.addSceneGraphNode(Moon)
  openspace.addSceneGraphNode(MoonLabel)

  openspace.action.registerAction(FocusMoon)
  openspace.action.registerAction(ToggleMoonShading)
end)

asset.onDeinitialize(function()
  openspace.action.removeAction(ToggleMoonShading)
  openspace.action.removeAction(FocusMoon)

  openspace.removeSceneGraphNode(MoonLabel)
  openspace.removeSceneGraphNode(Moon)
end)

asset.export(Moon)
asset.export(MoonLabel)
asset.export("FocusMoon", FocusMoon.Identifier)
asset.export("ToggleMoonShading", ToggleMoonShading.Identifier)



asset.meta = {
  Name = "Moon",
  Version = "1.2",
  Description = "Moon globe with labels and actions",
  Author = "OpenSpace Team",
  URL = "http://openspaceproject.com",
  License = "MIT license"
}
