#===============================================================
# NOTE: The figures are generated with the following environment:
# - On workstation
#===============================================================

# Simple example
x <- array(1:(20 * 10), dim = c(lat = 10, lon = 20)) / 200
a <- x * 0.6
b <- (1 - x) * 0.6
c <- 1 - (a + b)
lons <- seq(0, 359.5, length = 20)
lats <- seq(-89.5, 89.5, length = 10)

#--------------------------------------------------------------------

test_that("1. Input checks", {
  # check probs
  expect_error(
    VizCombinedMap(maps = list(), lon = lons, lat = lats),
    "Parameter 'maps' must be of length >= 1 if provided as a list."
  )
  expect_error(
    VizCombinedMap(maps = list(1:2, a), lon = lons, lat = lats),
    paste0("All arrays in parameter 'maps' must have the same dimension ",
           "sizes and names when 'maps' is provided as a list of arrays.")
  )
  expect_error(
    VizCombinedMap(1:10, lon = lons, lat = lats),
    paste0("Parameter 'maps' must be either a numeric array with 3 dimensions ",
           " or a list of numeric arrays of the same size with the 'lon' and ",
           "'lat' dimensions.")
  )
  # check cat_dim
  expect_error(
    VizCombinedMap(array(rnorm(240), dim = c(length(lons), length(lats), 3)), 
                   lon = lons, lat = lats, map_dim = 'bin'),
    paste0("Specified a dimension name in 'map_dim' but no dimension names provided ",
           "in 'maps'.")
  )
  expect_error(
    VizCombinedMap(maps = array(rnorm(240), dim = c(lon = length(lons), lat = length(lats), bin = 3)), 
                   lon = lons, lat = lats, cat_dim = 'a'),
    "Dimension 'map_dim' not found in 'maps'."
  )
  expect_error(
    VizCombinedMap(maps = array(rnorm(240), dim = c(lon = length(lons), lat = length(lats), bin = 3)), 
                   lon = lons, lat = lats, map_dim = NULL),
    paste0("Parameter 'map_dim' must be either a numeric value or a ",
           "dimension name.")
  )
  expect_error(
    VizCombinedMap(maps = array(rnorm(240), dim = c(lon = length(lons), lat = length(lats), bin = 3)), 
                   lon = lons, lat = lats, map_dim = numeric(0)),
    "Parameter 'map_dim' must be of length 1."
  )
})

#--------------------------------------------------------

# VizMostLikelyQuantileMap output tests

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

#--------------------------------------------------------

# Test triangle_ends
# Synthetic data
x <- array(1:(20 * 10), dim = c(lat = 10, lon = 20)) / 200
a <- x * 0.85
b <- (1 - x) * 0.85
c <- 1 - (a + b)
lons <- seq(200, 359.5, length = 20)
lats <- seq(49.5, 89.5, length = 10)

Lon <- seq(200, 359.5, length = 51)
Lat <- seq(49.5, 89.5, length = 26)
set.seed(1)
data <- rnorm(51 * 26 * 3)
dim(data) <- c(map = 3, lon = 51, lat = 26)
mask <- sample(c(0,1), replace = TRUE, size = 51 * 26)
dim(mask) <- c(lat = 26, lon = 51)

bar_limits_each <- list(c(40, 70), c(40, 85), c(30, 70))
brks_each <- list(5,6,8)

test_that("2. Test outputs", {
  expect_snapshot_file(
    save_fun(list(a, b, c), lons, lats, 
             toptitle = 'full range',
             map_select_fun = max,
             display_range = c(0, 1),
             bar_titles = paste('% of belonging to', c('a', 'b', 'c')), 
             brks = 5, bar_extra_margin = rep(0,4)),
    name = 'VizCombinedMap_1a.png' 
  )
  expect_snapshot_file(
    save_fun(list(a, b, c), lons, lats, 
             toptitle = 'Combined map',
             map_select_fun = max,
             display_range = c(0, 1),
             bar_titles = paste('% of belonging to', c('a', 'b', 'c')), 
             brks = brks_each, bar_extra_margin = rep(0,4),
             triangle_ends = c(F, T), bar_limits = bar_limits_each,
             col_sup = list("black", "yellow", "pink")),
    name = 'VizCombinedMap_1b.png' 
  )
  expect_snapshot_file(
    save_fun(data, Lon, Lat, 
             map_select_fun = max,
             display_range = range(data),
             mask = mask,
             width = 14, height = 10, brks = 5, 
             triangle_ends = c(F,T), bar_limits = range(data)),
    name = 'VizCombinedMap_1c.png' 
  )
  expect_snapshot_file(
    save_fun(data, Lon, Lat, 
             map_select_fun = max,
             display_range = range(data),
             mask = mask,
             width = 14, height = 10, brks = 5, 
             triangle_ends = c(F, T), bar_limits = range(data),
             col_sup = list("black", "yellow", "pink")),
    name = 'VizCombinedMap_1d.png' 
  )
})

#--------------------------------------------------------
# Test triangle ends different category

x <- array(1:(20 * 10), dim = c(lat = 10, lon = 20)) / 200
a <- x * 0.88
b <- (1 - x) * 0.88; b[c(1, 25, 26, 33)] <- b[c(1, 25, 26, 33)] - 0.4
c <- 1 - (a + b); c[c(1, 25, 26, 33)] <- c[c(1, 25, 26, 33)] + 0.4
lons <- seq(200, 359.5, length = 20)
lats <- seq(49.5, 89.5, length = 10)

bar_limits_each <- list(c(40, 85), c(40, 85), c(40, 85))
brks_each <- list(4,2,4)

test_that("3. Test output 2", {
  expect_snapshot_file(
    save_fun(list(a, c, b), lons, lats, 
             toptitle = 'Combined map',
             map_select_fun = max,
             display_range = c(0, 1),
             bar_titles = paste('% of belonging to', c('a', 'b', 'c')), 
             brks = brks_each, bar_extra_margin = rep(0,4),
             triangle_ends = c(F, T), bar_limits = bar_limits_each,
             cols = list(c("#6BAED6", "#4F9BCB", "#3787C0"),
                         c("grey"),
                         c("#FC4E2A", "#EB2B20", "#D6111F")),
             col_sup = list("#08519CFF", "grey", "#800026FF")),
    name = 'VizCombinedMap_2a.png' 
  )
})
