local earthAsset = asset.require("scene/solarsystem/planets/earth/earth")



local AltAzGridPosition = {
  Identifier = "AltAzGridPosition",
  Parent = earthAsset.Earth.Identifier,
  Transform = {
    Translation = {
      Type = "GlobeTranslation",
      Globe = earthAsset.Earth.Identifier,
      Latitude = 0.0,
      Longitude = 0.0,
      Altitude = 0.0,
      UseHeightmap = false,
      UseCamera = true
    },
    Rotation = {
      Type = "GlobeRotation",
      Globe = earthAsset.Earth.Identifier,
      Latitude = 0.0,
      Longitude = 0.0,
      UseHeightmap = false,
      UseCamera = true
    }
  },
  GUI = {
    Name = "Altitude/Azimuth Grid Position",
    Path = "/Other/Grids",
    Hidden = true
  }
}

local AltAzGrid = {
  Identifier = "AltAzGrid",
  Parent = AltAzGridPosition.Identifier,
  Transform = {
    Scale = {
      Type = "StaticScale",
      Scale = 50000
    },
    Rotation = {
      Type = "StaticRotation",
      Rotation = { -math.pi / 2.0, 0.0, 0.0 }
    }
  },
  Renderable = {
    Type = "RenderableSphericalGrid",
    Enabled = asset.enabled,
    Opacity = 0.8,
    Color = { 0.4, 0.8, 0.4 },
    LineWidth = 2.0,
    RenderBinMode = "PostDeferredTransparent"
  },
  GUI = {
    Name = "Altitude/Azimuth Grid",
    Path = "/Other/Grids"
  }
}


local ShowAltaz = {
  Identifier = "os.nightsky.ShowAltaz",
  Name = "Show Alt/Az grid",
  Command = [[
    local camera = openspace.navigation.getNavigationState()
    openspace.setParent("AltAzGridPosition", camera.Anchor)
    openspace.setPropertyValueSingle("Scene.AltAzGridPosition.Translation.Globe", camera.Anchor)
    openspace.setPropertyValueSingle("Scene.AltAzGridPosition.Rotation.Globe", camera.Anchor)
    openspace.fadeIn("Scene.AltAzGrid.Renderable")
  ]],
  Documentation = "Shows a local Altitude/Azimuth grid centered around your position",
  GuiPath = "/Night Sky/Lines and Grids/Show and Hide",
  IsLocal = false
}

local HideAltaz = {
  Identifier = "os.nightsky.HideAltaz",
  Name = "Hide Alt/Az grid",
  Command = "openspace.fadeOut('Scene.AltAzGrid.Renderable')",
  Documentation = "Hides the local Altitude/Azimuth grid centered around your position",
  GuiPath = "/Night Sky/Lines and Grids/Show and Hide",
  IsLocal = false
}

local ToggleAltaz = {
  Identifier = "os.nightsky.ToggleAltaz",
  Name = "Toggle Alt/Az grid",
  Command = [[
    local camera = openspace.navigation.getNavigationState()
    openspace.setParent("AltAzGridPosition", camera.Anchor)
    openspace.setPropertyValueSingle("Scene.AltAzGridPosition.Translation.Globe", camera.Anchor)
    openspace.setPropertyValueSingle("Scene.AltAzGridPosition.Rotation.Globe", camera.Anchor)
    openspace.toggleFade("Scene.AltAzGrid.Renderable")
  ]],
  Documentation = "Toggles the local Altitude/Azimuth grid centered around your position",
  GuiPath = "/Night Sky/Lines and Grids",
  IsLocal = false
}

asset.onInitialize(function()
  openspace.addSceneGraphNode(AltAzGridPosition)
  openspace.addSceneGraphNode(AltAzGrid)
  openspace.action.registerAction(ShowAltaz)
  openspace.action.registerAction(HideAltaz)
  openspace.action.registerAction(ToggleAltaz)

end)

asset.onDeinitialize(function()
  openspace.action.removeAction(ToggleAltaz)
  openspace.action.removeAction(HideAltaz)
  openspace.action.removeAction(ShowAltaz)
  openspace.removeSceneGraphNode(AltAzGrid)
  openspace.removeSceneGraphNode(AltAzGridPosition)
end)

asset.export(AltAzGridPosition)
asset.export(AltAzGrid)
asset.export("ShowAltaz", ShowAltaz.Identifier)
asset.export("HideAltaz", HideAltaz.Identifier)
asset.export("ToggleAltaz", ToggleAltaz.Identifier)
