#' Return distribution fits for smoothed ROC curves.
#'
#' Retrieves the  distribution fits associated with smoothed ROC curves generated using the \ifelse{html}{\code{\link[pROC:smooth]{smooth()}}}{\code{smooth()}} function of the ‘pROC’ package via the` “fitdist” ` method. A `smooth.roc` object is provided as input, and the fitted case and control distribution families, parameters, and AIC values are returned. Can be used downstream of [find.distr()] and \ifelse{html}{\code{\link[pROC:smooth]{smooth()}}}{\code{smooth()}} to inspect and evaluate the final distribution fits ultimately used to generate the smoothed ROC curve.
#'
#' @param smooth.roc
#' A `smooth.roc` object generated by \ifelse{html}{\code{\link[pROC:smooth]{smooth()}}}{\code{smooth()}} using `method = "fitdistr"`.
#'
#' @return
#' A nested list with the following elements:
#' \item{`controls`}{A list containing fit information for the fitted control distribution:
#'   \describe{
#'     \item{`distribution`}{The distribution family.}
#'     \item{`parameters`}{A named numeric vector containing the distribution parameters.}
#'     \item{`aic`}{The AIC value.}
#'   }
#' }
#' \item{`cases`}{A list containing fit information for the fitted case distribution:
#'   \describe{
#'     \item{`distribution`}{The distribution family.}
#'     \item{`parameters`}{A named numeric vector containing the distribution parameters.}
#'     \item{`aic`}{The AIC value.}
#'   }
#' }
#'
#' Objects returned by [get.fits()] contain a custom print method that displays a human readable summary of the fitted distributions when printed at the console.
#'
#' @seealso
#' \ifelse{html}{\link[pROC]{pROC-package}}{pROC-package} \cr
#' \ifelse{html}{\code{\link[pROC:roc]{roc()}}}{\code{pROC::roc()}} \cr
#' \ifelse{html}{\code{\link[pROC:smooth]{smooth()}}}{\code{pROC::smooth()}} \cr
#' [rocTools-package] \cr
#' [find.distr()] \cr
#' \code{\link{blood_data}}
#'
#' @examples
#' # load packages and example blood biomarker data
#'
#' library(pROC)
#' library(rocTools)
#' data(blood_data)
#'
#' # build raw ROC curve
#'
#' raw_roc <- roc(
#'   blood_data,
#'   response = "Diagnosis",
#'   predictor = "NfL"
#' )
#'
#' # find best fitting case and control distribution
#' # families to use for ROC smoothing
#'
#' best_fits <- find.distr(raw_roc)
#'
#' # use best fitting families to build smooth ROC curve
#'
#' smooth_roc <- smooth(
#'   raw_roc,
#'   method = "fitdistr",
#'   density.controls = best_fits$density.controls,
#'   density.cases = best_fits$density.cases,
#'   start.controls = best_fits$start.controls,
#'   start.cases = best_fits$start.cases
#' )
#'
#' # print final distribution fits used
#' # to build the smoothed curve
#'
#' get.fits(smooth_roc)
#'
#' @export

get.fits<-function(smooth.roc){

  smooth_roc<-smooth.roc

  if(!inherits(smooth_roc, "smooth.roc")){
    stop("Input must be a smooth.roc object generated by the 'pROC' package.")
  }

  if(is.null(smooth_roc$smoothing.args$method)||
     smooth_roc$smoothing.args$method!="fitdistr"){
    stop("Input must be a smooth.roc object generated with method = \"fitdistr\".")
  }

  extract_one<-function(fit){

    if(inherits(fit, "try-error")||is.null(fit)){
      stop("Distribution fitting failed; cannot extract parameters.")
    }

    dist<-fit$densfun
    params<-fit$estimate

    if(!is.numeric(params)||length(params)<1){
      stop("Invalid parameter estimates in smooth.roc object.")
    }

    k<-length(params)
    ll<-fit$loglik

    if(!is.finite(ll)){
      stop("Non-finite log-likelihood in fitted distribution.")
    }

    aic<-2*k-2*ll

    list(
      distribution=dist,
      parameters=params,
      aic=aic
    )
  }

  out<-list(
    controls=extract_one(smooth_roc$fit.controls),
    cases=extract_one(smooth_roc$fit.cases)
  )

  class(out)<-"roc.fits"

  return(out)
}
