local sun = asset.require("scene/solarsystem/sun/sun")
local sunTransforms = asset.require("scene/solarsystem/sun/transforms")
local mercuryTransforms = asset.require("scene/solarsystem/planets/mercury/transforms")


local models = asset.syncedResource({
  Name = "Messenger Models",
  Type = "HttpSynchronization",
  Identifier = "messenger_model",
  Version = 2
})

local kernels = asset.syncedResource({
  Name = "Messenger Kernels",
  Type = "HttpSynchronization",
  Identifier = "messenger_spice",
  Version = 2
})


local LocalKernels = {
  kernels .. "messenger_2548.tsc",
  kernels .. "msgr_v231.tf",

  kernels .. "de405.bsp",
  kernels .. "msgr_040803_150430_150430_od431sc_2.bsp",
  kernels .. "msgr_antenna_v000.bsp",
  kernels .. "msgr_de405_de423s.bsp",

  kernels .. "msgr_epps_v100.ti",
  kernels .. "msgr_grns_v110.ti",
  kernels .. "msgr_mag_v021.ti",
  kernels .. "msgr_mascs_v100.ti",
  kernels .. "msgr_mdis_v160.ti",
  kernels .. "msgr_mla_v010.ti",
  kernels .. "msgr_rs_v111.ti",
  kernels .. "msgr_xrs_v001.ti",


  kernels .. "pck00008.tpc",
  kernels .. "pck00008_msgr.tpc",
  kernels .. "pck00009_msgr_v10.tpc",
  kernels .. "pck00010_msgr_v10.tpc",
  kernels .. "pck00010_msgr_v23.tpc",
  kernels .. "pck00010.tpc",

  kernels .. "msgr_1103_v02.bc",
  kernels .. "msgr_1104_v02.bc",
  kernels .. "msgr_1105_v02.bc",
  kernels .. "msgr_1106_v02.bc"
}


local RotationMatrix = {
  1, 0, 0,
  0, 0, -1,
  0, 1, 0
}

local Messenger = {
  Identifier = "Messenger",
  Parent = sunTransforms.SolarSystemBarycenter.Identifier,
  Transform = {
    Translation = {
      Type = "SpiceTranslation",
      Target = "MESSENGER",
      Observer = "SSB",
      Kernels = LocalKernels
    },
    Rotation = {
      Type = "SpiceRotation",
      SourceFrame = "MSGR_SPACECRAFT",
      DestinationFrame = "GALACTIC"
    }
  },
  TimeFrame = {
    Type = "TimeFrameInterval",
    Start = "2011-03-01",
    End = "2011-06-30"
  },
  GUI = {
    Name = "Messenger",
    Path = "/Solar System/Missions/Messenger"
  }
}

local MessengerProbeBlack = {
  Identifier = "MessengerProbe_black",
  Parent = Messenger.Identifier,
  Renderable = {
    Type = "RenderableModel",
    GeometryFile = models .. "MessengerProbe_black.obj",
    ModelTransform = RotationMatrix,
    LightSources = {
      sun.LightSource,
      {
        Identifier = "Camera",
        Type = "CameraLightSource",
        Intensity = 0.5
      }
    }
  },
  GUI = {
    Name = "MessengerProbe Black",
    Path = "/Solar System/Missions/Messenger"
  }
}

local MessengerProbeFoil = {
  Identifier = "MessengerProbe_foil",
  Parent = Messenger.Identifier,
  Renderable = {
    Type = "RenderableModel",
    GeometryFile = models .. "MessengerProbe_foil.obj",
    ModelTransform = RotationMatrix,
    LightSources = {
      sun.LightSource,
      {
        Identifier = "Camera",
        Type = "CameraLightSource",
        Intensity = 0.5
      }
    }
  },
  GUI = {
    Name = "MessengerProbe foil",
    Path = "/Solar System/Missions/Messenger"
  }
}

local MessengerProbeHeatShield = {
  Identifier = "MessengerProbe_heatShield",
  Parent = Messenger.Identifier,
  Renderable = {
    Type = "RenderableModel",
    GeometryFile = models .. "MessengerProbe_heatShield.obj",
    ModelTransform = RotationMatrix,
    LightSources = {
      sun.LightSource,
      {
        Identifier = "Camera",
        Type = "CameraLightSource",
        Intensity = 0.5
      }
    }
  },
  GUI = {
    Name = "MessengerProbe Heat Sheild",
    Path = "/Solar System/Missions/Messenger"
  }
}

local MessengerProbeMetal = {
  Identifier = "MessengerProbe_Metal",
  Parent = Messenger.Identifier,
  Renderable = {
    Type = "RenderableModel",
    GeometryFile = models .. "MessengerProbe_metal.obj",
    ModelTransform = RotationMatrix,
    LightSources = {
      sun.LightSource,
      {
        Identifier = "Camera",
        Type = "CameraLightSource",
        Intensity = 0.5
      }
    }
  },
  GUI = {
    Name = "MessengerProbe Metal",
    Path = "/Solar System/Missions/Messenger"
  }
}

local MessengerProbePanels = {
  Identifier = "MessengerProbe_panels",
  Parent = Messenger.Identifier,
  Renderable = {
    Type = "RenderableModel",
    GeometryFile = models .. "MessengerProbe_panels.obj",
    ModelTransform = RotationMatrix,
    LightSources = {
      sun.LightSource,
      {
        Identifier = "Camera",
        Type = "CameraLightSource",
        Intensity = 0.5
      }
    }
  },
  GUI = {
    Name = "MessengerProbe Panels",
    Path = "/Solar System/Missions/Messenger"
  }
}

local MessengerTrail = {
  Identifier = "MessengerTrail",
  Parent = mercuryTransforms.MercuryBarycenter.Identifier,
  Renderable = {
    Type = "RenderableTrailOrbit",
    Translation = {
      Type = "SpiceTranslation",
      Target = "MESSENGER",
      Observer = "MERCURY BARYCENTER",
      Kernels = LocalKernels
    },
    Color = { 0.288, 0.375, 0.934 },
    EnableFade = false,
    StartTime = "2011 MARCH 01 12:00:00",
    EndTime = "2011 MAY 30 12:00:00",
    Period = 12,
    Resolution = 10000
  },
  GUI = {
    Name = "Messenger Trail",
    Path = "/Solar System/Missions/Messenger"
  }
}


local nodes = {
  Messenger,
  MessengerProbeBlack,
  MessengerProbeFoil,
  MessengerProbeHeatShield,
  MessengerProbeMetal,
  MessengerProbePanels,
  MessengerTrail
}

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
