# Copyright 2011-2022 Meik Michalke <meik.michalke@hhu.de>
#
# This file is part of the R package roxyPackage.
#
# roxyPackage is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# roxyPackage is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with roxyPackage.  If not, see <http://www.gnu.org/licenses/>.


## function roxy.description()
# create package description
roxy.description <- function(val, description, version=NULL, date=NULL, pck.aliases=NULL){

  if(!is.null(version)){
    description[["Version"]] <- version
  } else {}
  if(!is.null(date)){
    description[["Date"]] <- date
  } else {}

#   # prefer Author@R/Authors@R over Author/Maintainer
#   pck.authors <- get.authors(description, maintainer=TRUE, contributor=TRUE)
#   pck.author <- pck.authors[["aut"]]
#   pck.maintainer <- pck.authors[["cre"]]
#   pck.contributor <- pck.authors[["ctb"]]
#   pck.maintainer.clean <- pck.authors[["cre.clean"]]

  ## basic package info
  if(identical(val, "package")){
    return(description[["Package"]])
  } else {}
  if(identical(val, "title")){
    return(description[["Title"]])
  } else {}
  if(identical(val, "depds")){
    return(description[["Depends"]])
  } else {}
  if(identical(val, "description")){
    return(description)
  } else {}

  checkFor <- c("Version","Date","Depends","Enhances","Encoding","License","LazyLoad","URL")
  desc.parts <- sapply(checkFor, function(this.entry){
      found <- ifelse(this.entry %in% names(description), paste0("\n#' ",this.entry,": \\tab ",description[[this.entry]],"\\cr"),"")
      found <- gsub("^[[:space:]]*#'[[:space:]]*$", ".\n", found)
      return(found)
    })
  # extra check for package type
  if("Type" %in% names(description)){
    if(description[["Type"]] %in% c("Package", "Frontend", "Translation")){
      pck.type <- description[["Type"]]
    } else {
      pck.type <- "Package"
    }
  } else {
    pck.type <- "Package"
  }

  if(is.character(pck.aliases)){
    if(all(nchar(pck.aliases) > 0)){
      pck_aliases <- paste0("\n#' @aliases\n#' ", paste0(pck.aliases, collapse=" "))
    } else {
      stop(simpleError("empty values are not valid for \"pck.aliases\"!"))
    }
  } else {
    pck_aliases <- ""
  }

  pckg.package.v <- paste0(
      "# This file was generated by roxyPackage::roxy.package()",
      "\n# and will likely be overwritten on its next run.",
      "\n# Editing is futile.\n",
      "\n#' \\packageDescription{",description[["Package"]],"}\n#'\n#' The DESCRIPTION file:\n#' \\tabular{ll}{",
      ## unfortunately, this leads to useless "This package was not yet installed at build time." messages in the doc file:
      #"\n#' The DESCRIPTION file:",
      #"\n#' \\packageDESCRIPTION{",description[["Package"]],"}",
      #"\n#' \\packageIndices{",description[["Package"]],"}\n#'",
      "\n#' Package: \\tab ",description[["Package"]],"\\cr",
      "\n#' Type: \\tab ",pck.type,"\\cr",
      paste(desc.parts, collapse=""),
      "\n#' }\n#'",
      "\n#' @title\n#' \\packageTitle{",description[["Package"]],"}",
      "\n#' @author\n#' \\packageAuthor{",description[["Package"]],"}\n#'",
      "\n#' Maintainer: \\packageMaintainer{",description[["Package"]],"}",
      pck_aliases,
      ## TODO:
      #"\n#' @references",
      #"\n#' @seealso",
      #"\n#' @examples",
      #"\n#' @keywords package",
      "\n\"_PACKAGE\"\n",
      "\n## usethis namespace: start",
      "\n## usethis namespace: end",
      "\nNULL"
  )
  if(identical(val, "pckg.description")){
    return(pckg.package.v)
  } else {}
} ## end function roxy.description()
