local function scaleCommand(identifier, scale, speed)
  local command = [[openspace.setPropertyValue("]] .. identifier .. [[.Scale.Scale", ]]
  command = command .. scale .. ", " .. speed
  if (scale == 1) then
    command = command .. ")"
  else
    command = command .. [[, "CubicEaseOut")]]
  end
  return command
end

local function scaleAction(identifier, scale, name, speed)
  local actionString = "Grow"
  if (scale == 1) then
    actionString = "Shrink"
  end
  return {
    Identifier = "os." .. actionString .. "_" .. identifier,
    Name = actionString .. " " .. name,
    Command = scaleCommand(identifier, scale, speed),
    Documentation = "Scales " .. name .. " to " .. scale .. "x",
    GuiPath = "/Solar System/Scale",
    IsLocal = true
  }
end

local function toggleScaleAction(identifier, scale, name, speedup, speeddown)
  return {
    Identifier = "os.toggle_" .. string.gsub(name, "%s+", "") .. "_scale",
    Name = "Toggle " .. name .. " scale",
    Command = [[
local list = openspace.getProperty("]] .. identifier .. [[.Scale.Scale")
if #list == 0 then
  openspace.printWarning("No planets to resize")
else
  local prop = list[1]
  local currentScale = openspace.getPropertyValue(prop)
  local newScale = 1
  if (currentScale == 1) then
    ]] .. scaleCommand(identifier, scale, speedup) .. [[
  else
    ]] .. scaleCommand(identifier, 1, speeddown) .. [[
  end
end
]],
    GuiPath = "/Solar System/Scale",
    Documentation = "Toggle the scale of " .. name,
    IsLocal = true
  }
end

local GrowPlanets = scaleAction("{planet_solarSystem}", 3400, "Planets", 3)
local ShrinkPlanets = scaleAction("{planet_solarSystem}", 1, "Planets", 2)
local TogglePlanetScale = toggleScaleAction("{planet_solarSystem}", 3400, "Planets", 3, 2)

local GrowInnerPlanets = scaleAction("{planet_terrestrial}", 3400, "Inner Planets", 3)
local ShrinkInnerPlanets = scaleAction("{planet_terrestrial}", 1, "Inner Planets", 2)
local ToggleInnerPlanetScale = toggleScaleAction("{planet_terrestrial}", 3400, "Inner Planets", 3, 2)

local GrowOuterPlanets = scaleAction("{planet_giants}", 3400, "Outer Planets", 3)
local ShrinkOuterPlanets = scaleAction("{planet_giants}", 1, "Outer Planets", 2)
local ToggleOuterPlanetScale = toggleScaleAction("{planet_giants}", 3400, "Outer Planets", 3, 2)

local GrowJupiterMoons = scaleAction("{moon_jupiter}", 50, "Jupiter Moons", 3)
local ShrinkJupiterMoons = scaleAction("{moon_jupiter}", 1, "Jupiter Moons", 2)
local ToggleJupiterMoonScales = toggleScaleAction("{moon_jupiter}", 50, "Jupiter Moons", 3, 2)

local GrowMoon = scaleAction("Scene.Moon", 25, "Moon", 3)
local ShrinkMoon = scaleAction("Scene.Moon", 1, "Moon", 2)
local ToggleMoonScale = toggleScaleAction("Scene.Moon", 25, "Moon", 3, 2)


asset.onInitialize(function()
  openspace.action.registerAction(GrowPlanets)
  openspace.action.registerAction(ShrinkPlanets)
  openspace.action.registerAction(TogglePlanetScale)

  openspace.action.registerAction(GrowInnerPlanets)
  openspace.action.registerAction(ShrinkInnerPlanets)
  openspace.action.registerAction(ToggleInnerPlanetScale)

  openspace.action.registerAction(GrowOuterPlanets)
  openspace.action.registerAction(ShrinkOuterPlanets)
  openspace.action.registerAction(ToggleOuterPlanetScale)

  openspace.action.registerAction(GrowJupiterMoons)
  openspace.action.registerAction(ShrinkJupiterMoons)
  openspace.action.registerAction(ToggleJupiterMoonScales)

  openspace.action.registerAction(GrowMoon)
  openspace.action.registerAction(ShrinkMoon)
  openspace.action.registerAction(ToggleMoonScale)
end)

asset.onDeinitialize(function()
  openspace.action.removeAction(ToggleMoonScale)
  openspace.action.removeAction(ShrinkMoon)
  openspace.action.removeAction(GrowMoon)

  openspace.action.removeAction(ToggleJupiterMoonScales)
  openspace.action.removeAction(ShrinkJupiterMoons)
  openspace.action.removeAction(GrowJupiterMoons)

  openspace.action.removeAction(ToggleOuterPlanetScale)
  openspace.action.removeAction(ShrinkOuterPlanets)
  openspace.action.removeAction(GrowOuterPlanets)

  openspace.action.removeAction(ToggleInnerPlanetScale)
  openspace.action.removeAction(ShrinkInnerPlanets)
  openspace.action.removeAction(GrowInnerPlanets)

  openspace.action.removeAction(TogglePlanetScale)
  openspace.action.removeAction(ShrinkPlanets)
  openspace.action.removeAction(GrowPlanets)
end)

asset.export("GrowPlanets", GrowPlanets.Identifier)
asset.export("ShrinkPlanets", ShrinkPlanets.Identifier)
asset.export("TogglePlanetScale", TogglePlanetScale.Identifier)
asset.export("GrowInnerPlanets", GrowInnerPlanets.Identifier)
asset.export("ShrinkInnerPlanets", ShrinkInnerPlanets.Identifier)
asset.export("ToggleInnerPlanetScale", ToggleInnerPlanetScale.Identifier)
asset.export("GrowOuterPlanets", GrowOuterPlanets.Identifier)
asset.export("ShrinkOuterPlanets", ShrinkOuterPlanets.Identifier)
asset.export("ToggleOuterPlanetScale", ToggleOuterPlanetScale.Identifier)
asset.export("GrowJupiterMoons", GrowJupiterMoons.Identifiers)
asset.export("ShrinkJupiterMoons", ShrinkJupiterMoons.Identifier)
asset.export("ToggleJupiterMoonScales", ToggleJupiterMoonScales.Identifier)
asset.export("GrowMoon", GrowMoon.Identifier)
asset.export("ShrinkMoon", ShrinkMoon.Identifier)
asset.export("ToggleMoonScale", ToggleMoonScale.Identifier)
