local mercury = asset.require("scene/solarsystem/planets/mercury/transforms")
local venus = asset.require("scene/solarsystem/planets/venus/transforms")
local mars = asset.require("scene/solarsystem/planets/mars/transforms")
local jupiter = asset.require("scene/solarsystem/planets/jupiter/transforms")
local saturn = asset.require("scene/solarsystem/planets/saturn/transforms")



local textures = asset.syncedResource({
  Name = "Night Sky Planet Textures",
  Type = "HttpSynchronization",
  Identifier = "night_sky_planet_textures",
  Version = 1
})


local Mercury = {
  Identifier = "NightSkyMercury",
  Parent = mercury.MercuryBarycenter.Identifier,
  Renderable = {
    Type = "RenderablePlaneImageLocal",
    Billboard = true,
    Enabled = asset.enabled,
    Size = 2439700 * 500,
    Texture = textures .. "glare.png",
    MultiplyColor = { 0.608, 0.604, 0.455 },
    DimInAtmosphere = true,
    RenderBinMode = "PostDeferredTransparent"
  },
  Tag = { "nightsky_billboard" },
  GUI = {
    Name = "Night Sky Mercury",
    Path = "/Other/Night Sky/Planets"
  }
}

local Venus = {
  Identifier = "NightSkyVenus",
  Parent = venus.VenusBarycenter.Identifier,
  Renderable = {
    Type = "RenderablePlaneImageLocal",
    Enabled = asset.enabled,
    Billboard = true,
    Size = 6051900 * 700,
    Texture = textures .. "glare.png",
    MultiplyColor = { 1.0 , 0.992, 0.757 },
    DimInAtmosphere = true,
    RenderBinMode = "PostDeferredTransparent"
  },
  Tag = { "nightsky_billboard" },
  GUI = {
    Name = "Night Sky Venus",
    Path = "/Other/Night Sky/Planets"
  }
}

local Mars = {
  Identifier = "NightSkyMars",
  Parent = mars.MarsBarycenter.Identifier,
  Renderable = {
    Type = "RenderablePlaneImageLocal",
    Enabled = asset.enabled,
    Billboard = true,
    Size = 3396190 * 1000,
    Texture = textures .. "glare.png",
    MultiplyColor = { 0.756, 0.267, 0.054 },
    DimInAtmosphere = true,
    RenderBinMode = "PostDeferredTransparent"
  },
  Tag = { "nightsky_billboard" },
  GUI = {
    Name = "Night Sky Mars",
    Path = "/Other/Night Sky/Planets"
  }
}

local Jupiter = {
  Identifier = "NightSkyJupiter",
  Parent = jupiter.JupiterBarycenter.Identifier,
  Renderable = {
    Type = "RenderablePlaneImageLocal",
    Enabled = asset.enabled,
    Billboard = true,
    Size = 71492000 * 400,
    Texture = textures .. "glare.png",
    MultiplyColor = { 0.608, 0.604, 0.455 },
    DimInAtmosphere = true,
    RenderBinMode = "PostDeferredTransparent"
  },
  Tag = { "nightsky_billboard" },
  GUI = {
    Name = "Night Sky Jupiter",
    Path = "/Other/Night Sky/Planets"
  }
}

local Saturn = {
  Identifier = "NightSkySaturn",
  Parent = saturn.SaturnBarycenter.Identifier,
  Renderable = {
    Type = "RenderablePlaneImageLocal",
    Enabled = asset.enabled,
    Billboard = true,
    Size = 60268000 * 500,
    Texture = textures .. "glare.png",
    MultiplyColor = { 0.608, 0.604, 0.455 },
    DimInAtmosphere = true,
    RenderBinMode = "PostDeferredTransparent"
  },
  Tag = { "nightsky_billboard" },
  GUI = {
    Name = "Night Sky Saturn",
    Path = "/Other/Night Sky/Planets"
  }
}

