#' Function to visualize SHARP test results
#'
#' @importFrom ggplot2 ggplot
#' @importFrom ggplot2 aes
#' @importFrom ggplot2 geom_point
#' @importFrom ggplot2 geom_hline
#' @importFrom ggplot2 geom_vline
#' @importFrom ggplot2 labs
#' @importFrom ggplot2 theme
#' @importFrom ggplot2 element_blank
#' @importFrom ggplot2 annotate
#' @importFrom ggplot2 theme_minimal
#' @importFrom ggplot2 geom_text
#' @importFrom ggplot2 scale_x_continuous
#' @importFrom ggplot2 scale_y_continuous
#' @importFrom scales trans_new
#'
#' @details
#' The shape scatter plot represents the four p-values (corresponding to four shape types) of SHARP test on a 2D space.
#' it can not only visualize the conclusions of the test, but also the confidence level of the test.
#' The plot surface is divided into two parts along two perpendicular directions, corresponding to two opposite shapes.
#' The coordinates of points are derived from p-values such that:
#' * Inconclusive shapes are placed in the center area within the significance thresholds (p > 0.05)
#' * Significant shapes are placed in the edge area out of the significance thresholds (p < 0.05)
#' * The closer points are to the significance thresholds, the more ambiguous shape conclusion is
#'
#'
#' @param pinc,pdec,pconc,pconv SHARP test p-values corresponding to increasing, decreasing, concave and convex
#' @param alpha significance threshold. alpha=NULL corresponding to no significance threshold
#' @param scale if the axis should be scale so that the significant threshold is placed in the center
#' @param label labels of the point to visualize. label=NULL means no labeling.
#' @param size_point,size_label the size of points and label. size_lable is used when label is not NULL.
#' @param ... additional parameters passed to ggplot
#' @return The plotted ggplot object
#'
#' @export
#' @examples
#' # Simulate dose-response data
#' x <- seq(0, 1, length.out = 48)
#' y <- 2*sqrt(x)+rnorm(48)
#' y[17:32] <- y[17:32]+0.5
#' y[33:48] <- y[33:48]+1
#' curve <- data.frame(x, y)
#' curve$rep <- rep(1:3, each = 16)
#'
#' # Fixed-model based test
#' sharpt <- SHARPtest(curve, xName = "x", yName = "y", niter = 100)
#'
#' # Plot the graph
#' SharpScatter(sharpt[1], sharpt[2], sharpt[3], sharpt[4], niter = 100)

SharpScatter <- function(pinc, pdec, pconc, pconv, alpha = 0.05, scale = TRUE, label = NULL, size_point=2, size_label = 5,...){
  # coordinates for points
  ycoord = 0*(pinc==pdec)+(1-pinc)*(pinc<pdec)+(pdec-1)*(pinc>pdec)
  xcoord = 0*(pconv==pconc)+(pconc<pconv)*(1-pconc)+(pconc>pconv)*(pconv-1)

  # scatter plot
  sharp_scatter <-ggplot(data.frame(xcoord, ycoord), aes(x=xcoord, y=ycoord,...))+
    geom_point(size = size_point)+
    geom_hline(yintercept = 0, linewidth = 1) +  # center y-axis
    geom_vline(xintercept = 0, linewidth = 1) +  # center x-axis
    labs(x=" ", y=" ")+
    theme(panel.grid = element_blank(), axis.text = element_blank(), axis.ticks = element_blank())+
    annotate("text", x = c(-0.9, 0.9), y = c(0,0), label = c("Convex", "Concave"), vjust = 1.1)+
    annotate("text", y = c(-1, 1), x = c(0,0), label = c("Decrease", "Increase"), hjust = 1.1)+
    theme_minimal()

  # visualization threshold
  if(!is.null(alpha)){
    sharp_scatter <- sharp_scatter+
      geom_hline(yintercept = c(1-alpha, -1+alpha), linetype = "dashed", linewidth = 0.5)+
      geom_vline(xintercept = c(1-alpha, -1+alpha), linetype = "dashed", linewidth = 0.5)
  }

  # scale significance threshold
  if(scale){
    NewScale <- function(x){
      newx <- ifelse(abs(x)>(1-alpha), 1-((1-abs(x))/alpha)*0.5, abs(x)*0.5/(1-alpha))
      newx = sign(x)*newx
      return(newx)
    }

    # scaling functions
    InverseNewScale <- function(y){
      origy <- ifelse(abs(y) > 0.5, 1 - ((1 - abs(y)) * alpha / 0.5), abs(y) * (1-alpha) / 0.5)
      origy = sign(y) * origy
      return(origy)
    }
    # transformation object
    NewTrans <- trans_new(
      name = "NewTrans",
      transform = NewScale,
      inverse = InverseNewScale
    )
    sharp_scatter <- sharp_scatter+
      scale_x_continuous(trans = NewTrans, limits = c(-1, 1))+
      scale_y_continuous(trans = NewTrans, limits = c(-1, 1))
  }


  # point label
  if(!is.null(label)){
    sharp_scatter <- sharp_scatter+geom_text(aes(label=label), size = size_label)
  }

 return(sharp_scatter)

}




