local transforms = asset.require("./transforms")
local kernel = asset.require("./kernels")



local Miranda = {
  Identifier = "Miranda",
  Parent = transforms.UranusBarycenter.Identifier,
  Transform = {
    Translation = {
      Type = "SpiceTranslation",
      Target = "MIRANDA",
      Observer = "URANUS BARYCENTER"
    }
  },
  Renderable = {
    Type = "RenderableGlobe",
    Radii = { 471600, 468000, 466000 },
    SegmentsPerPatch = 64,
    Layers = {}
  },
  Tag = { "moon_solarSystem", "moon_giants", "moon_uranus", "moon_major", "moon_major_uranus" },
  GUI = {
    Name = "Miranda",
    Path = "/Solar System/Planets/Uranus/Major Moons/Miranda"
  }
}

local MirandaTrail = {
  Identifier = "MirandaTrail",
  Parent = transforms.UranusBarycenter.Identifier,
  Renderable = {
    Type = "RenderableTrailOrbit",
    Translation = {
      Type = "SpiceTranslation",
      Target = "MIRANDA",
      Observer = "URANUS BARYCENTER"
    },
    Color = { 0.60, 0.65, 0.84 },
    Period = 1.413479,
    Resolution = 1000
  },
  Tag = { "moonTrail_solarSystem", "moonTrail_giants", "moonTrail_uranus", "moonTrail_major_uranus" },
  GUI = {
    Name = "Miranda Trail",
    Path = "/Solar System/Planets/Uranus/Major Moons/Miranda"
  }
}

local MirandaLabel = {
  Identifier = "MirandaLabel",
  Parent = Miranda.Identifier,
  Renderable = {
    Enabled = false,
    Type = "RenderableLabel",
    Text = "Miranda",
    FontSize = 70.0,
    Size = 7.4,
    MinMaxSize = { 1, 25 },
    OrientationOption = "Camera View Direction",
    BlendMode = "Additive",
    EnableFading = true,
    FadeUnit = "Gm",
    FadeDistances = { 1, 250.0 },
    FadeWidths = { 1, 250.0 }
  },
  Tag = { "solarsystem_labels", "moon_labels", "major_moon_labels" },
  GUI = {
    Name = "Miranda Label",
    Path = "/Solar System/Planets/Uranus/Major Moons/Miranda",
    Description = "Label for Uranus' moon Miranda"
  }
}

local Ariel = {
  Identifier = "Ariel",
  Parent = transforms.UranusBarycenter.Identifier,
  Transform = {
    Translation = {
      Type = "SpiceTranslation",
      Target = "ARIEL",
      Observer = "URANUS BARYCENTER"
    }
  },
  Renderable = {
    Type = "RenderableGlobe",
    Radii = { 1162000, 1156000, 1155000 },
    SegmentsPerPatch = 64,
    Layers = {}
  },
  Tag = { "moon_solarSystem", "moon_giants", "moon_uranus", "moon_major", "moon_major_uranus" },
  GUI = {
    Name = "Ariel",
    Path = "/Solar System/Planets/Uranus/Major Moons/Ariel"
  }
}

local ArielTrail = {
  Identifier = "ArielTrail",
  Parent = transforms.UranusBarycenter.Identifier,
  Renderable = {
    Type = "RenderableTrailOrbit",
    Translation = {
      Type = "SpiceTranslation",
      Target = "ARIEL",
      Observer = "URANUS BARYCENTER"
    },
    Color = { 0.60, 0.65, 0.84 },
    Period = 2.520379,
    Resolution = 1000
  },
  Tag = { "moonTrail_solarSystem", "moonTrail_giants", "moonTrail_uranus", "moonTrail_major_uranus" },
  GUI = {
    Name = "Ariel Trail",
    Path = "/Solar System/Planets/Uranus/Major Moons/Ariel"
  }
}

local ArielLabel = {
  Identifier = "ArielLabel",
  Parent = Ariel.Identifier,
  Renderable = {
    Enabled = false,
    Type = "RenderableLabel",
    Text = "Ariel",
    FontSize = 70.0,
    Size = 7.4,
    MinMaxSize = { 1, 25 },
    OrientationOption = "Camera View Direction",
    BlendMode = "Additive",
    EnableFading = true,
    FadeUnit = "Gm",
    FadeDistances = { 1, 250.0 },
    FadeWidths = { 1, 250.0 }
  },
  Tag = { "solarsystem_labels", "moon_labels", "major_moon_labels" },
  GUI = {
    Name = "Ariel Label",
    Path = "/Solar System/Planets/Uranus/Major Moons/Ariel",
    Description = "Label for Uranus' moon Ariel"
  }
}

