/**** Start of imports. If edited, may not auto-convert in the playground. ****/
var au = ee.ImageCollection("projects/sat-io/open-datasets/LandCoverNet/LABELS/ref_landcovernet_au_v1_labels"),
    af = ee.ImageCollection("projects/sat-io/open-datasets/LandCoverNet/LABELS/ref_landcovernet_af_v1_labels"),
    as = ee.ImageCollection("projects/sat-io/open-datasets/LandCoverNet/LABELS/ref_landcovernet_as_v1_labels"),
    eu = ee.ImageCollection("projects/sat-io/open-datasets/LandCoverNet/LABELS/ref_landcovernet_eu_v1_labels"),
    na = ee.ImageCollection("projects/sat-io/open-datasets/LandCoverNet/LABELS/ref_landcovernet_na_v1_labels"),
    sa = ee.ImageCollection("projects/sat-io/open-datasets/LandCoverNet/LABELS/ref_landcovernet_sa_v1_labels");
/***** End of imports. If edited, may not auto-convert in the playground. *****/
print(af.aggregate_histogram('source_imagery_start'))

//Add a multiclass chip
var image = na.filter(ee.Filter.gte('distinct_classes',7)).first()
image = image.updateMask(image.gt(0)).select('b1')
Map.centerObject(image)

// var img = ee.Image('projects/earthengine-legacy/assets/projects/sat-io/open-datasets/LandCoverNet/LABELS/au/51JXH_01_2018_LC_10m')
// print(img)
// var image = au.first()
// print(au)

print(image)
print(ee.Date.parse("YYYYMMDD",ee.String(ee.List(ee.String(image.get('source_imagery_datelist')).split(',')).sort().get(0)).trim()))
//print(ee.List(ee.String(image.get('datelist')).split(',')).get(20))
// print(ee.Date.parse("YYYYMMDD",ee.String(ee.List(ee.String(image.get('datelist')).split(',')).get(20))))
// print(ee.Date.parse(ee.List(ee.String(image.get('datelist')).split(',')).sort().get(20)))
// var img =ee.Image('projects/sat-io/open-datasets/LandCoverNet/LABELS/ref_landcovernet_au_v1_labels/59GLL_12_2018_LC_10m')
// print(img)

// Define a dictionary which will be used to make legend and visualize image on map
var dict = {
  "names": [
    "Water (Permanent)",//1
    "Artificial",//2
    "Natural",//3
    "Snow/Ice (Permanent)",//4
    "Woody",//5
    "Cultivated",//6
    "(Semi) Natural",//7
  ],
  "colors": [
    "#0000ff",
    "#888888",
    "#d1a46d",
    "#f5f5ff",
    "#d64c2b",
    "#186818",
    "#00ff00"
  ]};

// Create a panel to hold the legend widget
var legend = ui.Panel({
  style: {
    position: 'bottom-left',
    padding: '8px 15px'
  }
});

// Function to generate the legend
function addCategoricalLegend(panel, dict, title) {
  
  // Create and add the legend title.
  var legendTitle = ui.Label({
    value: title,
    style: {
      fontWeight: 'bold',
      fontSize: '18px',
      margin: '0 0 4px 0',
      padding: '0'
    }
  });
  panel.add(legendTitle);
  
  var loading = ui.Label('Loading legend...', {margin: '2px 0 4px 0'});
  panel.add(loading);
  
  // Creates and styles 1 row of the legend.
  var makeRow = function(color, name) {
    // Create the label that is actually the colored box.
    var colorBox = ui.Label({
      style: {
        backgroundColor: color,
        // Use padding to give the box height and width.
        padding: '8px',
        margin: '0 0 4px 0'
      }
    });
  
    // Create the label filled with the description text.
    var description = ui.Label({
      value: name,
      style: {margin: '0 0 4px 6px'}
    });
  
    return ui.Panel({
      widgets: [colorBox, description],
      layout: ui.Panel.Layout.Flow('horizontal')
    });
  };
  
  // Get the list of palette colors and class names from the image.
  var palette = dict['colors'];
  var names = dict['names'];
  loading.style().set('shown', false);
  
  for (var i = 1; i < names.length; i++) {
    panel.add(makeRow(palette[i], names[i]));
  }
  
  Map.add(panel);
  
}


/*
  // Display map and legend ///////////////////////////////////////////////////////////////////////////////
*/

// Add the legend to the map
addCategoricalLegend(legend, dict, 'Land cover');

// Add LC image to the map
Map.addLayer(image, {min:1, max:7, palette:dict['colors']}, 'LandCoverNet v1.0 NA')

//Map.centerObject(collection.first(),8)