<% 	
	import os
	import numpy as np
	import pylab
	import pandas as pd
	import matplotlib.pyplot as plt
	import warnings
	import math
	from py_exp_calc.exp_calc import update_innermost

	def to_css_rgb(color):
		red, green, blue, *alpha = list(map(lambda channel: round(channel*255), color))
		return f"rgb({red},{green},{blue})"
	
	#Defining default configuration for plotly and canvas ontoplots
	px_color_legend = {label: to_css_rgb(rgb) for rgb, label in color_legend.items()}

	cvx_def_conf = {
					"colorBy": "colors", 
					"colorLegendTitle": "Main branch",
					"legendTextScaleFontFact": 0.5,
					"legendTextScaleFontFactor": 0.5,
					"sizeBy": "freqs",
					"sizeByContinuous": True,
					"initialSize": 0,
					"sizeStep": 2,
					"sizeLegendTitle": "Frequency",
					"rAxisShow": False,
					"setMaxY": max_level,
					"yAxisTicks": 1 if guide_lines != "grid" else max_level,
					"rAxisGridMajorColor": 'rgb(150,150,150)',
					"theme": "GGPlot",
					"legendOrder": { "colors": list(px_color_legend.keys()) },
					"colorKey": { "colors": px_color_legend },
					"hoverTemplate": "<strong>{hp_names}</strong> <br> Main branch: {colors} <br> Frequency: {freqs} <br> level: {level}"
				}

	plotly_def_conf = {'toImageButtonOptions': {'format': 'svg', 'height': 800, 'width': 800, 'scale': 1}}
	
	if mode == "canvas": update_innermost(cvx_def_conf, config)
	if mode == "dynamic": update_innermost(plotly_def_conf, config)

	#Defining default layout for plotly ontoplots
	plotly_def_layout = dict(title = dict(text=title, font=dict(family="Courier", size=12, color="black")), 
		                   	 legend = dict(bordercolor="Black", 
						   				 borderwidth=2, 
										 title_font_family="Times New Roman", 
										 bgcolor="LightSteelBlue",
						   				 itemsizing= 'constant',
        								 font = dict(family="Courier", 
										 			 size=12,
										 			 weight = "bold", 
										 			 color="black")), 
						   	 polar = dict(angularaxis = dict(showticklabels = False, 
						   								   rotation = 90, 
														   direction = "clockwise"),
						   	   			  radialaxis = dict(dtick = 1, 
														  tickfont= dict(size = 12,
														  				 weight = "bold")))
							)

	update_innermost(plotly_def_layout, plotly_layout)
%>


