local globeIdentifier = asset.require("../../earth").Earth.Identifier


local Name = "Vegetation: Seasonal Changes - Apr 2012 - Apr 2013"
local Identifier = "noaa-sos-land-global_vegetation"
local Description = [[Although 75% of the planet is a relatively unchanging ocean of 
blue, the remaining 25% of Earth's surface is a dynamic green. Data from the NASA/NOAA 
Suomi NPP satellite is able to show these subtle differences in greenness using the 
Visible-Infrared Imager/Radiometer Suite (VIIRS) instrument on board the satellite. This 
dataset highlights our ever-changing planet, using a highly detailed vegetation index 
data from the satellite, developed by scientists at NOAA]]
local URL = "https://sos.noaa.gov/catalog/datasets/vegetation-seasonal-changes-apr-2012-apr-2013/"


local syncedDirectory = asset.syncedResource({
  Name = Name,
  Type = "HttpSynchronization",
  Identifier = Identifier,
  Version = 1
})

local imagesDestination = syncedDirectory .. "images/"
local colorbarDestination = syncedDirectory .. "colorbar/"

local background1layer = {
  Identifier = Identifier .. "-background-14a",
  Name = Name .. " (Background Week 14a)",
  Enabled = asset.enabled,
  FilePath = syncedDirectory .. "2012_week14a.png",
  Description = Description
}

local background2layer = {
  Identifier = Identifier .. "-background-14",
  Name = Name .. " (Background Week 14)",
  Enabled = asset.enabled,
  FilePath = syncedDirectory .. "2012week14.png",
  Description = Description
}

local layer = {
  Identifier = Identifier,
  Name = Name,
  Enabled = asset.enabled,
  Type = "TemporalTileLayer",
  Mode = "Folder",
  Folder = {
    Folder = imagesDestination,
    -- See https://en.cppreference.com/w/cpp/io/manip/get_time for an explanation of the
    -- time formatting string
    Format = "%Yweek%U.png"
  },
  Description = Description
}

local colorbar = {
  Identifier = Identifier,
  Name = Name,
  Enabled = asset.enabled,
  Type = "ScreenSpaceImageLocal",
  -- @TODO:  We need a temporal provider for ScreenSpaceImages to handle this
  TexturePath = colorbarDestination .. "NDVIColor00001.png",
  FaceCamera = false,
  CartesianPosition = { 0.0, -0.5, -2.0 }
}

asset.onInitialize(function()
  if not openspace.directoryExists(imagesDestination) then
    openspace.printInfo("Extracting " .. Name .. " Images")
    openspace.unzipFile(syncedDirectory .. "4096.zip", imagesDestination, true)
  end

  if not openspace.directoryExists(colorbarDestination) then
    openspace.printInfo("Extracting " .. Name .. " Colorbar")
    openspace.unzipFile(syncedDirectory .. "colorbar.zip", colorbarDestination, true)
  end

  openspace.globebrowsing.addLayer(globeIdentifier, "ColorLayers", background1layer)
  openspace.globebrowsing.addLayer(globeIdentifier, "ColorLayers", background2layer)
  openspace.globebrowsing.addLayer(globeIdentifier, "ColorLayers", layer)
  openspace.addScreenSpaceRenderable(colorbar);  
end)

asset.onDeinitialize(function()
  openspace.globebrowsing.deleteLayer(globeIdentifier, "ColorLayers", layer)
  openspace.globebrowsing.deleteLayer(globeIdentifier, "ColorLayers", background2layer)
  openspace.globebrowsing.deleteLayer(globeIdentifier, "ColorLayers", background1layer)
  openspace.removeScreenSpaceRenderable(colorbar)
end)

asset.export(layer)
asset.export(background2layer)
asset.export(background1layer)
asset.export(colorbar)


asset.meta = {
  Name = Name,
  Version = "1.0",
  Description = Description .. "Data provided by the National Oceanic and Atmospheric Administration",
  Author = "National Oceanic and Atmospheric Administration",
  URL = URL,
  License = "https://sos.noaa.gov/copyright/"
}