local ShowNightSkyPlanets = {
  Identifier = "os.nightsky.ShowNightSkyPlanets",
  Name = "Show night sky planets",
  Command = [[
    openspace.fadeIn("Scene.NightSkyMercury.Renderable")
    openspace.fadeIn("Scene.NightSkyVenus.Renderable")
    openspace.fadeIn("Scene.NightSkyMars.Renderable")
    openspace.fadeIn("Scene.NightSkyJupiter.Renderable")
    openspace.fadeIn("Scene.NightSkySaturn.Renderable")
    openspace.setPropertyValueSingle("Scene.Moon.Scale.Scale", 2)
  ]],
  Documentation = "Show night sky versions of the planets (Note: Increases the scale of the Moon)",
  GuiPath = "/Night Sky/Planets",
  IsLocal = false
}

local HideNightSkyPlanets = {
  Identifier = "os.nightsky.HideNightSkyPlanets",
  Name = "Hide night sky planets",
  Command = [[
    openspace.fadeOut("Scene.NightSkyMercury.Renderable")
    openspace.fadeOut("Scene.NightSkyVenus.Renderable")
    openspace.fadeOut("Scene.NightSkyMars.Renderable")
    openspace.fadeOut("Scene.NightSkyJupiter.Renderable")
    openspace.fadeOut("Scene.NightSkySaturn.Renderable")
    openspace.setPropertyValueSingle("Scene.Moon.Scale.Scale", 1)
  ]],
  Documentation = "Hides night sky versions of the planets",
  GuiPath = "/Night Sky/Planets",
  IsLocal = false
}


local ToggleNightSkyPlanets = {
  Identifier = "os.nightsky.ToggleNightSkyPlanets",
  Name = "Toggle night sky planets",
  Command = [[
    openspace.toggleFade("Scene.NightSkyMercury.Renderable")
    openspace.toggleFade("Scene.NightSkyVenus.Renderable")
    openspace.toggleFade("Scene.NightSkyMars.Renderable")
    openspace.toggleFade("Scene.NightSkyJupiter.Renderable")
    openspace.toggleFade("Scene.NightSkySaturn.Renderable")
    local scale = openspace.getPropertyValue("Scene.Moon.Scale.Scale")
    if (scale > 1) then
      openspace.setPropertyValueSingle("Scene.Moon.Scale.Scale", 1.0)
    else
      openspace.setPropertyValueSingle("Scene.Moon.Scale.Scale", 2.0)
    end
  ]],
  Documentation = "Toggles visibility of the night sky versions of the planets (Note: Increases the scale of the Moon)",
  GuiPath = "/Night Sky/Planets",
  IsLocal = false
}

asset.onInitialize(function()
  openspace.addSceneGraphNode(Mercury)
  openspace.addSceneGraphNode(Venus)
  openspace.addSceneGraphNode(Mars)
  openspace.addSceneGraphNode(Jupiter)
  openspace.addSceneGraphNode(Saturn)
  openspace.action.registerAction(ShowNightSkyPlanets)
  openspace.action.registerAction(HideNightSkyPlanets)
  openspace.action.registerAction(ToggleNightSkyPlanets)
end)

asset.onDeinitialize(function()
  openspace.action.removeAction(ToggleNightSkyPlanets)
  openspace.action.removeAction(HideNightSkyPlanets)
  openspace.action.removeAction(ShowNightSkyPlanets)
  openspace.removeSceneGraphNode(Saturn)
  openspace.removeSceneGraphNode(Jupiter)
  openspace.removeSceneGraphNode(Mars)
  openspace.removeSceneGraphNode(Venus)
  openspace.removeSceneGraphNode(Mercury)
end)

asset.export(Mercury)
asset.export(Venus)
asset.export(Mars)
asset.export(Jupiter)
asset.export(Saturn)
asset.export("ShowNightSkyPlanets", ShowNightSkyPlanets.Identifier)
asset.export("HideNightSkyPlanets", HideNightSkyPlanets.Identifier)
asset.export("ToggleNightSkyPlanets", ToggleNightSkyPlanets.Identifier)
