local moon = asset.require("scene/solarsystem/planets/earth/moon/moon")
-- This is technically not necessary, but we need to ensure that the default layers load
-- first before we add the layers from this assets or else the default layers might be
-- initialized later, causing them to appear **after** these layers
asset.require("scene/solarsystem/planets/earth/moon/default_layers")


local heightmaps = asset.syncedResource({
  Name = "Apollo Globebrowsing Heightmaps",
  Type = "HttpSynchronization",
  Identifier = "apollo_globebrowsing_heightmaps",
  Version = 1
})

local basemaps = asset.syncedResource({
  Name = "Apollo Globebrowsing Basemaps",
  Type = "HttpSynchronization",
  Identifier = "apollo_globebrowsing_basemaps",
  Version = 1
})

local naclighting = asset.syncedResource({
  Name = "Apollo Globebrowsing NAC Lighting",
  Type = "HttpSynchronization",
  Identifier = "apollo_globebrowsing_naclighting",
  Version = 1
})

local stations = asset.syncedResource({
  Name = "Apollo 17 Globebrowsing Stations",
  Type = "HttpSynchronization",
  Identifier = "apollo_17_stations",
  Version = 1
})


local ToggleKaguyaLayer = {
  Identifier = "os.apollo.moon.ToggleKaguyaLayer",
  Name = "Toggle Kaguya layer",
  Command = [[
    openspace.setPropertyValueSingle(
      "Scene.Moon.Renderable.Layers.ColorLayers.Kaguya_Utah.Enabled",
      not openspace.getPropertyValue("Scene.Moon.Renderable.Layers.ColorLayers.Kaguya_Utah.Enabled")
    )
  ]],
  Documentation = "Toggles Moon Kaguya color layer",
  GuiPath = "/Missions/Apollo",
  IsLocal = false
}

local DisableApolloSites = {
  Identifier = "os.apollo.DisableApolloSites",
  Name = "Disable Apollo sites",
  Command = [[
    openspace.setPropertyValue("Scene.Moon.Renderable.Layers.ColorLayers.A17_*.Enabled", false)
    openspace.setPropertyValueSingle("Scene.Moon.Renderable.Layers.HeightLayers.LRO_NAC_Apollo_11.Enabled", false)
    openspace.setPropertyValueSingle("Scene.Moon.Renderable.Layers.ColorLayers.A11_M177481212_p_longlat.Enabled", false)
    openspace.setPropertyValueSingle("Scene.Apollo11MoonTrail.Renderable.Enabled", false)
    openspace.setPropertyValueSingle("Scene.Apollo11LemTrail.Renderable.Enabled", false)
    openspace.setPropertyValueSingle("Scene.Moon.Renderable.Layers.HeightLayers.LRO_NAC_Apollo_17.Enabled", false)
  ]],
  Documentation = "Disable apollo site on moon when leaving",
  GuiPath = "/Missions/Apollo",
  IsLocal = false
}

asset.onInitialize(function()
  openspace.globebrowsing.addBlendingLayersFromDirectory(heightmaps, moon.Moon.Identifier)
  openspace.globebrowsing.addBlendingLayersFromDirectory(basemaps, moon.Moon.Identifier)
  openspace.globebrowsing.addBlendingLayersFromDirectory(naclighting, moon.Moon.Identifier)
  openspace.globebrowsing.addBlendingLayersFromDirectory(stations, moon.Moon.Identifier)

  openspace.action.registerAction(ToggleKaguyaLayer)
  openspace.action.registerAction(DisableApolloSites)
end)

asset.onDeinitialize(function()
  openspace.action.removeAction(DisableApolloSites)
  openspace.action.removeAction(ToggleKaguyaLayer)
end)