<%
	gridlines, gridlabels = tuple(range(0,13)), tuple([str(num) for num in range(0,13)])
	
	def draw_static_rontoplot(data, plotter_list):
		fig = plotter_list['plt'].figure(figsize=(height/100,width/100), dpi=dpi)
		ax = fig.add_subplot(projection='polar')

		for color in data["colors"].unique():
			d = data[data["colors"] == color]
			scatter = ax.scatter(d["arc_values"], d["radius_values"], s=d["sizes"], color=color, label=color_legend[color], alpha=d["alphas"])
		
		#Legend customization
		legend1 = plt.legend(fontsize="7", bbox_to_anchor=(1.04, 1), loc="upper left")
		legend_point_size = 15
		for lh in legend1.legend_handles:
			alphas_len = len(lh.get_alpha())
			lh.set_sizes(np.array([legend_point_size]))
			lh.set_alpha(np.array([1]*alphas_len))

		#If we want to add a textbox showing info about user root original level 
		#ax.text(0.05, 0.05, root_legend, transform=ax.transAxes, fontsize=10, verticalalignment='top', bbox=dict(boxstyle='round', alpha=0.5))
		
		# Other customizations
		ax.set_facecolor("white")
		ax.set_theta_zero_location('N')
		ax.set_theta_direction(-1)
		ax.set_xticklabels([])
		if guide_lines != "grid": ax.set_yticklabels([])
		if guide_lines == "grid": 
			ax.tick_params(axis='y', colors='grey')
			ax.grid(True, color="grey")
			plt.rgrids(gridlines, gridlabels)
		else:
			ax.tick_params(axis='y', colors='white')
			ax.grid(False)
		plt.tight_layout()

	def draw_dynamic_rontoplot(data, plotter_list):
		#hover_debug="%{customdata}<br>radius_values: %{r:.2f}<br>arc_values: %{theta:.2f}<br>size: %{marker.size:.2f}<br>alpha: %{marker.opacity}"
		hover="%{customdata[0]}<br>Frequency in profiles: %{customdata[1]:.5f}"
		
		fig = plotter_list['go'].Figure(layout=plotter_list['go'].Layout(height=height, width=width))
		for branch, phens in data.groupby("colors", sort=False): 
			fig.add_trace(plotter_list['go'].Scatterpolargl(
				r = phens.radius_values,
				theta = phens.arc_values,
				name = branch,
				marker=dict(size=phens.sizes, color=color_legend[branch], opacity=phens.alphas, line=dict(width=0)),
				customdata=list(phens[['hp_names', 'freqs']].to_numpy()),
				hovertemplate = hover
				))
		fig.update_traces(mode="markers")
		#If we want to add a textbox showing info about user root original level add to layout the following line:
		#annotations = [dict(showarrow=False, text=root_legend.replace("\n","<br>"), x=0., y=0.15, xanchor='left', yanchor='bottom', font=dict(size=12 ))]
		fig.update_layout( plotly_def_layout )
		if guide_lines != "grid":
			fig.update_polars(angularaxis_showgrid=False, angularaxis_showline=False, angularaxis_showticklabels=False,
							  radialaxis_showgrid=False, radialaxis_showline=False, radialaxis_showticklabels=False) 
							  
		return fig
	
	#print(plotter.hash_vars["ontoplot_table_format"][:3])
	if mode != "canvas":
		plotter.hash_vars["ontoplot_table_format_df"] = pd.DataFrame(plotter.hash_vars["ontoplot_table_format"][1:], columns=plotter.hash_vars["ontoplot_table_format"][0])
		if mode == "dynamic":
			plotter.hash_vars["ontoplot_table_format_df"]["arc_values"] = plotter.hash_vars["ontoplot_table_format_df"]["arc_values"] / (2*math.pi) * 360
			plotter.hash_vars["ontoplot_table_format_df"]["colors"] = plotter.hash_vars["ontoplot_table_format_df"]["colors"].apply(lambda color: color_legend[color])
			color_legend.clear()
			color_legend.update(px_color_legend)
		
		plot_modes = {"static": draw_static_rontoplot,
					"dynamic": draw_dynamic_rontoplot}
		plotter_to_use = plot_modes[mode]
	elif mode == "canvas":
		plotter.hash_vars["ontoplot_table_format"][0].append("level")
		for row in plotter.hash_vars["ontoplot_table_format"][1:]: 
			row[0] = color_legend[row[0]]
			row.append(int(row[2]))
%>

<% 
mat_height = matplot_height if dynamic_units_calc == False else None
mat_width = matplot_width if dynamic_units_calc == False else None
%>

%if mode != "canvas":
	<style>
	.legendpoints .scatterpts {
	opacity: 1 !important;
	}
	</style>

	<% is_dynamic = True if mode == "dynamic" else False %>
	${plotter.static_plot_main(id="ontoplot_table_format_df", dynamic=is_dynamic, raw=True, header=True, row_names=False, 
							   plotting_function = plotter_to_use, title=title, var_attr=[0], dynamic_units_calc= dynamic_units_calc,
							   height=height, width=width, matplot_height=mat_height, matplot_width=mat_width, dpi = dpi, config=plotly_def_conf)}

%elif mode == "canvas":
	${ plotter.scatter_polar(id="ontoplot_table_format", header=True, row_names=False, transpose=True, responsive=responsive,
												title=title, height=height, width=width,
                                                radialAxis="radius_values", circularAxis="arc_values",
                                                smp_attr = [0,1,4,5,6,7],
												after_render = [["drawCustomizer",["rAxisShow",False],{}, 1744282597486],],
                                                config=cvx_def_conf
                                        )}
%endif