#' Compute pairwise GDSC matrix for a collection of series
#'
#' @param X Matrix with time series in columns (rows = time points).
#' @param n_s Integer, number of states.
#' @param lags Vector of lag orders.
#' @param th_step Numeric, threshold step size.
#' @param show_progress Logical.
#'
#' @return Matrix of GDSC values between columns.
#' @export
#'
#' @examples
#' n <- 1000
#' X <- matrix(0,n,4)
#' X[,1]=arima.sim(model = list(ar = 0.5), n = n)
#' X[,2]=arima.sim(model = list(ar = 0.5), n = n)
#' X[,3]=arima.sim(model = list(ar = -0.5), n = n)
#' X[,4]=arima.sim(model = list(ar = -0.5), n = n)
#' gdsc_matrix(X, n_s = 10,lags=1:3,th_step = 0.005)
gdsc_matrix <- function(X, n_s = 15, lags = 1:5, th_step = 0.005,
                        show_progress = FALSE) {
  X <- as.matrix(X)
  m <- ncol(X)
  mat <- matrix(0, m, m)
  colnames(mat) <- rownames(mat) <- colnames(X)

  if (show_progress) pb <- utils::txtProgressBar(min = 0, max = m*(m-1)/2, style = 3)
  counter <- 0
  for (i in 1:m) {
    for (j in i:m) {
      val <- gdsc(X[, i], X[, j], n_s, lags, th_step)
      mat[i, j] <- val
      mat[j, i] <- val
      if (i != j) {
        counter <- counter + 1
        if (show_progress) utils::setTxtProgressBar(pb, counter)
      }
    }
  }
  if (show_progress) close(pb)
  return(mat)
}
