#===============================================================
# 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."
  )
  expect_error(
    VizScorecard(data, legend_breaks = 1),
    "Parameter 'legend_breaks' can't be of length 1."
  )
  expect_error(
    VizScorecard(data, legend_breaks = seq(-1, 1, 0.1)),
    paste0("Parameter 'palette' must be provided when 'legend_breaks' exceed ", 
           "the length of 11.")
  )
  # 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' 
#   )
# })