am5.ready(function() {

// Create root element
// https://www.amcharts.com/docs/v5/getting-started/#Root_element 
var root = am5.Root.new("instancesChart");

try {
		root.setThemes([
			am5themes_Animated.new(root),
			am5themes_Dark.new(root)
		]);
	}
	catch(e) {
		root.setThemes([
			am5themes_Animated.new(root)
		]);
	}

// Create chart
// https://www.amcharts.com/docs/v5/charts/xy-chart/
var chart = root.container.children.push(am5xy.XYChart.new(root, {
  panX: true,
  panY: true,
  wheelX: "panX",
  wheelY: "zoomX",
  maxTooltipDistance: 0,
  pinchZoomX:true
}));


// Create axes
// https://www.amcharts.com/docs/v5/charts/xy-chart/axes/
var xAxis = chart.xAxes.push(am5xy.DateAxis.new(root, {
  maxDeviation: 0.2,
  baseInterval: {
    timeUnit: "day",
    count: 1
  },
  renderer: am5xy.AxisRendererX.new(root, {
    minorGridEnabled: true
  }),
  tooltip: am5.Tooltip.new(root, {})
}));

var yAxis = chart.yAxes.push(am5xy.ValueAxis.new(root, {
  renderer: am5xy.AxisRendererY.new(root, {})
}));

var count = 0;
var totalProducts = $('.product').length;
// Add series
$(".product").each(function(idx,obj) {
	
	var uuid = $(this).val();
	var name2 = $(this).attr('name');
	am5.net.load("/app/api/instances/daily/" + uuid).then(function(result) {

	  var data  = am5.JSONParser.parse(result.response);
	
	  var series = chart.series.push(am5xy.LineSeries.new(root, {
	    name: name2,
	    xAxis: xAxis,
	    yAxis: yAxis,
	    valueYField: "value",
	    valueXField: "date",
	    legendValueText: "{valueY}",
	    tooltip: am5.Tooltip.new(root, {
	      pointerOrientation: "horizontal",
	      labelText: "{valueY}"
	    })
	  }));
	
	  date = new Date();
	  date.setHours(0, 0, 0, 0);
	  value = 0;
	  series.data.setAll(data.resource);
	
	  // Make stuff animate on load
	  // https://www.amcharts.com/docs/v5/concepts/animations/
	  series.appear();
	
	  if(++count == totalProducts) {
	
			// Add legend
		// https://www.amcharts.com/docs/v5/charts/xy-chart/legend-xy-series/
		var legend = chart.rightAxesContainer.children.push(am5.Legend.new(root, {
		  width: 200,
		  paddingLeft: 15,
		  height: am5.percent(100)
		}));
		
		// When legend item container is hovered, dim all the series except the hovered one
		legend.itemContainers.template.events.on("pointerover", function(e) {
		  var itemContainer = e.target;
		
		  // As series list is data of a legend, dataContext is series
		  var series = itemContainer.dataItem.dataContext;
		
		  chart.series.each(function(chartSeries) {
		    if (chartSeries != series) {
		      chartSeries.strokes.template.setAll({
		        strokeOpacity: 0.15,
		        stroke: am5.color(0x000000)
		      });
		    } else {
		      chartSeries.strokes.template.setAll({
		        strokeWidth: 3
		      });
		    }
		  })
		})
		
		// When legend item container is unhovered, make all series as they are
		legend.itemContainers.template.events.on("pointerout", function(e) {
		  var itemContainer = e.target;
		  var series = itemContainer.dataItem.dataContext;
		
		  chart.series.each(function(chartSeries) {
		    chartSeries.strokes.template.setAll({
		      strokeOpacity: 1,
		      strokeWidth: 1,
		      stroke: chartSeries.get("fill")
		    });
		  });
		})
		
		legend.itemContainers.template.set("width", am5.p100);
		legend.valueLabels.template.setAll({
		  width: am5.p100,
		  textAlign: "right"
		});
		
		legend.labels.template.setAll({
		  fontSize: 10,
		  fontWeight: "300"
		});
		
		// It's is important to set legend data after all the events are set on template, otherwise events won't be copied
		legend.data.setAll(chart.series.values);
		
		
		// Make stuff animate on load
		// https://www.amcharts.com/docs/v5/concepts/animations/
		chart.appear(1000, 100);
		
	  }
	}).catch(function(result) {
	  console.log("Error loading " + result.xhr);
	});
	
	
});


// Add cursor
// https://www.amcharts.com/docs/v5/charts/xy-chart/cursor/
var cursor = chart.set("cursor", am5xy.XYCursor.new(root, {
  behavior: "none"
}));
cursor.lineY.set("visible", false);


}); 