local Umbriel = {
  Identifier = "Umbriel",
  Parent = transforms.UranusBarycenter.Identifier,
  Transform = {
    Translation = {
      Type = "SpiceTranslation",
      Target = "UMBRIEL",
      Observer = "URANUS BARYCENTER"
    }
  },
  Renderable = {
    Type = "RenderableGlobe",
    Radii = { 1169400, 1169400, 1169400 },
    SegmentsPerPatch = 64,
    Layers = {}
  },
  Tag = { "moon_solarSystem", "moon_giants", "moon_uranus", "moon_major", "moon_major_uranus" },
  GUI = {
    Name = "Umbriel",
    Path = "/Solar System/Planets/Uranus/Major Moons/Umbriel"
  }
}

local UmbrielTrail = {
  Identifier = "UmbrielTrail",
  Parent = transforms.UranusBarycenter.Identifier,
  Renderable = {
    Type = "RenderableTrailOrbit",
    Translation = {
      Type = "SpiceTranslation",
      Target = "UMBRIEL",
      Observer = "URANUS BARYCENTER"
    },
    Color = { 0.60, 0.65, 0.84 },
    Period = 4.144177,
    Resolution = 1000
  },
  Tag = { "moonTrail_solarSystem", "moonTrail_giants", "moonTrail_uranus", "moonTrail_major_uranus" },
  GUI = {
    Name = "Umbriel Trail",
    Path = "/Solar System/Planets/Uranus/Major Moons/Umbriel"
  }
}

local UmbrielLabel = {
  Identifier = "UmbrielLabel",
  Parent = Umbriel.Identifier,
  Renderable = {
    Enabled = false,
    Type = "RenderableLabel",
    Text = "Umbriel",
    FontSize = 70.0,
    Size = 7.4,
    MinMaxSize = { 1, 25 },
    OrientationOption = "Camera View Direction",
    BlendMode = "Additive",
    EnableFading = true,
    FadeUnit = "Gm",
    FadeDistances = { 1, 250.0 },
    FadeWidths = { 1, 250.0 }
  },
  Tag = { "solarsystem_labels", "moon_labels", "major_moon_labels" },
  GUI = {
    Name = "Umbriel Label",
    Path = "/Solar System/Planets/Uranus/Major Moons/Umbriel",
    Description = "Label for Uranus' moon Umbriel"
  }
}

local Titania = {
  Identifier = "Titania",
  Parent = transforms.UranusBarycenter.Identifier,
  Transform = {
    Translation = {
      Type = "SpiceTranslation",
      Target = "TITANIA",
      Observer = "URANUS BARYCENTER"
    }
  },
  Renderable = {
    Type = "RenderableGlobe",
    Radii = { 1576800, 1576800, 1576800 },
    SegmentsPerPatch = 64,
    Layers = {}
  },
  Tag = { "moon_solarSystem", "moon_giants", "moon_uranus", "moon_major", "moon_major_uranus" },
  GUI = {
    Name = "Titania",
    Path = "/Solar System/Planets/Uranus/Major Moons/Titania"
  }
}

local TitaniaTrail = {
  Identifier = "TitaniaTrail",
  Parent = transforms.UranusBarycenter.Identifier,
  Renderable = {
    Type = "RenderableTrailOrbit",
    Translation = {
      Type = "SpiceTranslation",
      Target = "TITANIA",
      Observer = "URANUS BARYCENTER"
    },
    Color = { 0.60, 0.65, 0.84 },
    Period = 8.705872,
    Resolution = 1000
  },
  Tag = { "moonTrail_solarSystem", "moonTrail_giants", "moonTrail_uranus", "moonTrail_major_uranus" },
  GUI = {
    Name = "Titania Trail",
    Path = "/Solar System/Planets/Uranus/Major Moons/Titania"
  }
}

