# TODO: Add comment
# 
# Author: E.Korsching  2024
###############################################################################



#### plot which is setting up on the result data
#    of the generalized regression procedure
#    it is an alternative to the  plot.sample.search.dependencies()  plot.sample.search.dependencies.2()


plot.star.weight <- function(esdbf90, vertex.size=30, edge.w=1, label.cex=1, mfrow=c(1,1), title="", subt=NULL){
	# plot based on the generalized regression graph plot and data
	#  a star topology plot is generated
	#  for all reference situations
	# title: text, subt: vector of text elements (number of situations)
	# esdbf90: input list object from enumeration.search.dep.boots.f90() or similar results
	# vertex.size: node size,  label.cex: cex of vertex label
	# mfrow: mfrow parameter from c(1,1) to something like c(2,3) ...
	# edge.w: edge width
	#  small distance high synergistic, large distance high antagonistic : both big arrow sizes, color: blue,red
	#  intemediate distance (~0): small arrow size, color: ligth gray
	l.len <- length(esdbf90)
	if(l.len!=3){ stop("list input seems to be wrong") }
	mat.nr <- nrow(esdbf90[[3]])
	mat.nc <- ncol(esdbf90[[3]])
	mat.rn <- dimnames(esdbf90[[3]])[[1]]
	mat.cn <- dimnames(esdbf90[[3]])[[2]]
	mD <- esdbf90[[3]]
	# create vertex color per vertex
	mC <- matrix("", mat.nr, mat.nc)
	mC[mD>0] <- "deepskyblue"
	mC[mD==0] <- "gray"
	mC[mD<0] <- "sienna1"
	# customize weight  cor: -1,1 -> 1,-1 -> 2.5,0.5
	mDa <- mD*(-1)				# flip scale
	mDa <- mDa+1.5				# shift to >=0.5 to not overlap with the center
	## plot star structure - equal edge length adjusted by weight
	if(mfrow[1]==1){
		layout(rbind(	rep(1, mfrow[2]),
						seq(2, (2+2*(mfrow[2]-1)), 2),
						seq(3, (3+2*(mfrow[2]-1)+1), 2)
						),
				heights=c(0.1,0.1,0.35),
				widths=rep(0.333, mfrow[2]) )
	}else if(mfrow[1]==2){
		layout(rbind(	rep(1, mfrow[2]),
						seq(2, (2+2*(mfrow[2]-1)), 2),
						seq(3, (3+2*(mfrow[2]-1)+1), 2),
						seq((2*mfrow[2]+2), ((2*mfrow[2]+2)+2*(mfrow[2]-1)), 2),
						seq((2*mfrow[2]+3), ((2*mfrow[2]+3)+2*(mfrow[2]-1)+1), 2)
						),
				heights=c(0.1,0.1,0.35,0.1,0.35),
				widths=rep(0.333, mfrow[2]) )
	}else{
		stop("adjust the function")
	}
	#layout.show(n=13)	# 7,13
	par(mar=c(0.1,0.1,0.1,0.1))
	# title
	plot(0,0, type="n", axes=F, xlab="", ylab="", xlim=c(0,1), ylim=c(0,1))
	text(0,0.5, labels=paste(title,", ssq min. ",round(esdbf90[[2]][1,1],1),", max. ",round(esdbf90[[2]][1,3],1),sep=""),
		adj=c(0,1), cex=1, col="black")
	# subtitle + graphs
	for(i in 1:mat.nc){
		# sub
		plot(0,0, type="n", axes=F, xlab="", ylab="", xlim=c(0,1), ylim=c(0,1))
		text(0,0.5, labels=subt[i], adj=c(0,1), vfont=c("sans serif", "bold"), cex=2, col="black")
		# graph
		# name node 1 (always the same node) to name node 2 , weight==distance of nodes by coordinate transformation
		net <- data.frame(n1=rep(mat.cn[i], mat.nr), n2=mat.rn, weight=mDa[,i])
		ord <- order(mD[,i], decreasing=F)	# order
		net <- net[ord,]
		g1 <- graph_from_data_frame(net, directed=F)	# create igraph data object
		coords <- layout_( g1, as_star() )	# create a layout object (matrix : x,y coordinate positions, all nodes the same distance)
		weight.scale <- c(1, net$weight)	# create weight object, vector
		coords2 <- weight.scale * coords	# adjust coordinates, multiply vector by each column
		plot(g1,
			layout=coords2,
			vertex.size=vertex.size,
			vertex.color=c("cornsilk", mC[ord, i]),		# center reference color + test colors
			vertex.frame.color="gray",
			vertex.label.color="black",
			vertex.label.cex=label.cex,
			edge.width=edge.w,		# mD[ord, i]*edge.f
			add=F)
	}
	par(mar=c(5,4,4,2))
	return()
}

#plot.star.weight(h.bp2.06, vertex.size=50, edge.w=1, label.cex=1, mfrow=c(2,3), title="experiment h.bp2.06", subt=c("A","B","C","D"))

# tech info:
# https://igraph.org/r/doc/plot.common.html
# ?plot.igraph   in the R prompt to get the full source code https://stackoverflow.com/questions/11253726/igraph-fixed-node-coordinates-layout
# tkplot.setcoords(id, coor) , tkplot.getcoords(id,norm=F)
# possible: segments(x0=-0.5, y0=0.5, x1=0.5, y1=0.5, col="black", lty=1, lwd=1.2)

