#' setPriors1 is a general function to set priors for MHalgoGen()
#' @title Set priors for MHalgoGen()
#' @author Marc Girondot \email{marc.girondot@@gmail.com}
#' @return Return a data.frame with the formatted prior
#' @param Name Name of the parameter.
#' @param Prior1 The value for the first parameter of the prior distribution.
#' @param Prior2 The value for the second parameter of the prior distribution.
#' @param Prior3 The value for the third parameter of the prior distribution.
#' @param Parameters A vector with parameters of the prior distribution
#' @param Density The density function of the prior (ex. "dunif", "dnorm", "dbeta").
#' @param SDProp The SD of the propositions for the Markov chain.
#' @param Min The minimum value to truncate the prior distribution.
#' @param Max The maximum value to truncate the prior distribution.
#' @param Init The value at the first iteration.
#' @description Set priors for MHalgoGen()\cr
#' A check is done to verify that the Init value is well between the Min and Max range.\cr
#' The order of Prior1, 2, and 3 must be similar to the order in the Density function. 
#' For this reason, it is safer to use Parameters.\cr
#' As a rule of thumb, SDProp can be 1/10th of the range of posterior. For example if 
#' the posterior is thought to be from 0.1 to 0.2, you can use SDProp being (0.2-0.1)/10=0.01.
#' @family mcmcComposite functions
#' @examples
#' \dontrun{
#' library(HelpersMG)
#' Priors <- setPriors1(Name="a0", Prior1=0, Prior2=1, Density="dunif", 
#'                      SDProp=0.1, Min=0, Max=1, Init=0.5)
#' Priors <- Priors + setPriors1(Name="a1", Prior1=0.5, Prior2=0.1, Density="dnorm", 
#'                      SDProp=0.05, Min=0, Max=1, Init=0.5)
#' # Using Parameters
#' Priors <- setPriors1(Name="a0", Parameters=c(min=0, max=1), Density="dunif", 
#'                      SDProp=0.1, Min=0, Max=1, Init=0.5)
#' Priors <- Priors + setPriors1(Name="a1", Parameters=c(mean=0.5, sd=0.1), Density="dnorm", 
#'                      SDProp=0.05, Min=0, Max=1, Init=0.5)
#' }
#' @export


setPriors1 <- function(Name=stop("A name of parameter is required."), 
                       Prior1 = NULL, 
                       Prior2 = NULL,
                       Prior3 = NULL, 
                       Parameters = NULL, 
                       Density=stop("A density is required."), 
                       SDProp =stop("A SD of proposal is required."), 
                       Min=-Inf, 
                       Max=+Inf, 
                       Init=stop("An initial value is required.")) {
  
  if (Init < Min) stop("The Init value must not be lower than the Min value.")
  if (Init > Max) stop("The Init value must not be higher than the Max value.")
  
  if (Min > Max) {
    warning("I reverse Min and Max.")
    interm <- Min; Min <- Max; Max <- interm
  }
  
  if (is.null(c(Prior1, Prior2, Prior3, Parameters))) {
    stop("All Prior1, Prior2, Prior3, and Parameters cannot be NULL.")
  }
  
  if (!is.null(Parameters)) {
    np <- getFromNamespace("formalArgs", ns="methods")(Density)
    for (i in 1:length(Parameters)) {
      pos <- which(names(Parameters[i]) == np)
      if (identical(pos, integer(0))) stop(paste0("I don't understand the name of Parameters ", names(Parameters[i])))
      assign(paste0("Prior", as.character(pos-1)), unname(Parameters[i]))
    }
  }
  
  if (is.null(Prior3)) Prior3 <- NA
  
  df_priors <- data.frame(Density=Density, 
                          Prior1=Prior1, Prior2=Prior2, Prior3=Prior3, 
                          SDProp=SDProp, 
                          Min=Min, Max=Max, 
                          Init=Init, row.names = Name)
  
  df_priors <- addS3Class(df_priors, c("PriorsmcmcComposite", "data.frame"))
  return(df_priors)
}
