local trail = asset.require("./trail")
local marsTransforms = asset.require("scene/solarsystem/planets/mars/transforms")
local sun = asset.require("scene/solarsystem/sun/sun")
local spice = asset.require("./spice")



local models = asset.syncedResource({
  Name = "Perseverance Model",
  Type = "HttpSynchronization",
  Identifier = "perseverance_models",
  Version = 2
})


local TranslationKeyframes = {
  ["1850 JAN 01 00:00:00"] = {
    Type = "SpiceTranslation",
    Target = spice.ID,
    Observer = "MARS",
    Frame = "IAU_MARS",
    FixedDate = "2020 JUL 17 13:56:43"
  },
  ["2020 JUL 17 13:56:42"] = {
    Type = "SpiceTranslation",
    Target = spice.ID,
    Observer = "MARS",
    Frame = "IAU_MARS",
    FixedDate = "2020 JUL 17 13:56:43"
  },
  ["2020 JUL 17 13:56:43"] = {
    Type = "SpiceTranslation",
    Target = spice.ID,
    Observer = "MARS",
    Frame = "IAU_MARS"
  },
  ["2020 JUL 17 13:56:44"] = {
    Type = "SpiceTranslation",
    Target = spice.ID,
    Observer = "MARS",
    Frame = "IAU_MARS"
  },
  ["2021 FEB 18 20:43:48"] = {
    Type = "SpiceTranslation",
    Target = spice.ID,
    Observer = "MARS",
    Frame = "IAU_MARS"
  },
  ["2021 FEB 18 20:43:49"] = {
    Type = "SpiceTranslation",
    Target = spice.ID,
    Observer = "MARS",
    Frame = "IAU_MARS",
    FixedDate = "2021 FEB 18 20:43:48"
  }
}

local PerseveranceNode = {
  Identifier = "PerseveranceNode",
  Parent = marsTransforms.MarsBarycenter.Identifier,
  Transform = {
    Translation = {
      Type = "SpiceTranslation",
      Target = "MARS",
      Observer = "MARS BARYCENTER"
    },
    Rotation = {
      Type = "SpiceRotation",
      SourceFrame = "IAU_MARS",
      DestinationFrame = "GALACTIC"
    }
  },
  GUI = {
    Name = "Perseverance Node",
    Path = "/Solar System/Missions/Perseverance",
    Hidden = true
  }
}

-- Perseverance Model --
local Perseverance = {
  Identifier = "Perseverance",
  Parent = PerseveranceNode.Identifier,
  Transform = {
    Translation = {
      Type = "TimelineTranslation",
      Keyframes = TranslationKeyframes
    }
  },
  GUI = {
    Name = "Perseverance",
    Path = "/Solar System/Missions/Perseverance"
  }
}

-- Perseverance Model --
local PerseveranceModel = {
  Identifier = "PerseveranceModel",
  Parent = Perseverance.Identifier,
  GUI = {
    Name = "Perseverance Model",
    Path = "/Solar System/Missions/Perseverance",
    Hidden = true
  }
}

-- Perseverance Model Instruments --
local Body = {
  Identifier = "Perseverance_body",
  Parent = PerseveranceModel.Identifier,
  Renderable = {
    Type = "RenderableModel",
    Body = "MARS SCIENCE LABORATORY",
    GeometryFile = models .. "Perseverance.obj",
    LightSources = {
      sun.LightSource,
      {
        Identifier = "Camera",
        Type = "CameraLightSource",
        Intensity = 0.5
      }
    },
    RotationVector = { 19.19, 0.0, 348.08 }
  },
  GUI = {
    Name = "Perseverance Model Body",
    Path = "/Solar System/Missions/Perseverance/Model"
  }
}


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

  openspace.addSceneGraphNode(PerseveranceNode)
  openspace.addSceneGraphNode(Perseverance)
  openspace.addSceneGraphNode(PerseveranceModel)
  openspace.addSceneGraphNode(Body)
end)

asset.onDeinitialize(function()
  openspace.removeSceneGraphNode(Body)
  openspace.removeSceneGraphNode(PerseveranceModel)
  openspace.removeSceneGraphNode(Perseverance)
  openspace.removeSceneGraphNode(PerseveranceNode)

  openspace.spice.unloadKernel(spice.Kernels)
end)

asset.export(PerseveranceNode)
asset.export(Perseverance)
asset.export(PerseveranceModel)
asset.export(Body)
