/**** Start of imports. If edited, may not auto-convert in the playground. ****/
var class_prob = ee.Image("projects/sat-io/open-datasets/GFM/ProbaV_LC100_epoch2015_global_v203"),
    crowdsourced_data = ee.FeatureCollection("projects/sat-io/open-datasets/GFM/original_crowdsourced_data"),
    validation_data = ee.FeatureCollection("projects/sat-io/open-datasets/GFM/validation_data_set"),
    fml = ee.Image("projects/sat-io/open-datasets/GFM/FML_v3-2");
/***** End of imports. If edited, may not auto-convert in the playground. *****/
Map.addLayer(class_prob,{min:0,max:100,palette: ["ff5400","ff6d00","ff8500","ff9100","ff9e00","00b4d8","0096c7","0077b6","023e8a","03045e"]},'Class Probability')
Map.addLayer(crowdsourced_data,{},'Crowdsourced data',false)
Map.addLayer(validation_data,{},'Validation data',false)

/*
11 – Naturally regenerating forest without any signs of management, including primary forests;
20 – Naturally regenerating forest with signs of management, e.g., logging, clear cuts etc;
31 – Planted forests;
32 –Plantation forests (rotation time up to 15 years);
40 – Oil palm plantations;
53 – Agroforestry;
*/

var from = [11, 20, 31, 32, 40, 53];
var to =   [0, 1,  2,  3,  4,  5];
var fml_last = fml.remap(from, to);

print("Reclassed values:");
print({"from": from, "to": to});

// Define a dictionary which will be used to make legend and visualize image on map
var dict = {
  "names": [
  "Naturally regenerating forest without any signs of management, including primary forests",
  "Naturally regenerating forest with signs of management, e.g., logging, clear cuts etc",
  "Planted forests",
  "Plantation forests (rotation time up to 15 years)",
  "Oil palm plantations",
  ],
  "colors": [
    "0E7105",
    "32ED16",
    "2C65FC",
    "E49A22",
    "FF6EDD",
    "FEFF2C"
  ]};

// 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 = 0; 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, 'Global Forest Management dataset 2015');

Map.setCenter(-97.61655457157725,55.6280720462063,4)

// Add image to the map
Map.addLayer(fml_last, {min:0, max:5, palette:dict['colors']}, 'Global Forest Management dataset 2015')