local TitaniaLabel = {
  Identifier = "TitaniaLabel",
  Parent = Titania.Identifier,
  Renderable = {
    Enabled = false,
    Type = "RenderableLabel",
    Text = "Titania",
    FontSize = 70.0,
    Size = 7.4,
    MinMaxSize = { 1, 25 },
    OrientationOption = "Camera View Direction",
    BlendMode = "Additive",
    EnableFading = true,
    FadeUnit = "Gm",
    FadeDistances = { 1, 250.0 },
    FadeWidths = { 1, 250.0 }
  },
  Tag = { "solarsystem_labels", "moon_labels", "major_moon_labels" },
  GUI = {
    Name = "Titania Label",
    Path = "/Solar System/Planets/Uranus/Major Moons/Titania",
    Description = "Label for Uranus' moon Titania"
  }
}

local Oberon = {
  Identifier = "Oberon",
  Parent = transforms.UranusBarycenter.Identifier,
  Transform = {
    Translation = {
      Type = "SpiceTranslation",
      Target = "OBERON",
      Observer = "URANUS BARYCENTER"
    }
  },
  Renderable = {
    Type = "RenderableGlobe",
    Radii = { 1522800, 1522800, 1522800 },
    SegmentsPerPatch = 64,
    Layers = {}
  },
  Tag = { "moon_solarSystem", "moon_giants", "moon_uranus", "moon_major", "moon_major_uranus" },
  GUI = {
    Name = "Oberon",
    Path = "/Solar System/Planets/Uranus/Major Moons/Oberon"
  }
}

local OberonTrail = {
  Identifier = "OberonTrail",
  Parent = transforms.UranusBarycenter.Identifier,
  Renderable = {
    Type = "RenderableTrailOrbit",
    Translation = {
      Type = "SpiceTranslation",
      Target = "OBERON",
      Observer = "URANUS BARYCENTER"
    },
    Color = { 0.60, 0.65, 0.84 },
    Period = 13.463239,
    Resolution = 1000
  },
  Tag = { "moonTrail_solarSystem", "moonTrail_giants", "moonTrail_uranus", "moonTrail_major_uranus" },
  GUI = {
    Name = "Oberon Trail",
    Path = "/Solar System/Planets/Uranus/Major Moons/Oberon"
  }
}

local OberonLabel = {
  Identifier = "OberonLabel",
  Parent = Oberon.Identifier,
  Renderable = {
    Enabled = false,
    Type = "RenderableLabel",
    Text = "Oberon",
    FontSize = 70.0,
    Size = 7.4,
    MinMaxSize = { 1, 25 },
    OrientationOption = "Camera View Direction",
    BlendMode = "Additive",
    EnableFading = true,
    FadeUnit = "Gm",
    FadeDistances = { 1, 250.0 },
    FadeWidths = { 1, 250.0 }
  },
  Tag = { "solarsystem_labels", "moon_labels", "major_moon_labels" },
  GUI = {
    Name = "Oberon Label",
    Path = "/Solar System/Planets/Uranus/Major Moons/Oberon",
    Description = "Label for Uranus' moon Oberon"
  }
}


local UranusMajorMoonsOn = {
  Identifier = "os.solarsystem.UranusMajorMoonsOn",
  Name = "Turn ON major moons and trails",
  Command = [[
    local trails = openspace.getProperty("{moonTrail_major_uranus}.Renderable.Enabled")
    local trails_fade = openspace.getProperty("{moonTrail_major_uranus}.Renderable.Fade")

    local moons = openspace.getProperty("{moon_major_uranus}.Renderable.Enabled")
    local moons_fade = openspace.getProperty("{moon_major_uranus}.Renderable.Fade")

    for i, v in pairs(trails_fade) do
      openspace.setPropertyValueSingle(trails[i], true)
      openspace.setPropertyValueSingle(v, 1, 2, "Linear")
    end

    for i, v in pairs(moons_fade) do
      openspace.setPropertyValueSingle(moons[i], true)
      openspace.setPropertyValueSingle(v, 1, 2, "Linear")
    end
  ]],
  Documentation = "Turn ON Uranus's major moons and their trails",
  GuiPath = "/Solar System/Uranus",
  IsLocal = true
}

