<div class="row mt-2 mb-4">
	<div class="col">
		<h3>Annotations</h3>
		<!-- arrower -->
		<div class="row mt-4 mb-4">
			<div class="col pl-2 pr-2 text-center">
				<div id="annot_arrower" class="overflow-auto" style="height: 5em; max-width: 100vw;">
					<div class="spinner-border text-info" role="status">
						<span class="sr-only">Loading...</span>
					</div>
				</div>
			</div>
		</div>
		<div class="row mt-2">
			<!-- word cloud -->
			<div class="col-4">
				<h4>Features word cloud <a href="javascript:void();" class="badge badge-info badge-pill ml-2" data-toggle="popover" data-trigger="hover" data-html="true" data-content="<p>Word size is defined by the total pHMM hit counts for the entire BGC. <br />For the sub_pfam features (ends with '%_cN'), counts were weighted <br />based on their ranked bitscores (i.e. first will have 1x, second 0.75x, <br />and so on). Only top-30 abundant features are being shown in this <br />word cloud." title="What is this?">&nbsp;?&nbsp;</a></h4>
				<div id="annot_word_cloud" class="text-center" style="height: 480px">
					<div class="spinner-border text-info" role="status">
						<span class="sr-only">Loading...</span>
					</div>
				</div>
			</div>
			<!-- genes table -->
			<div class="col-8">
				<h4>Genes</h4>
				<table id="annot_table_genes" class="table table-striped table-bordered table-hover nowrap">
					<thead class="thead-light">
						<tr>
							<th>name</th>
							<th>product</th>
							<th>location</th>
							<th>domains</th>
							<th>sequence</th>
						</tr>
					</thead>
					<tbody class="tbody">
					</tbody>
				</table>
			</div>
		</div>
		<!-- scripts -->
		<script type="text/javascript">
			$(document).ready(function(){
				// fetch and update BGC arrower on annotation page
				$.get({
					url: "/api/reports/module/query/detail/get_arrower_objects",
					data: {
						"bgc_id": $("#bgc_id").val(),
						"report_id": $("#report_id").val()
					},
					dataType: "json",
					success: function(response) {
						let bgcObject = response[$("#bgc_id").val()];
						bgcObject["orfs"].forEach(function(cds) {
							cds["domains"].forEach(function(domain) {
								if (!domainColors.hasOwnProperty(domain.code)) {
									domainColors[domain.code] = getRandomColor(0.5);
								}
								domain["color"] = domainColors[domain.code];
							});
						});
						$("#annot_arrower").html(Arrower.drawClusterSVG(bgcObject, height=40));
					}
				});
				// fetch and update word cloud
				$.get({
					url: "/api/reports/module/query/detail/get_word_cloud",
					data: {
						"bgc_id": $("#bgc_id").val(),
						"report_id": $("#report_id").val(),
						"limit": 30
					},
					dataType: "json",
					success: function(response) {
						$('#annot_word_cloud').html("");
						$('#annot_word_cloud').jQCloud(response["words"], {
							shape: "elliptic",
							steps: 5
						});							
					}
				});
				// fetch and update gene tables
				$("#annot_table_genes").DataTable({
					searching: false,
					ordering:  false,
					serverSide: true,
					responsive: true,
					ajax: {
						url: "/api/reports/module/query/detail/get_genes_table",
						type: "GET",
						data: {
							"bgc_id": $("#bgc_id").val(),
							"report_id": $("#report_id").val()
						}
					},
					lengthMenu: [[5, 10, 25, -1], [5, 10, 25, "All"]],
					columnDefs: [
						{
							targets: 0, // gene name
							render: function(data, type, row, meta) {
								return data.join(" / ");
							}
						},
						{
							targets: 2, // locus
							render: function(data, type, row, meta) {
								let start = data[0];
								let end = data[1];
								let strand = data[2] > 0? "+" : data[2] < 0? "-" : "?";
								let length = ((end - start) / 1000).toFixed(2);

								return start + "-" + end + " [" + strand + "] (" + length + " kb)";
							}
						},
						{
							targets: 3, // domains
							render: function(data, type, row, meta) {
								if (data.length > 0) {
									let content_popover = "<div class=\"list-group\">";
									data.forEach(function(value, index) {
										content_popover += "<div class=\"list-group-item\">" + value[2] + "-" + value[3] + ": <strong>" + value[0] + "</strong> (bitscore: " + value[1] + ")</div>";
									});				
									content_popover += "</div>";
									return "<button class='btn btn-info gene_table_domains_viewbtn' data-content='" + content_popover + "'>view</button>";
								} else {
									return "n/a";
								}

							}								
						},
						{
							targets: 4, // aa_seq
							render: function(data, type, row, meta) {
								html_content = "<span value='" + data[1] + "' class='gene_table_copierbtn btn btn-info'>copy</span>";
								return html_content; // todo
							}								
						},
						{ className: "text-right", "targets": [] },
						{ className: "text-center", "targets": [3, 4] }
					],
					drawCallback: function( settings ) {
						$(".gene_table_domains_viewbtn").popover({
							trigger: "focus",
							html: true,
							title: "Domains",
							template: '<div class="popover" role="tooltip"><div class="arrow"></div><h3 class="popover-header"></h3><div class="popover-body"></div></div>'
						});
						$(".gene_table_copierbtn").click(function(){
							copy_to_clipboard($(this).attr("value"));
						});
				    }
				});
			});
		</script>
	</div>
</div>