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

local OsirisRexKernels = asset.require("./kernels").kernels

local models = asset.syncedResource({
  Name = "Osiris Rex Models",
  Type = "HttpSynchronization",
  Identifier = "osirisrex_models",
  Version = 3
})

local BENNU_BODY = "2101955"

local OsirisRex = {
  Identifier = "OsirisRex",
  Parent = sunTransforms.SolarSystemBarycenter.Identifier,
  Transform = {
    Translation = {
      Type = "SpiceTranslation",
      Target = "OSIRIS-REX",
      Observer = "SSB",
      Kernels = OsirisRexKernels
    },
    Rotation = {
      Type = "SpiceRotation",
      SourceFrame = "ORX_SPACECRAFT",
      DestinationFrame = "GALACTIC"
    },
  },
  Renderable = {
    Type = "RenderableModel",
    Body = "OSIRIS-REX",
    GeometryFile = models .. "orx_base_resized_12_sep_2016.obj",
    LightSources = {
      sun.LightSource,
      {
        Identifier = "Camera",
        Type = "CameraLightSource",
        Intensity = 0.5
      }
    }
  },
  GUI = {
    Name = "OSIRIS REx",
    Path = "/Solar System/Missions/OSIRIS REx"
  }
}

local PolyCam = {
  Identifier = "ORX_OCAMS_POLYCAM",
  Parent = OsirisRex.Identifier,
  Transform = {
    Translation = {
      Type = "StaticTranslation",
      Position = { -0.2476, 0.2710, 0.3364 }
    },
    Rotation = {
      Type = "SpiceRotation",
      SourceFrame = "ORX_OCAMS_POLYCAM",
      DestinationFrame = "ORX_SPACECRAFT"
    }
  },
  Renderable = {
    Type = "RenderableModel",
    Body = "OSIRIS-REX",
    GeometryFile = models .. "orx_polycam_resized_12_sep_2016.obj",
    LightSources = {
      sun.LightSource,
      {
        Identifier = "Camera",
        Type = "CameraLightSource",
        Intensity = 0.5
      }
    }
  },
  GUI = {
    Name = "OCAMS POLYCAM",
    Path = "/Solar System/Missions/OSIRIS REx/Instruments"
  }
}

local Rexis = {
  Identifier = "ORX_REXIS",
  Parent = OsirisRex.Identifier,
  Renderable = {
    Type = "RenderableModel",
    Body = "OSIRIS-REX",
    GeometryFile = models .. "orx_rexis_resized_12_sep_2016.obj",
    LightSources = {
      sun.LightSource,
      {
        Identifier = "Camera",
        Type = "CameraLightSource",
        Intensity = 0.5
      }
    }
  },
  Transform = {
    Translation = {
      Type = "StaticTranslation",
      Position = { 0, 0.3371, 0.2712 }
    },
    Rotation = {
      Type = "SpiceRotation",
      SourceFrame = "ORX_REXIS",
      DestinationFrame = "ORX_SPACECRAFT"
    },
  },
  GUI = {
    Name = "REXIS",
    Path = "/Solar System/Missions/OSIRIS REx/Instruments"
  }
}

local PolyCamFov = {
  Identifier = "POLYCAM FOV",
  Parent = PolyCam.Identifier,
  Renderable = {
    Type = "RenderableFov",
    Body = "OSIRIS-REX",
    Frame = "ORX_OCAMS_POLYCAM",
    RGB = { 0.8, 0.7, 0.7 },
    Instrument = {
      Name = "ORX_OCAMS_POLYCAM",
      Method = "ELLIPSOID",
      Aberration = "NONE"
    },
    PotentialTargets = { BENNU_BODY }
  },
  GUI = {
    Name = "POLYCAM FOV",
    Path = "/Solar System/Missions/OSIRIS REx/Instruments"
  }
}

-- Commenting this out as REXIS' shape is circle, which is currently not supported in
-- the RenderableFOV class
-- local RexisFov = {
--   Identifier = "REXIS FOV",
--   Parent = Rexis.Identifier,
--   Renderable = {
--     Type = "RenderableFov",
--     Body = "OSIRIS-REX",
--     Frame = "ORX_REXIS",
--     RGB = { 0.8, 0.7, 0.7 },
--     Instrument = {
--       Name = "ORX_REXIS",
--       Method = "ELLIPSOID",
--       Aberration = "NONE"
--     },
--     PotentialTargets = { BENNU_BODY },
--     FrameConversions = {
--       [BENNU_BODY] = "IAU_BENNU"
--     }
--   },
--   GUI = {
--     Name = "REXIS FOV",
--     Path = "/Solar System/Missions/OSIRIS REx/Instruments"
--   }
-- }

local nodes =  {
  OsirisRex,
  PolyCam,
  Rexis,
  PolyCamFov,
  -- RexisFov
}

asset.onInitialize(function()
  for i, node in ipairs(nodes) do
    openspace.addSceneGraphNode(node)
  end
end)
  
asset.onDeinitialize(function()
  for i = #nodes, 1, -1 do
    openspace.removeSceneGraphNode(nodes[i])
  end
end)

for i, node in ipairs(nodes) do
  asset.export(node)
end
