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



local modelFolder = asset.syncedResource({
  Name = "Scale School Bus",
  Type = "HttpSynchronization",
  Identifier = "scale_model_school_bus",
  Version = 1
})


local Location = { 40.780671, -73.971701, 0.78 }

local ScaleModel = {
  Identifier = "Scale_SchoolBus",
  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 .. "school_bus.gltf",
    RotationVector = { 0.0, 90.0, 0.0 },
    LightSources = { sunAsset.LightSource }
  },
  GUI = {
    Name = "School bus",
    Path = "/Scale Objects"
  }
}

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

local ResetPositionAction = {
  Identifier = "os.scalemodels.ResetSchoolBus",
  Name = "Reset School bus position",
  Command = [[
    openspace.globebrowsing.setNodePosition("Scale_SchoolBus", "]] .. earthAsset.Earth.Identifier .. [[", ]] .. Location[1] .. "," .. Location[2] .. ")",
  Documentation = "Resets the school bus 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)
