#===============================================================
# NOTE: The figures are generated with the following environment:
# - On workstation
# Also: module load Phantomjs
#===============================================================

data <- array(rnorm(1000), dim = c('sdate' = 12, 'metric' = 4, 'region' = 3, 
                                   'time' = 6)) 
row.names <- c('Tropics', 'Extra-tropical NH', 'Extra-tropical SH')
col.names <- c('Mean bias (K)', 'Correlation', 'RPSS','CRPSS')

#--------------------------------------------------------------------
test_that("1. Test input", {
  # Check data
  expect_error(
    VizScorecard('a'),
    "Parameter 'data' must be a numeric array."
  )
  # Check row.dim
  expect_error(
    VizScorecard(data, row.dim = 1),
    "Parameter 'row.dim' must be a character string."
  )
  expect_error(
    VizScorecard(data, row.dim = 'a'),
    "Parameter 'row.dim' is not found in 'data' dimensions."
  )
  # Check row.names
  expect_error(
    VizScorecard(data, row.names = 'a'),
    paste0("Parameter 'row.names' must have the same length of dimension ",
           "'row.dim'.")
  )
  # Check subrow.dim
  expect_error(
    VizScorecard(data, subrow.dim = 1),
    paste0("Parameter 'subrow.dim' must be a character string.")
  )
  expect_error(
    VizScorecard(data, subrow.dim = 'a'),
    paste0("Parameter 'subrow.dim' is not found in 'data' dimensions.")
  )
  # Check subrow.names
  expect_error(
    VizScorecard(data, subrow.names = 'a'),
    paste0("Parameter 'subrow.names' must have the same length of dimension ",
           "'subrow.dim'.")
  )
  # Check col.dim
  expect_error(
    VizScorecard(data, col.dim = 1),
    paste0("Parameter 'col.dim' must be a character string.")
  )
  expect_error(
    VizScorecard(data, col.dim = 'a'),
    paste0("Parameter 'col.dim' is not found in 'data' dimensions.")
  )
  # Check col.names
  expect_error(
    VizScorecard(data, col.names = 'a'),
    paste0("Parameter 'col.names' must have the same length of dimension ",
           "'col.dim'.")
  )
  # Check subcol.dim
  expect_error(
    VizScorecard(data, subcol.dim = 1),
    paste0("Parameter 'subcol.dim' must be a character string.")
  )
  expect_error(
    VizScorecard(data, subcol.dim = 'a'),
    paste0("Parameter 'subcol.dim' is not found in 'data' dimensions.")
  )
  # Check subcol.names
  expect_error(
    VizScorecard(data, subcol.names = 'a'),
    paste0("Parameter 'subcol.names' must have the same length of dimension ",
           "'subcol.dim'.")
  )
  # Check legend.dim
  expect_error(
    VizScorecard(data, legend.dim = 1),
    paste0("Parameter 'legend.dim' must be a character string.")
  )
  expect_error(
    VizScorecard(data, legend.dim = 'a'),
    paste0("Parameter 'legend.dim' is not found in 'data' dimensions.")
  )
  # Check row.title 
  expect_error(
    VizScorecard(data, row.title = 1),
    paste0("Parameter 'row.title' must be a character string.")
  )
  # Check subrow.title 
  expect_error(
    VizScorecard(data, subrow.title = 1),
    paste0("Parameter 'subrow.title' must be a character string.")
  )
  # Check row.title 
  expect_error(
    VizScorecard(data, col.title = 1),
    paste0("Parameter 'col.title' must be a character string.")
  )
  # Check table.title
  expect_error(
    VizScorecard(data, table.title = 1),
    paste0("Parameter 'table.title' must be a character string.")
  )
  # Check table.subtitle
  expect_error(
    VizScorecard(data, table.subtitle = 1),
    paste0("Parameter 'table.subtitle' must be a character string.")
  )
  # Check legend.breaks
  expect_error(
    VizScorecard(data, legend.breaks = list(1)),
    paste0("Parameter 'legend.breaks' must be a list with the same number of ",
           "elements as the length of the 'legend.dim' dimension in data.")
  )
  expect_error(
    VizScorecard(data, legend.breaks = 'a'),
    "Parameter 'legend.breaks' must be a numeric vector, a list or NULL."
  )
  # Check plot.legend
  expect_error(
    VizScorecard(data, plot.legend = 'a'),
    "Parameter 'plot.legend' must be a logical value."
  )
  # Check label.scale
  expect_error(
    VizScorecard(data, label.scale = 'a'),
    "Parameter 'label.scale' must be a numeric value of length 1."
  )
  expect_error(
    VizScorecard(data, label.scale = c(1,2)),
    "Parameter 'label.scale' must be a numeric value of length 1."
  )
  # Check legend.width
  expect_error(
    VizScorecard(data, legend.width = c(1,2)),
    "Parameter 'legend.width' must be a numeric value of length 1."
  )
  expect_error(
    VizScorecard(data, legend.width = 'a'),
    "Parameter 'legend.width' must be a numeric value of length 1."
  )
  # Check legend.height
  expect_error(
    VizScorecard(data, legend.height = c(1,2)),
    "Parameter 'legend.height' must be a numeric value of length 1."
  )
  expect_error(
    VizScorecard(data, legend.height = 'a'),
    "Parameter 'legend.height' must be a numeric value of length 1."
  )
  # Check colour palette input
  expect_error(
    VizScorecard(data, palette = list(1)),
    paste0("Parameter 'palette' must be a list with the same number of ",
           "elements as the length of the 'legend.dim' dimension in data.")
  )
  expect_error(
    VizScorecard(data, palette = list(1,1,1,1)),
    paste0("Parameter 'palette' must be a list of character vectors.")
  )
  expect_error(
    VizScorecard(data, palette = 1),
    "Parameter 'palette' must be a character vector, a list or NULL."
  )
  # Check colorunder
  expect_error(
    VizScorecard(data, colorunder = 1),
    "Parameter 'colorunder' must be a character string vector."
  )
  expect_error(
    VizScorecard(data, colorunder = rep('a', 5)),
    paste0("Parameter 'colorunder' must be a list with the same number of ",
           "elements as the length of the 'legend.dim' dimension in data.")
  )
  # Check colorsup
  expect_error(
    VizScorecard(data, colorsup = 1),
    "Parameter 'colorsup' must be a character string vector."
  )
  expect_error(
    VizScorecard(data, colorsup = rep('a', 5)),
    paste0("Parameter 'colorsup' must be a list with the same number of ",
           "elements as the length of the 'legend.dim' dimension in data.")
  )
  # Check round.decimal
  expect_error(
    VizScorecard(data, round.decimal = 'a'),
    "Parameter 'round.decimal' must be a numeric value of length 1."
  )
  # Check font.size
  expect_error(
    VizScorecard(data, font.size = 'a'),
    "Parameter 'font.size' must be a numeric value of length 1."
  )
  # Check legend white space
  expect_error(
    VizScorecard(data, legend.white.space = 'a'),
    "Parameter 'legend.white.space' must be a numeric value of length 1."
  )
  # Check col1.width
  expect_error(
    VizScorecard(data, col1.width = 'a'),
    "Parameter 'col1.width' must be a numeric value of length 1."
  )
  # Check col2.width
  expect_error(
    VizScorecard(data, col2.width = 'a'),
    "Parameter 'col2.width' must be a numeric value of length 1."
  )
})

# TODO: Test Output


#-------------------------------------------------------------------
# NOTE: A change is detected by expect_snapshot_file but I haven't found the difference

# # Simple example
# # Example with random data
# set.seed(1)
# data <- array(rnorm(1000), dim = c('sdate' = 12, 'metric' = 4, 'region' = 3, 
#                                    'time' = 6)) 
# row.names <- c('Tropics', 'Extra-tropical NH', 'Extra-tropical SH')
# col.names <- c('Mean bias (K)', 'Correlation', 'RPSS','CRPSS')

# save_fun <- function(...) {
#   path <- tempfile(fileext = ".png")
#   do.call(VizScorecard, list(..., fileout = path))
#   path
# }

# test_that("2. Output", {
#   expect_snapshot_file(
#     save_fun(data = data, row.names = row.names, col.names = col.names, 
#              subcol.names =  month.abb[as.numeric(1:12)],
#              row.title = 'Region', subrow.title = 'Forecast Month', 
#              col.title = 'Start date', 
#              table.title = "Temperature of ECMWF System 5", 
#              table.subtitle = "(Ref: ERA5 1994-2016)",
#              plot.legend = TRUE),
#     name = 'VizScorecard_1a.png' 
#   )
# })