asset.require("spice/base")
asset.require("../actions")
asset.require("./actions")
local sun = asset.require("scene/solarsystem/sun/sun")
local moonTransforms = asset.require("scene/solarsystem/planets/earth/moon/moon")
local descentKeyframes = asset.require("./lem_descent")
local descentRotationKeyframes = asset.require("./lem_descent_rotation")
local kernels = asset.require("./kernels")



local models = asset.syncedResource({
  Name = "Apollo Models",
  Type = "HttpSynchronization",
  Identifier = "apollo_11_models",
  Version = 1
})

local lemModel = asset.syncedResource({
  Name = "Apollo Lem Models",
  Type = "HttpSynchronization",
  Identifier = "apollo_lem_model",
  Version = 4
})


--landing - 1969-07-20T20:17:40
local ApolloSpiceId = -911
local ApolloLemSpiceId = -911500

local Apollo11Position = {
  Identifier = "Apollo11Position",
  Parent = moonTransforms.Moon.Identifier,
  TimeFrame = {
    Type = "TimeFrameInterval",
    Start = "1969 JUL 19 19:38:29.183",
    End = "1969 JUL 22 04:55:35.183"
  },
  Transform = {
    Translation = {
      Type = "SpiceTranslation",
      Target = ApolloSpiceId,
      Observer = "MOON",
      Frame = "MOON_ME"
    }
  },
  GUI = {
    Hidden = true,
    Name = "Apollo 11",
    Path = "/Solar System/Missions/Apollo/11"
  }
}

local Apollo11Model = {
  Identifier = "Apollo11",
  Parent = Apollo11Position.Identifier,
  TimeFrame = {
    Type = "TimeFrameInterval",
    Start = "1969 JUL 19 19:38:29.183",
    End = "1969 JUL 22 04:55:35.183"
  },
  Transform = {
    Scale = {
      Type = "StaticScale",
      Scale = 20.0
    }
  },
  Renderable = {
    Type = "RenderableModel",
    GeometryFile = models .. "Apollo_CSM_shrunk_rotated_xy_double_size.obj",
    LightSources = {
      sun.LightSource,
      {
        Identifier = "Camera",
        Type = "CameraLightSource",
        Intensity = 0.5
      }
    }
  },
  GUI = {
    Name = "Apollo 11 CSM",
    Path = "/Solar System/Missions/Apollo/11"
  }
}

local Apollo11MoonTrail = {
  Identifier = "Apollo11MoonTrail",
  Parent = moonTransforms.Moon.Identifier,
  Renderable = {
    Type = "RenderableTrailTrajectory",
    Translation = {
      Type = "SpiceTranslation",
      Target = ApolloSpiceId,
      Observer = "MOON",
      Frame = "IAU_MOON"
    },
    Color = { 0.180000,0.510000,0.750000 },
    StartTime = "1969 JUL 19 19:38:29.183",
    EndTime = "1969 JUL 22 04:55:35.183",
    SampleInterval = 60,
    EnableFade = false,
    Enabled = false
  },
  GUI = {
    Name = "Apollo 11 Moon Orbits",
    Path = "/Solar System/Missions/Apollo/11"
  }
}


local lemTranslation = {
  Type = "TimelineTranslation",
  Keyframes = {
    -- 20:14:30 is an arbitrary cutoff, but last 4 minutes data in descentKeyframes
    -- begins at 20:14:40. Due to linear interpolation, we will get
    -- a 10s linear transition to the location where the descentKeyframes start.
    ["1969 JUL 20 20:14:30"] = {
      Type = "SpiceTranslation",
      Target = ApolloLemSpiceId,
      Observer = "MOON",
      Frame = "IAU_MOON"
    },
    ["1969 JUL 20 20:14:40"] = {
      Type = "TimelineTranslation",
      Keyframes = descentKeyframes.keyframes
    }
  }
}

local lemRotation = {
  Type = "TimelineRotation",
  Keyframes = descentRotationKeyframes.keyframes
}



