#===============================================================
# 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(
    VizMostLikelyQuantileMap(probs = list(), lon = lons, lat = lats),
    "Parameter 'probs' must be of length >= 1 if provided as a list."
  )
  expect_error(
    VizMostLikelyQuantileMap(probs = list(1:2, a), lon = lons, lat = lats),
    paste0("All arrays in parameter 'probs' must have the same dimension ",
           "sizes and names when 'probs' is provided as a list of arrays.")
  )
  expect_error(
    VizMostLikelyQuantileMap(1:10, lon = lons, lat = lats),
    paste0("Parameter 'probs' 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(
    VizMostLikelyQuantileMap(array(rnorm(240), dim = c(length(lons), length(lats), 3)), 
                                   lon = lons, lat = lats, cat_dim = 'bin'),
    paste0("Specified a dimension name in 'cat_dim' but no dimension names provided ",
           "in 'probs'.")
  )
  expect_error(
    VizMostLikelyQuantileMap(probs = array(rnorm(240), dim = c(lon = length(lons), lat = length(lats), bin = 3)), 
                             lon = lons, lat = lats, cat_dim = 'a'),
    "Dimension 'cat_dim' not found in 'probs'."
  )
  expect_error(
    VizMostLikelyQuantileMap(probs = array(rnorm(240), dim = c(lon = length(lons), lat = length(lats), bin = 3)), 
                             lon = lons, lat = lats, cat_dim = NULL),
    paste0("Parameter 'cat_dim' must be either a numeric value or a ",
           "dimension name.")
  )
  expect_error(
    VizMostLikelyQuantileMap(probs = array(rnorm(240), dim = c(lon = length(lons), lat = length(lats), bin = 3)), 
                             lon = lons, lat = lats, cat_dim = numeric(0)),
    "Parameter 'cat_dim' must be of length 1."
  )
})

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

# VizMostLikelyQuantileMap output tests

save_fun <- function(...) {
  path <- tempfile(fileext = ".png")
  do.call(VizMostLikelyQuantileMap, 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)

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

test_that("2. Output: test triangle ends", {
  expect_snapshot_file(
    save_fun(list(a, b, c), lons, lats, 
             toptitle = 'full range',
             bar_titles = paste('% of belonging to', c('a', 'b', 'c')), 
             brks = 5, bar_extra_margin = rep(0,4)),
    name = 'VizMostLikelyQuantileMap_1a.png' 
  )
  expect_snapshot_file(
    save_fun(list(a, b, c), lons, lats, 
             toptitle = 'upper triangle with upper limit = 70',
             bar_titles = paste('% of belonging to', c('a', 'b', 'c')), 
             brks = 5, bar_extra_margin = rep(0,4),
             triangle_ends = c(F,T), bar_limits = c(40, 70)),
    name = 'VizMostLikelyQuantileMap_1b.png' 
  )
  expect_snapshot_file(
    save_fun(list(a, b, c), lons, lats, 
             toptitle = 'upper triangle with upper limit = 70 and specified col_sup',
             bar_titles = paste('% of belonging to', c('a', 'b', 'c')), 
             brks = 5, bar_extra_margin = rep(0,4),
             triangle_ends = c(F,T), bar_limits = c(40, 70),
             col_sup = list("black", "yellow", "pink")),
    name = 'VizMostLikelyQuantileMap_1c.png' 
  )
  expect_snapshot_file(
    save_fun(list(a, b, c), lons, lats, 
             toptitle = 'Most likely tercile map',
             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 = 'VizMostLikelyQuantileMap_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. Output: triangle ends 2", {
  expect_snapshot_file(
    save_fun(list(a, c, b), lons, lats, 
             toptitle = 'Most likely tercile map',
             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 = 'VizMostLikelyQuantileMap_2a.png' 
  )
})