local UranusMajorMoonsOff = {
  Identifier = "os.solarsystem.UranusMajorMoonsOff",
  Name = "Turn OFF majors moon and trails",
  Command = [[
    local trails = openspace.getProperty("{moonTrail_major_uranus}.Renderable.Enabled")
    local trails_fade = openspace.getProperty("{moonTrail_major_uranus}.Renderable.Fade")

    local moons = openspace.getProperty("{moon_major_uranus}.Renderable.Enabled")
    local moons_fade = openspace.getProperty("{moon_major_uranus}.Renderable.Fade")

    for i, v in pairs(trails_fade) do
      openspace.setPropertyValueSingle(
        v,
        0,
        2,
        "Linear",
        "openspace.setPropertyValueSingle('" .. trails[i] .. "', false)"
      )
    end

    for i, v in pairs(moons_fade) do
      openspace.setPropertyValueSingle(
        v,
        0,
        2,
        "Linear",
        "openspace.setPropertyValueSingle('" .. moons[i] .. "', false)"
      )
    end
  ]],
  Documentation = "Turn OFF Uranus's major moons and their trails",
  GuiPath = "/Solar System/Uranus",
  IsLocal = true
}


asset.onInitialize(function()
  openspace.spice.loadKernel(kernel.ura111)


  openspace.addSceneGraphNode(Miranda)
  openspace.addSceneGraphNode(MirandaTrail)
  openspace.addSceneGraphNode(MirandaLabel)

  openspace.addSceneGraphNode(Ariel)
  openspace.addSceneGraphNode(ArielTrail)
  openspace.addSceneGraphNode(ArielLabel)

  openspace.addSceneGraphNode(Umbriel)
  openspace.addSceneGraphNode(UmbrielTrail)
  openspace.addSceneGraphNode(UmbrielLabel)

  openspace.addSceneGraphNode(Titania)
  openspace.addSceneGraphNode(TitaniaTrail)
  openspace.addSceneGraphNode(TitaniaLabel)

  openspace.addSceneGraphNode(Oberon)
  openspace.addSceneGraphNode(OberonTrail)
  openspace.addSceneGraphNode(OberonLabel)

  openspace.action.registerAction(UranusMajorMoonsOn)
  openspace.action.registerAction(UranusMajorMoonsOff)
end)

asset.onDeinitialize(function()
  openspace.action.removeAction(UranusMajorMoonsOff)
  openspace.action.removeAction(UranusMajorMoonsOn)

  openspace.removeSceneGraphNode(MirandaLabel)
  openspace.removeSceneGraphNode(MirandaTrail)
  openspace.removeSceneGraphNode(Miranda)

  openspace.removeSceneGraphNode(ArielLabel)
  openspace.removeSceneGraphNode(ArielTrail)
  openspace.removeSceneGraphNode(Ariel)

  openspace.removeSceneGraphNode(UmbrielLabel)
  openspace.removeSceneGraphNode(UmbrielTrail)
  openspace.removeSceneGraphNode(Umbriel)

  openspace.removeSceneGraphNode(TitaniaLabel)
  openspace.removeSceneGraphNode(TitaniaTrail)
  openspace.removeSceneGraphNode(Titania)

  openspace.removeSceneGraphNode(OberonLabel)
  openspace.removeSceneGraphNode(OberonTrail)
  openspace.removeSceneGraphNode(Oberon)


  openspace.spice.unloadKernel(kernel.ura111)
end)

asset.export(Miranda)
asset.export(MirandaTrail)
asset.export(MirandaLabel)
asset.export(Ariel)
asset.export(ArielTrail)
asset.export(ArielLabel)
asset.export(Umbriel)
asset.export(UmbrielTrail)
asset.export(UmbrielLabel)
asset.export(Titania)
asset.export(TitaniaTrail)
asset.export(TitaniaLabel)
asset.export(Oberon)
asset.export(OberonTrail)
asset.export(OberonLabel)
asset.export("UranusMajorMoonsOn", UranusMajorMoonsOn.Identifier)
asset.export("UranusMajorMoonsOff", UranusMajorMoonsOff.Identifier)


asset.meta = {
  Name = "Uranus Marjor Moons",
  Version = "1.0",
  Description = [[Procedural Globe asset containing Uranus' major moons: Miranda, Ariel,
    Umbriel, Titania, and Oberon. Blank globes and SPICE trails are generated for each
    moon]],
  Author = "OpenSpace Team",
  URL = "http://openspaceproject.com",
  License = "MIT license"
}