local Apollo11LemTrail = {
  Identifier = "Apollo11LemTrail",
  Parent = moonTransforms.Moon.Identifier,
  Renderable = {
    Type = "RenderableTrailTrajectory",
    Translation = lemTranslation,
    Color = { 0.780000,0.940000,0.340000 },
    StartTime = "1969 JUL 20 19:10:25.183",
    EndTime = "1969 JUL 20 20:17:46.183",
    SampleInterval = 2,
    EnableFade = false,
    Enabled = false
  },
  GUI = {
    Name = "Apollo 11 Lunar Lander Trail",
    Path = "/Solar System/Missions/Apollo/11"
  }
}

local Apollo11LemPosition = {
  Identifier = "Apollo11LemPosition",
  Parent = moonTransforms.Moon.Identifier,
  TimeFrame = {
    Type = "TimeFrameInterval",
    Start = "1969 JUL 20 19:10:25.183"
  },
  Transform = {
    Translation = lemTranslation,
    Rotation = lemRotation
  },
  GUI = {
    Name = "Apollo 11 Lunar Lander Position",
    Path = "/Solar System/Missions/Apollo/11"
  }
}
--landing - 1969-07-20T20:17:40

local Apollo11LemDescentModel = {
  Identifier = "Apollo11LemDescentModel",
  Parent = Apollo11LemPosition.Identifier,
  TimeFrame = {
    Type = "TimeFrameInterval",
    Start = "1969 JUL 19 19:38:29.183",
    End = "1969 JUL 20 20:17:40.0"
  },
  Transform = {
    Scale = {
      Type = "StaticScale",
      Scale = 0.24
    }
  },
  Renderable = {
    Type = "RenderableModel",
    GeometryFile = lemModel .. "lmremoved.obj",
    SpecularIntensity = 0.0,
    RotationVector = { 273.750, 28.0, 309.85 },
    LightSources = {
      sun.LightSource,
      {
        Identifier = "Camera",
        Type = "CameraLightSource",
        Intensity = 0.5
      }
    }
  },
  GUI = {
    Name = "Apollo 11 Descent Lem",
    Path = "/Solar System/Missions/Apollo/11"
  }
}

local Apollo11LemLandedModel = {
  Identifier = "Apollo11LemLandedModel",
  Parent = Apollo11LemPosition.Identifier,
  TimeFrame = {
    Type = "TimeFrameInterval",
    Start = "1969 JUL 20 20:17:40.0"
  },
  Transform = {
    Scale = {
      Type = "StaticScale",
      Scale = 0.24
    }
  },
  Renderable = {
    Type = "RenderableModel",
    GeometryFile = lemModel .. "LM-2_ver2clean.obj",
    SpecularIntensity = 0.0,
    RotationVector = { 273.750, 28.0, 309.85 },
    LightSources = {
      sun.LightSource,
      {
        Identifier = "Camera",
        Type = "CameraLightSource",
        Intensity = 0.5
      }
    }
  },
  GUI = {
    Name = "Apollo 11 Landed Lem",
    Path = "/Solar System/Missions/Apollo/11"
  }
}


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

  openspace.addSceneGraphNode(Apollo11Position)
  openspace.addSceneGraphNode(Apollo11LemPosition)
  openspace.addSceneGraphNode(Apollo11Model)
  openspace.addSceneGraphNode(Apollo11LemDescentModel)
  openspace.addSceneGraphNode(Apollo11LemLandedModel)
  openspace.addSceneGraphNode(Apollo11MoonTrail)
  openspace.addSceneGraphNode(Apollo11LemTrail)
end)

asset.onDeinitialize(function()
  openspace.removeSceneGraphNode(Apollo11LemTrail)
  openspace.removeSceneGraphNode(Apollo11MoonTrail)
  openspace.removeSceneGraphNode(Apollo11LemLandedModel)
  openspace.removeSceneGraphNode(Apollo11LemDescentModel)
  openspace.removeSceneGraphNode(Apollo11Model)
  openspace.removeSceneGraphNode(Apollo11LemPosition)
  openspace.removeSceneGraphNode(Apollo11Position)

  openspace.spice.unloadKernel(kernels.kernels)
end)

asset.export(Apollo11Position)
asset.export(Apollo11LemPosition)
asset.export(Apollo11Model)
asset.export(Apollo11LemDescentModel)
asset.export(Apollo11LemLandedModel)
asset.export(Apollo11MoonTrail)
asset.export(Apollo11LemTrail)
