local transforms = asset.require("scene/solarsystem/sun/transforms")



local textures = asset.syncedResource({
  Name = "Ecliptic Band Textures",
  Type = "HttpSynchronization",
  Identifier = "ecliptic_band_textures",
  Version = 2
})


local EclipticRotationMatrix = {
    -0.05487554,  0.4941095, -0.8676661,
    -0.9938214,  -0.1109906, -0.0003515167,
    -0.09647644,  0.8622859,  0.4971472
}

local EclipticLine = {
  Identifier = "EclipticLine",
  Parent = transforms.SolarSystemBarycenter.Name,
  Transform = {
    Scale = {
      Type = "StaticScale",
      Scale = 4.28601E17
    },
    Rotation = {
      Type = "StaticRotation",
      Rotation = EclipticRotationMatrix
    }
  },
  Renderable = {
    Type = "RenderableRadialGrid",
    Opacity = 0.8,
    Color = { 1.0, 1.0, 1.0 },
    LineWidth = 3.0,
    GridSegments = { 1, 1 },
    Radii = { 0.5, 0.5 },
    Enabled = asset.enabled
  },
  GUI = {
    Name = "Ecliptic Line",
    Path = "/Other/Lines"
  }
}

local EclipticBand = {
  Identifier = "EclipticBand",
  Parent = transforms.SolarSystemBarycenter.Name,
  Transform = {
    Rotation = {
      Type = "StaticRotation",
      Rotation = EclipticRotationMatrix
    }
  },
  Renderable = {
    Type = "RenderableSphere",
    Texture = textures .. "band2x.png",
    Size = 4.28601E17,
    Segments = 50,
    DisableFadeInOut = true,
    Orientation = "Inside",
    Opacity = 0.05,
    Enabled = asset.enabled
  },
  GUI = {
    Name = "Ecliptic Band",
    Path = "/Other/Lines"
  }
}

local ShowEclipticLine = {
  Identifier = "os.nightsky.ShowEclipticLine",
  Name = "Show ecliptic line",
  Command = "openspace.fadeIn('Scene.EclipticLine.Renderable')",
  Documentation = "Shows the ecliptic line",
  GuiPath = "/Night Sky/Lines and Grids/Show and Hide",
  IsLocal = false
}

local HideEclipticLine = {
  Identifier = "os.nightsky.HideEclipticLine",
  Name = "Hide ecliptic line",
  Command = "openspace.fadeOut('Scene.EclipticLine.Renderable')",
  Documentation = "Hides the ecliptic line",
  GuiPath = "/Night Sky/Lines and Grids/Show and Hide",
  IsLocal = false
}

local ToggleEclipticLine = {
  Identifier = "os.nightsky.ToggleEclipticLine",
  Name = "Toggle ecliptic line",
  Command = "openspace.toggleFade('Scene.EclipticLine.Renderable')",
  Documentation = "Toggles the ecliptic line visibilty",
  GuiPath = "/Night Sky/Lines and Grids",
  IsLocal = false
}

local ShowEclipticBand = {
  Identifier = "os.nightsky.ShowEclipticBand",
  Name = "Show ecliptic band",
  Command = [[
    openspace.fadeIn("Scene.EclipticBand.Renderable")
  ]],
  Documentation = "Shows the ecliptic band",
  GuiPath = "/Night Sky/Lines and Grids/Show and Hide",
  IsLocal = false
}

local HideEclipticBand = {
  Identifier = "os.nightsky.HideEclipticBand",
  Name = "Hide ecliptic band",
  Command = [[
    openspace.fadeOut("Scene.EclipticBand.Renderable")
  ]],
  Documentation = "Hides the ecliptic band",
  GuiPath = "/Night Sky/Lines and Grids/Show and Hide",
  IsLocal = false
}

local ToggleEclipticBand = {
  Identifier = "os.nightsky.ToggleEclipticBand",
  Name = "Toggle ecliptic band",
  Command = "openspace.toggleFade('Scene.EclipticBand.Renderable')",
  Documentation = "Toggles the ecliptic band visibilty",
  GuiPath = "/Night Sky/Lines and Grids",
  IsLocal = false
}


asset.onInitialize(function()
  openspace.addSceneGraphNode(EclipticLine)
  openspace.addSceneGraphNode(EclipticBand)
  openspace.action.registerAction(ShowEclipticLine)
  openspace.action.registerAction(HideEclipticLine)
  openspace.action.registerAction(ToggleEclipticLine)
  openspace.action.registerAction(ShowEclipticBand)
  openspace.action.registerAction(HideEclipticBand)
  openspace.action.registerAction(ToggleEclipticBand)
end)

asset.onDeinitialize(function()
  openspace.action.removeAction(ToggleEclipticBand)
  openspace.action.removeAction(HideEclipticBand)
  openspace.action.removeAction(ShowEclipticBand)
  openspace.action.removeAction(ToggleEclipticLine)
  openspace.action.removeAction(HideEclipticLine)
  openspace.action.removeAction(ShowEclipticLine)
  openspace.removeSceneGraphNode(EclipticBand)
  openspace.removeSceneGraphNode(EclipticLine)
end)

asset.export(EclipticLine)
asset.export(EclipticBand)
asset.export("ShowEclipticLine", ShowEclipticLine.Identifier)
asset.export("HideEclipticLine", HideEclipticLine.Identifier)
asset.export("ToggleEclipticLine", ToggleEclipticLine.Identifier)
asset.export("ShowEclipticBand", ShowEclipticBand.Identifier)
asset.export("HideEclipticBand", HideEclipticBand.Identifier)
asset.export("ToggleEclipticBand", ToggleEclipticBand.Identifier)
