#library(startR)
#MergeArrays <- 
library(s2dverification)
MergeArrays <- s2dverification:::.MergeArrays

shared_dir <- 

args <- commandArgs(TRUE)[-1]

chunk_files_original <- list.files(paste0(shared_dir, '/'), '.*\\.Rds$')
chunk_files <- gsub('\\.Rds$', '', chunk_files_original)
chunk_filename_parts <- strsplit(chunk_files, '__')
if (length(unique(sapply(chunk_filename_parts, length))) != 1) {
  stop("Detected chunks with more dimensions than others.")
}
dim_names <- sapply(chunk_filename_parts[[1]], 
# TODO: strsplit by the last '_' match, not the first.
                    function(x) strsplit(x, '_')[[1]][1])
# TODO check all files have exactly the same dimnames
found_chunk_indices <- sapply(chunk_filename_parts, 
                              function(x) as.numeric(strsplit(x, '_')[[1]][2]))
found_chunk_indices <- array(found_chunk_indices, 
                             dim = c(length(dim_names), 
                                     length(found_chunk_indices) / length(dim_names))
                            )
found_chunks_str <- apply(found_chunk_indices, 2, paste, collapse = '_')

if (length(args) > 0) {
  if ((length(args) %% 2) != 0) {
    stop("Wrong number of parameters.")
  }
  expected_dim_names <- args[(1:(length(args) / 2) - 1) * 2 + 1])
  if (any(!is.character(expected_dim_names))) {
    stop("Expected dimension names in parameters at odd positions.")
  }
  dim_indices <- args[(1:(length(args) / 2) - 1) * 2 + 2])
  if (!any(dim_indices == 'all')) {
    stop("Expected one dimension index to be 'all'.")
  }
  dim_to_merge <- which(dim_indices == 'all')
  if (length(dim_indices) > 1) {
    if (!all(is.numeric(dim_indices[-dim_to_merge]))) {
      stop("Expected all dimension index but one to be numeric.")
    }
  }
  # Check expected dim names match dim names
  ## TODO
  # Merge indices that vary along dim_to_merge whereas other fixed by dim_indices
  # REMOVE FILES
  ## TODO
  stop("Feature not implemented.")
} else {
  chunks_indices <- 1:length(dim_names)
  chunks_indices <- lapply(chunks_indices, function(x) sort(unique(found_chunk_indices[x, ])))
  names(chunks_indices) <- dim_names

  # Load all found chunks into the array 'array_of_chuks'.
  array_dims <- sapply(chunks_indices, length)
  names(array_dims) <- dim_names
  array_of_chunks <- vector('list', prod(array_dims))
  dim(array_of_chunks) <- array_dims
  array_of_chunks_indices <- array(1:prod(array_dims), array_dims)
  for (i in 1:prod(array_dims)) {
    chunk_indices <- which(array_of_chunks_indices == i, arr.ind = TRUE)[1, ]
    found_chunk <- which(found_chunks_str == paste(chunk_indices, collapse = '_'))[1]
    if (length(found_chunk) > 0) {
      array_of_chunks[[i]] <- loadRDS(chunk_files_original[found_chunk])
    }
  }

  # Merge the chunks.
  for (dim_index in length(dim_names):1) {
    dim_sub_array_of_chunks <- NULL
    if (dim_index > 1) {
      dim_sub_array_of_chunks <- sapply(chunks_indices[1:(dim_index - 1)], length)
      names(dim_array_of_chunks) <- dim_names[1:(dim_index - 1)]
    }
  #  for (i in 1:prod(dim_sub_array_of_chunks)) {
  #    
  #    for 
  #  merged_chunks <- list(mod = NULL, obs = NULL)
  #    array_of_chunks <- array(1:length(found_chunk_indices), dim_array_of_chunks)
  result <- list(mod = NULL, obs = NULL)
  ## TODO
  #previous_chunk_indices <- NULL
  #for chunk
  #  .......
  #  result$mod <- MergeArrays(res_lat$mod, res_lon$mod, 'lat')
  #  resilt$obs <- MergeArrays(res_lat$obs, res_lon$obs, 'lat')
  # REMOVE FILES
  #  previous_chunk_indices <- current_chunk_indices
  #}
  #  } else {
  #  }
  }
}

saveRDS(result, file = paste0(shared_dir, '/result.Rds'))

