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



local kernels = asset.syncedResource({
  Name = "Dawn Kernels",
  Type = "HttpSynchronization",
  Identifier = "dawn_kernels",
  Version = 3
})

local textures = asset.syncedResource({
  Name = "Dawn Textures",
  Type = "HttpSynchronization",
  Identifier = "dawn_textures",
  Version = 1
})

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


local KernelFiles = {
  kernels .. "dawn_ref_070926-150201_070829.bsp",

  --ik
  kernels .. "dawn_fc_v10.ti",

  -- SPK
  kernels .. "dawn_rec_070927_070930_081218_v1.bsp",
  -- kernels .. "dawn_rec_110201_110328_110328_v1.bsp",
  -- kernels .. "dawn_rec_110328_110419_110420_v1.bsp",
  kernels .. "dawn_rec_110416_110802_110913_v1.bsp",
  kernels .. "dawn_rec_110802_110831_110922_v1.bsp",
  -- kernels .. "spk/dawn_rec_110831_110928_111221_v1.bsp",
  -- kernels .. "dawn_rec_110928_111102_120615_v1.bsp",
  -- kernels .. "dawn_rec_111102_111210_120618_v1.bsp",
  -- kernels .. "dawn_rec_111211_120501_120620_v1.bsp",
  kernels .. "dawn_rec_120724_120913_121213_v1.bsp",

  -- FK
  kernels .. "dawn_v12.tf",

  --SCLK
  kernels .. "dawn_203_sclkscet_00039.tsc",

  -- CK
  kernels .. "dawn_sc_070927_070930.bc",
  kernels .. "dawn_sc_110801_110807.bc",
  kernels .. "dawn_sc_110808_110814.bc",
  kernels .. "dawn_sc_120910_120916.bc",

  -- space craft CK files
  -- kernels .. "dawn_sc_070927_070930.bc",
  -- kernels .. "dawn_sc_110801_110807.bc",
  -- kernels .. "dawn_sc_110808_110814.bc",
  -- kernels .. "dawn_sc_120910_120916.bc",

  -- Solar array rotation kernels
  kernels .. "dawn_sa_070927_070930.bc",
  -- kernels .. "dawn_sa_110801_110807.bc",
  -- kernels .. "dawn_sa_110808_110814.bc",
  -- kernels .. "dawn_sa_120910_120916.bc"
}

local LightSources = {
  sun.LightSource,
  {
    Identifier = "Camera",
    Type = "CameraLightSource",
    Intensity = 0.5
  }
}

local DawnPosition = {
  Identifier = "DawnPosition",
  Parent = transforms.SolarSystemBarycenter.Identifier,
  Transform = {
    Translation = {
      Type = "SpiceTranslation",
      Target = "DAWN",
      Observer = "SSB"
    },
    -- Rotation for model version 2
    Rotation = {
      Type = "StaticRotation",
      Rotation = { math.pi / 2.0, 0.0, math.pi / 2.0 }
    }
  },
  GUI = {
    Name = "Dawn Position",
    Path = "/Solar System/Missions/Dawn",
    Hidden = true
  }
}

local Dawn = {
  Identifier = "Dawn",
  Parent = DawnPosition.Identifier,
  Transform = {
    Rotation = {
      Type = "SpiceRotation",
      SourceFrame = "DAWN_SPACECRAFT",
      DestinationFrame = "GALACTIC"
    }
  },
  Renderable = {
    Type = "RenderableModel",
    GeometryFile = models .. "Dawn_19.glb",
    LightSources = LightSources
  },
  GUI = {
    Name = "Dawn",
    Path = "/Solar System/Missions/Dawn"
  }
}

local DawnTrail = {
  Identifier = "DawnTrail",
  Parent = transforms.SolarSystemBarycenter.Identifier,
  Renderable = {
    Type = "RenderableTrailTrajectory",
    Translation = {
      Type = "SpiceTranslation",
      Target = "DAWN",
      Observer = "SSB"
    },
    Color = { 1.0, 0.8, 0.4 },
    ShowFullTrail = false,
    StartTime = "2007 SEP 26 13:28:00",
    EndTime = "2012 SEP 12 12:00:00",
    PointSize = 5,
    SampleInterval = 3600,
    TimeStampSubsampleFactor = 4,
    EnableFade = true,
    Rendering = "Lines+Points"
  },
  GUI = {
    Name = "Dawn Trail",
    Path = "/Solar System/Missions/Dawn"
  }
}

-- DawnFov 1
local DawnFramingCamera1 = {
  Identifier = "Dawn_framing_camera_1",
  Parent = Dawn.Identifier,
  Renderable = {
    Type = "RenderableFov",
    Body = "DAWN",
    Frame = "DAWN_SPACECRAFT",
    Color = { 0.8, 0.7, 0.7 },
    Instrument = {
      Name = "DAWN_FC1",
      Method = "ELLIPSOID",
      Aberration = "NONE"
    },
    PotentialTargets = { "VESTA", "CERES" }
  },
  GUI = {
    Name = "Dawn Framing Camera 1",
    Path = "/Solar System/Missions/Dawn"
  }
}

local DawnFramingCamera2 = {
  Identifier = "Dawn_framing_camera_2",
  Parent = Dawn.Identifier,
  Renderable = {
    Type = "RenderableFov",
    Body = "DAWN",
    Frame = "DAWN_SPACECRAFT",
    Color = { 0.8, 0.7, 0.7 },
    Instrument = {
      Name = "DAWN_FC2",
      Method = "ELLIPSOID",
      Aberration = "NONE"
    },
    PotentialTargets = { "VESTA", "CERES" }
  },
  GUI = {
    Name = "Dawn Framing Camera 2",
    Path = "/Solar System/Missions/Dawn"
  }
}


asset.onInitialize(function()
  openspace.spice.loadKernel(KernelFiles)

  openspace.addSceneGraphNode(DawnPosition)
  openspace.addSceneGraphNode(Dawn)
  openspace.addSceneGraphNode(DawnTrail)
  openspace.addSceneGraphNode(DawnFramingCamera1)
  openspace.addSceneGraphNode(DawnFramingCamera2)
end)

asset.onDeinitialize(function()
  openspace.removeSceneGraphNode(DawnFramingCamera2)
  openspace.removeSceneGraphNode(DawnFramingCamera1)
  openspace.removeSceneGraphNode(DawnTrail)
  openspace.removeSceneGraphNode(Dawn)
  openspace.removeSceneGraphNode(DawnPosition)

  openspace.spice.unloadKernel(KernelFiles)
end)

asset.export(DawnPosition)
asset.export(Dawn)
asset.export(DawnTrail)
asset.export(DawnFramingCamera1)
asset.export(DawnFramingCamera2)



asset.meta = {
  Name = "Dawn",
  Version = "2.0",
  Description = "Dawn spacecraft and trail",
  Author = "OpenSpace Team",
  URL = "http://openspaceproject.com",
  License = "MIT license"
}
