local function getIlluminationCommand(node, global)
  local commandString = "local node = \"" .. node .. "\"\n"
  if (node == "Current Focus") then 
    commandString = "local node = openspace.navigation.getNavigationState().Anchor\n"
  end
  if (global) then
    commandString = commandString .. [[
if (openspace.hasProperty("Scene."..node..".Renderable.UseAccurateNormals")) then    
  local list = openspace.getProperty("Scene." .. node .. ".Renderable.Layers.NightLayers.*.Enabled")
  if (#list > 0) then
    openspace.setPropertyValue("Scene." .. node .. ".Renderable.Layers.NightLayers.*.Enabled", false)
  else
    openspace.setPropertyValueSingle("Scene." .. node .. ".Renderable.PerformShading", false) 
  end
  if openspace.hasSceneGraphNode(node .. "Atmosphere") then
    openspace.setPropertyValueSingle("Scene." .. node .. "Atmosphere.Renderable.SunFollowingCamera", true)
  end
  if openspace.hasProperty("Scene." .. node .. ".Renderable.ShadowsComponent.DistanceFraction") then
    openspace.setPropertyValueSingle("Scene." .. node .. ".Renderable.ShadowsComponent.DistanceFraction", 3000)
  end
else
  openspace.printWarning("Illumination action only available for globes")
end]]
  else
    --todo @micahnyc this 40 wont do once we have more rings
    commandString = commandString .. [[if (openspace.hasProperty("Scene."..node..".Renderable.UseAccurateNormals")) then
local list = openspace.getProperty("Scene." .. node .. ".Renderable.Layers.NightLayers.*.Enabled")
if (#list > 0) then
  openspace.setPropertyValue(list[1], true)
else
  openspace.setPropertyValueSingle("Scene." .. node .. ".Renderable.PerformShading", true) 
end
if openspace.hasSceneGraphNode(node .. "Atmosphere") then
  openspace.setPropertyValueSingle("Scene." .. node .. "Atmosphere.Renderable.SunFollowingCamera", false)
end
if openspace.hasProperty("Scene." .. node .. ".Renderable.ShadowsComponent.DistanceFraction") then
  openspace.setPropertyValueSingle("Scene." .. node .. ".Renderable.ShadowsComponent.DistanceFraction", 40)
end
else
  openspace.printWarning("Illumination action only available for globes")
end]]
  end
  return commandString
end

local function getIlluminationAction(node, node_lower, global)
  local actionString = "_standard_illumination"
  local actionName = "Standard Illumination"
  local actionCommand = getIlluminationCommand(node, global)
  if (global) then
    actionString = "_global_illumination"
    actionName = "Global Illumination"
    actionCommand = getIlluminationCommand(node, global)
  end

  local action = {
    Identifier = "os." .. string.gsub(node_lower, "%s+", "") .. actionString,
    Name = node .. " " .. actionName,
    Command = actionCommand,
    Documentation = "Enables " .. string.lower(actionName) .. " for" .. node,
    GuiPath = "/Solar System/" .. node,
    IsLocal = true
  }
  return action
end

local current_focus_global = getIlluminationAction("Current Focus", "current_focus", true)
asset.export("current_focus_global", current_focus_global.Identifier)
local current_focus_standard = getIlluminationAction("Current Focus", "current_focus", false)
asset.export("current_focus_standard", current_focus_standard.Identifier)

local earth_global = getIlluminationAction("Earth", "earth", true)
asset.export("earth_global", earth_global.Identifier)
local earth_standard = getIlluminationAction("Earth", "earth", false)
asset.export("earth_standard", earth_standard.Identifier)

local mars_global = getIlluminationAction("Mars", "mars", true)
asset.export("mars_global", mars_global.Identifier)
local mars_standard = getIlluminationAction("Mars", "mars", false)
asset.export("mars_standard", mars_standard.Identifier)

local venus_global = getIlluminationAction("Venus", "venus", true)
asset.export("venus_global", venus_global.Identifier)
local venus_standard = getIlluminationAction("Venus", "venus", false)
asset.export("venus_standard", venus_standard.Identifier)

local titan_global = getIlluminationAction("Titan", "titan", true)
asset.export("titan_global", titan_global.Identifier)
local titan_standard = getIlluminationAction("Titan", "titan", false)
asset.export("titan_standard", titan_standard.Identifier)

local saturn_global = getIlluminationAction("Saturn", "saturn", true)
asset.export("saturn_global", saturn_global.Identifier)
local saturn_standard = getIlluminationAction("Saturn", "saturn", false)
asset.export("titan_standard", saturn_standard.Identifier)


asset.onInitialize(function()
  openspace.action.registerAction(current_focus_global)
  openspace.action.registerAction(current_focus_standard)
  openspace.action.registerAction(earth_global)
  openspace.action.registerAction(earth_standard)
  openspace.action.registerAction(mars_global)
  openspace.action.registerAction(mars_standard)
  openspace.action.registerAction(venus_global)
  openspace.action.registerAction(venus_standard)
  openspace.action.registerAction(titan_global)
  openspace.action.registerAction(titan_standard)
  openspace.action.registerAction(saturn_global)
  openspace.action.registerAction(saturn_standard)
end)

asset.onDeinitialize(function()
  openspace.action.removeAction(saturn_standard)
  openspace.action.removeAction(saturn_global)
  openspace.action.removeAction(titan_standard)
  openspace.action.removeAction(titan_global)
  openspace.action.removeAction(venus_standard)
  openspace.action.removeAction(venus_global)
  openspace.action.removeAction(mars_standard)
  openspace.action.removeAction(mars_global)
  openspace.action.removeAction(earth_standard)
  openspace.action.removeAction(earth_global)
  openspace.action.removeAction(current_focus_standard)
  openspace.action.removeAction(current_focus_global)
end)
