#' @export
#' @method plot BoundIRT
plot.BoundIRT=function(x,what=c("icc","iic","tic","density"),items=NULL,separate=FALSE,range=c(-3,3),by=.001,ymax=NULL,xlab=NULL,ylab=NULL,main=NULL,col=NULL,lwd_prob=2, ...){
  object=x
  what=match.arg(what)
  par=object$output
  inflated=object$inflated
  model=object$model
  nit=nrow(par)
  beta=par[,3]
  alpha=par[,1]
  phi=par[,5]

  if(inflated=="zero" |inflated=="both") gamma0=par[,7] else gamma0=rep(-Inf,nit)
  if(inflated=="one") gamma1=par[,7] else gamma1=rep(Inf,nit)
  if(inflated=="both") gamma1=par[,9]

  theta=seq(range[1],range[2],by=by)
  if(is.null(items)) items=1:nit
  if(!is.null(items)){
    if(length(items)>nit) stop("invalid items argument")
    if(any(items<=0)) stop("invalid items argument")
  }
  out=matrix(,length(theta),length(items))

  if(what=="icc"){
    if(is.null(ylab)) ylab="expected response"
    if(is.null(xlab)) xlab="latent trait"
    if(is.null(main)) main="Item Charateristic Curve"

    for(i in 1:length(items)) out[,i]=iccBIRT(theta,alpha[items[i]],beta[items[i]],phi[items[i]],gamma0[items[i]],gamma1[items[i]],model=model)
  }

  if(what=="iic" | what=="tic"){
    if(is.null(ylab)) ylab="information"
    if(is.null(xlab)) xlab="latent trait"
    if(what=="iic") if(is.null(main)) main="Item Information Curve"
    if(what=="tic") if(is.null(main)) main="Test Information Curve"

    for(i in 1:length(items)) out[,i]=iicBIRT(theta,alpha[items[i]],beta[items[i]],phi[items[i]],gamma0[items[i]],gamma1[items[i]],model=model)
    if(what=="tic") out=apply(out,1,sum)
  }
  if(what=="density"){
    if(is.null(ylab)) ylab="density/probability"
    if(is.null(xlab)) xlab="z"
    if(is.null(main)) main="Model implied density"

    z=seq(0,1,by=by)
    out=matrix(,length(z),length(items))

    for(i in 1:length(items)) out[,i]=dBIRT(z,alpha[items[i]],beta[items[i]],phi[items[i]],gamma0[items[i]],gamma1[items[i]],model=model)
  }
  x0=x=theta
  out0=out
  if(what=="density"){
    gg=length(z)
    x=z
    ind=-c(1,gg)
    x0=x[ind]
    out0=out[ind,]
  }
  if(length(items)==1 | what=="tic") out0=matrix(out0)
  if(is.null(col)) col=rep(1:6,ceiling(length(items)/6))
  if(length(col)==1 & length(items!=1)) col=rep(col,length(items))

  if(is.null(ymax)) ymax=apply(out0,2,max)
  if(length(ymax)==1 & length(items!=1)) ymax=rep(ymax,length(items))

  if(!separate) graphics::matplot(x0,out0,type="l",lty=1,lwd=2,xlab=xlab,ylab=ylab,main=main,col=col,ylim=c(0,max(ymax)),...)
  for(i in 1:length(items)){
    if(separate) plot(x0,out0[,i],type="l",lty=1,lwd=2,xlab=xlab,ylab=ylab,main=main,col=col[i],ylim=c(0,ymax[i]),...)

    if(what=="density"){
        if(is.finite(gamma0[items[i]])) {
          graphics::segments(0,0,0,out[1,i],lwd=lwd_prob,col="grey")
          graphics::points(0,out[1,i],pch=16,col=col[i])
        }
        if(is.finite(gamma1[items[i]])){
          graphics::segments(1,0,1,out[gg,i],lwd=lwd_prob,col="grey")
          graphics::points(1,out[gg,i],pch=16,col=col[i])
        }
      }
  }

  invisible(list(x=x,out=out))
}
