local earthAsset = asset.require("scene/solarsystem/planets/earth/earth")
local sunAsset = asset.require("scene/solarsystem/sun/sun")



local modelFolder = asset.syncedResource({
  Name = "Scale Model Statue of Liberty",
  Type = "HttpSynchronization",
  Identifier = "scale_model_statue_of_liberty",
  Version = 1
})


local Location = { 40.689206, -74.044487, -3.0 }

local ScaleModel = {
  Identifier = "Scale_StatueOfLiberty",
  Parent = earthAsset.Earth.Identifier,
  -- Note: Lat/Lon/Scale values come from alignment with Esri World Imagery 2D layer
  Transform = {
    Translation = {
      Type = "GlobeTranslation",
      Globe = earthAsset.Earth.Identifier,
      Latitude = Location[1],
      Longitude = Location[2],
      Altitude = Location[3],
      UseHeightmap = true
    },
    Rotation = {
      Type = "GlobeRotation",
      Globe = earthAsset.Earth.Identifier,
      Latitude = Location[1],
      Longitude = Location[2]
    }
  },
  Renderable = {
    Type = "RenderableModel",
    GeometryFile = modelFolder .. "STATUE_OF_LIBERTY.osmodel",
    RotationVector = { 0.0, 235.0, 0.0 },
    LightSources = { sunAsset.LightSource }
  },
  GUI = {
    Name = "Statue of Liberty",
    Path = "/Scale Objects"
  }
}

local UpdatePositionAction = {
  Identifier = "os.scalemodels.DropStatueOfLiberty",
  Name = "Drop Statue of Liberty under camera",
  Command = [[
    openspace.globebrowsing.setNodePositionFromCamera("Scale_StatueOfLiberty")
  ]],
  Documentation = "Updates the Statue of Liberty position based on the globe location of the camera",
  GuiPath = "/Scale Objects",
  IsLocal = false
}

local ResetPositionAction = {
  Identifier = "os.scalemodels.ResetStatueOfLiberty",
  Name = "Reset Statue of Liberty position",
  Command = [[
    openspace.globebrowsing.setNodePosition("Scale_StatueOfLiberty", "]] .. earthAsset.Earth.Identifier .. [[", ]] .. Location[1] .. "," .. Location[2] .. ")",
  Documentation = "Resets the Statue of Liberty back to its actual location",
  GuiPath = "/Scale Objects",
  IsLocal = false
}


asset.onInitialize(function()
  openspace.addSceneGraphNode(ScaleModel)

  openspace.action.registerAction(UpdatePositionAction)
  openspace.action.registerAction(ResetPositionAction)
end)

asset.onDeinitialize(function()
  openspace.action.removeAction(ResetPositionAction)
  openspace.action.removeAction(UpdatePositionAction)

  openspace.removeSceneGraphNode(ScaleModel)
end)

asset.export(ScaleModel)
asset.export("UpdatePositionAction", UpdatePositionAction.Identifier)
asset.export("ResetPositionAction", ResetPositionAction.Identifier)
