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



local models = asset.syncedResource({
  Name = "ISS Models",
  Type = "HttpSynchronization",
  Identifier = "iss_model",
  Version = 3
})

local omm = asset.syncedResource({
  Name = "Satellite OMM Data (ISS)",
  Type = "UrlSynchronization",
  Identifier = "satellite_omm_data_iss",
  Url = "https://www.celestrak.com/NORAD/elements/gp.php?CATNR=25544&FORMAT=kvn",
  Filename = "ISS.txt",
  Override = true
})


local IssPosition = {
  Identifier = "ISSPosition",
  Parent = transforms.EarthInertial.Identifier,
  BoundingSphere = 54.5, -- half the width
  Transform = {
    Translation = {
      Type = "GPTranslation",
      Observer = transforms.EarthInertial.Identifier,
      File = omm .. "ISS.txt",
      Format = "OMM"
    },
    Rotation = {
      Type = "SpiceRotation",
      SourceFrame = "GALACTIC",
      DestinationFrame = "J2000",
    }
  },
  Tag = { "earth_satellite", "ISS" },
  GUI = {
    Name = "ISS Position",
    Path = "/Solar System/Planets/Earth/Satellites/ISS",
    Hidden = true
  }
}

local IssModel = {
  Identifier = "ISS",
  Parent = IssPosition.Identifier,
  Transform = {
    Rotation = {
      Type = "FixedRotation",
      Attached = "ISS",
      XAxis = { 0.01, -1.0, 0.56 },
      XAxisOrthogonal = true,
      YAxis = transforms.EarthInertial.Identifier
    }
  },
  Renderable = {
    Type = "RenderableModel",
    GeometryFile = models .. "ISS.fbx",
    ModelScale = "Centimeter",
    LightSources = {
      sun.LightSource
    },
    PerformShading = true
  },
  Tag = { "earth_satellite", "ISS" },
  GUI = {
    Name = "ISS",
    Path = "/Solar System/Planets/Earth/Satellites/ISS"
  }
}

local IssTrail = {
  Identifier = "ISS_trail",
  Parent = transforms.EarthInertial.Identifier,
  Renderable = {
    Type = "RenderableTrailOrbit",
    Translation = {
      Type = "GPTranslation",
      Observer = transforms.EarthInertial.Identifier,
      File = omm .. "ISS.txt",
      Format = "OMM"
    },
    RenderBinMode = "PostDeferredTransparent",
    Color = { 0.9, 0.6715, 0.0 },
    Fade = 1.5,
    Resolution = 320
  },
  Tag = { "earth_satellite", "ISS" },
  GUI = {
    Name = "ISS Trail",
    Path = "/Solar System/Planets/Earth/Satellites/ISS"
  }
}

-- @TODO (emmbr, 2021-05-27) add to scene when label rendering issues have been fixed
local IssLabel = {
  Identifier = "IssLabel",
  Parent = IssPosition.Identifier,
  Renderable = {
    Enabled = false,
    Type = "RenderableLabel",
    Text = "ISS",
    FontSize = 70.0,
    Size = 3.4,
    MinMaxSize = { 1, 100 },
    OrientationOption = "Camera View Direction",
    BlendMode = "Additive",
    EnableFading = true,
    FadeDistances = { 0.15, 15.0 },
    FadeWidths = { 1.0, 25.0 }
  },
  Tag = { "solarsystem_labels" },
  GUI = {
    Name = "ISS Label",
    Path = "/Solar System/Planets/Earth/Satellites"
  }
}

local FocusIss = {
  Identifier = "os.solarsystem.FocusIss",
  Name = "Focus on ISS",
  Command = [[
    openspace.setPropertyValueSingle("NavigationHandler.OrbitalNavigator.Aim", "")
    openspace.setPropertyValueSingle("NavigationHandler.OrbitalNavigator.Anchor", "ISS")
    openspace.setPropertyValueSingle("NavigationHandler.OrbitalNavigator.RetargetAnchor", nil)
  ]],
  Documentation = "Refocuses the camera on the ISS",
  GuiPath = "/Solar System/Earth",
  isLocal = false
}


asset.onInitialize(function()
  local i = openspace.space.readKeplerFile(omm .. "ISS.txt", "OMM")
  IssTrail.Renderable.Period = i[1].Period / (60 * 60 * 24)

  openspace.addSceneGraphNode(IssPosition)
  openspace.addSceneGraphNode(IssModel)
  openspace.addSceneGraphNode(IssTrail)

  openspace.setPropertyValueSingle("Scene.ISS.Rotation.yAxisInvertObject", true)
  openspace.action.registerAction(FocusIss)
end)

asset.onDeinitialize(function()
  openspace.action.removeAction(FocusIss)

  openspace.removeSceneGraphNode(IssTrail)
  openspace.removeSceneGraphNode(IssModel)
  openspace.removeSceneGraphNode(IssPosition)
end)

asset.export(IssTrail)
asset.export(IssModel)
asset.export(IssPosition)



asset.meta = {
  Name = "ISS",
  Version = "2.0",
  Description = [[Model and Trail for ISS. Model from NASA 3D models, trail from
    Celestrak.]],
  Author = "OpenSpace Team",
  URL = "https://celestrak.com/",
  License = "NASA"
}
