<div class="row mt-2">
	<div class="col">
		<h3>Statistics</h3>
		<!-- first row -->
		<div class="row mt-4 list-group list-group-horizontal">
			<div class="col-1"></div>
			<div class="col-3 list-group-item d-flex justify-content-between align-items-center">
				Threshold (T):
				<span id="run_stats_threshold" class="badge badge-secondary badge-pill">?</span>
			</div>
			<div class="col-7 list-group-item d-flex justify-content-between align-items-center">
				Result:
				<span>
					<span id="run_stats_bgc_assigned" class="badge badge-secondary badge-pill">?</span> BGCs were assigned to <span id="run_stats_gcf_assigned" class="badge badge-secondary badge-pill">?</span> GCFs <a id="run_stats_gcf_assigned_popover" href="javascript:void();" class="badge badge-info badge-pill ml-2 d-none" data-trigger="hover" data-html="true" data-content="" title="Details">details</a></span>
				</span>
			</div>
			<div class="col-1"></div>
		</div>
		<!-- second row -->
		<div class="row mt-2">
			<!-- bgc length histogram -->
			<div class="col-6" style="min-width: 480px;">
				<div id="hist_avg_dist" class="text-center">
					<div class="spinner-border text-info" role="status">
						<span class="sr-only">Loading...</span>
					</div>
				</div>
			</div>
			<!-- num BGCs per GCF -->
			<div class="col-6" style="min-width: 480px;">
				<div id="hist_bgc_counts" class="text-center">
					<div class="spinner-border text-info" role="status">
						<span class="sr-only">Loading...</span>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript">
	$(document).ready(function(){
		// fetch and update tables
		$.get({
			url: "/api/run/get_statistics_summary",
			data: {
				"run_id": $("#run_id").val()
			},
			dataType: "json",
			success: function(response) {
				$("#run_stats_threshold").text(response["threshold"].toFixed(2));
				$("#run_stats_bgc_assigned").text(response["bgc_assigned_counts"]);
				$("#run_stats_gcf_assigned").text(response["gcf_assigned_counts"]);

				// popover-bgc_assigned
				if (response["bgc_assigned_counts"] > 0) {
					let content_popover = "<div class='list-group'>";
					content_popover += "<div class='list-group-item'><span class='badge badge-secondary badge-pill'>" + response["bgc_not_assigned_counts"] + "</span> BGCs does not pass the threshold value (d > <span class='badge badge-secondary badge-pill'>" + response["threshold"] + "</span>).</div>";
					content_popover += "<div class='list-group-item'><span class='badge badge-secondary badge-pill'>" + (response["gcf_total_counts"] - response["gcf_assigned_counts"]) + "</span> GCFs have no member (n=0).</div>";
					content_popover += "<div class='list-group-item'><span class='badge badge-secondary badge-pill'>" + response["gcf_singleton_counts"] + "</span> GCFs were singletons (n=1).</div>";
					content_popover += "</div>";
					$("#run_stats_gcf_assigned_popover").removeClass("d-none");
					$("#run_stats_gcf_assigned_popover").popover({
						"content": content_popover
					});
				} else {
					$("#run_stats_gcf_assigned_popover").addClass("d-none");
				}
			}
		});
		// fetch and render avg dists barplot
		$.get({
			url: "/api/run/get_statistics_dist",
			data: {
				"run_id": $("#run_id").val()
			},
			dataType: "json",
			success: function(response) {
				let container_id = "hist_avg_dist";
				
				// hide spinner
				$("#" + container_id + " .spinner-border").addClass("d-none");

				// define traces
				let trace_complete = {
					x: response["labels"],
					y: response["values_complete"],
					marker: {
				    	color: "#1F77B4"
				    },
					name: 'Complete',
					type: 'bar'
				};
				let trace_fragmented = {
					x: response["labels"],
					y: response["values_fragmented"],
					marker: {
						color: "#FF7F0E"
					},
					name: 'Fragmented<br />(on_contig_edge)',
					type: 'bar'
				};
				let trace_na = {
					x: response["labels"],
					y: response["values_unknown"],
					marker: {
				    	color: "#2CA02C"
				    },
					name: 'n/a<br />(antiSMASH < 4.2.0)',
					type: 'bar'
				};
				let trace_threshold = {
					x: [""] + response["labels"] + [""],
					y: Array(response["labels"].length + 2).fill(response["threshold"]),
				    line: {
				    	color: "#000",
				    	dash: 'dot'
					},
					name: 'Threshold (T)',
					mode: 'lines'
				};
				let data = [trace_threshold, trace_complete, trace_fragmented, trace_na];
				let layout = {
					title: "Avg. dist to GCF models",
					barmode: "group",
					xaxis: {
					    tickangle: -45
					},
					yaxis: {
						title: "dist (euclidean)"
					},
					showlegend: true,
					legend: {
						font: {
							size: 8
						}
					}
				};
				let config = {
					responsive: false,
					staticPlot: false
				}
				Plotly.newPlot(container_id, data, layout, config);
		}})
		// fetch and render bgc counts barplot
		$.get({
			url: "/api/run/get_statistics_bgc_counts",
			data: {
				"run_id": $("#run_id").val()
			},
			dataType: "json",
			success: function(response) {
				let container_id = "hist_bgc_counts";
				
				// hide spinner
				$("#" + container_id + " .spinner-border").addClass("d-none");

				// define traces
				let data = [{
					x: response["labels"],
					y: response["values"],
					type: 'bar'
				}];
				let layout = {
					title: "BGCs in GCFs<br>(n_min=" + response["min"] + "; n_avg=" + response["avg"] + "; n_max=" + response["max"] + ")",
					xaxis: {
					    tickangle: -45
					},
					yaxis: {
						title: "count"
					},
					showlegend: false,
				};
				let config = {
					responsive: false,
					staticPlot: false
				}
				Plotly.newPlot(container_id, data, layout, config);
		}})
	});
</script>
