#===============================================================
# NOTE: The figures are generated with the following environment:
#- On workstation
#- R/4.1.2-foss-2015a-bare
#- GDAL/2.2.1-foss-2015a
#- PROJ/4.8.0-foss-2015a
#- GEOS/3.7.2-foss-2015a-Python-2.7.9
#===============================================================

# data1: global
set.seed(19)
data1 <- array(rep(seq(-10, 10, length.out = 181), 360) + rnorm(360), 
               dim = c(sdate = 1, lat = 181, lon = 360))
mask1 <- drop(data1)
mask1[which(mask1 > 9 & mask1 < -9)] <- 0
mask1[which(mask1 != 0)] <- 1
dots1 <- mask1
lon1_1 <- -180:179
lon1_2 <- 0:359
lat1 <- -90:90

data1_NA <- data1; data1_NA[10000:15000] <- NA

# data2: Europe
data2 <- data1[,21:61, 161:221]
lon2 <- lon1_1[161:221]
lat2 <- 70:30
dots2 <- array(t(dots1[21:61, 161:221]), dim = c(lon = length(lon2), lat = length(lat2)))

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

test_that("1. Input checks", {

  # data
  expect_error(
    VizRobinson(array(data1*2, dim = c(sdate = 2, lat = 181, lon = 360))),
    "Parameter 'data' must have two dimensions."
  )  
  # lon, lon_dim
  tmp <- data1; dim(tmp) <- c(sdate = 1, latt = 181, lonn = 360)
  expect_error(
    VizRobinson(tmp), 
    "Cannot find known longitude name in data dimension. Please define parameter 'lon_dim'."
  )
  expect_error(
    VizRobinson(data1, lon = 1:10),
    "Length of parameter 'lon' should match longitude dimension in 'data'."
  )
  # style
  expect_error(
    VizRobinson(data1, lon = lon1_1, lat = lat1, style = 'line'),
    "Parameter 'style' must be 'point' or 'polygon'."
  )
  # dots
  expect_error(
    VizRobinson(data1, lon = lon1_1, lat = lat1, dots = c(1, 0)),
    "Parameter 'dots' must have two dimensions named as longitude and latitude dimensions in 'data'."
  )
  tmp <- dots1; dim(tmp) <- c(sdate = 1, lat = 360, lon = 181)
  expect_error(
    VizRobinson(data1, lon = lon1_1, lat = lat1, dots = tmp),
    "Parameter 'dots' must have the same dimensions as 'data'."
  )
  tmp <- dots1; tmp[1] <- NA
  expect_error(
    VizRobinson(data1, lon = lon1_1, lat = lat1, dots = tmp),
    "Parameter 'dots' must have only TRUE/FALSE or 0/1."
  )
  # colNA
  expect_error(
    VizRobinson(data1, lon = lon1_1, lat = lat1, colNA = 'fair'),
    "Parameter 'colNA' must be a valid colour identifier."
  )
  # toptitle
  expect_error(
    VizRobinson(data1, lon = lon1_1, lat = lat1, toptitle = 1:10),
    "Parameter 'toptitle' must be a character string."
  )
  # caption
  expect_error(
    VizRobinson(data1, lon = lon1_1, lat = lat1, caption = 1:10),
    "Parameter 'caption' must be a character string."
  )
  # crop_coastlines
  expect_error(
    VizRobinson(data1, lon = lon1_1, lat = lat1, crop_coastlines = c(x1 = 1, x2 = 10, y1 = -10, y2 = 10)),
    "Parameter 'crop_coastlines' needs to have names 'latmax', 'latmin', 'lonmax', 'lonmin'."
  )
  # point_size
  expect_error(
    VizRobinson(data1, lon = lon1_1, lat = lat1, point_size = 'small'),
    "Parameter 'point_size' must be a number."
  )

})

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

test_that("2. data1: Global", {
  # up triangle end only
  expect_snapshot_file(
    save_VizRobinson(data1, lon = lon1_1, lat = lat1, dots = dots1, brks = seq(-12.6, 12.6, 1), vertical = F, toptitle = 'VizRobinson', caption = 'unit test: 1a'),
    name = 'VizRobinson_1a.png' 
  )
  # NAs
  expect_snapshot_file(
    save_VizRobinson(data1_NA, lon = lon1_1, lat = lat1, dots = dots1, brks = seq(-13, 13, 2), toptitle = 'VizRobinson', caption = 'unit test: 1b', bar_extra_margin = c(5, 0, 5, 0)),
    name = 'VizRobinson_1b.png'
  )

  expect_snapshot_file(
    save_VizRobinson(data1, lon = lon1_1, lat = lat1, mask = mask1, brks = seq(-11, 11, 2), color_fun = ClimPalette('purpleorange'), colNA = 'red', toptitle = 'VizRobinson', caption = 'unit test: 1c'),
    name = 'VizRobinson_1c.png'
  )
})


test_that("3. data2: Europe", {

  expect_snapshot_file(
    save_VizRobinson(data2, lon = lon2, lat = lat2, dots = dots2, target_proj = 102014,
      brks = seq(-10, 0, 2), cols = c("#FDD49E", "#FDBB84", "#FC8D59", "#E34A33", "#B30000"), col_inf = "#FEF0D9",
      toptitle = 'VizRobinson', caption = 'unit test: 1a\n projection: Lambert Europe (ESRI:102014)', point_size = 1.1, title_size = 12,
      width = 6, vertical = F, bar_extra_margin = c(0.5, 4, 0.5, 4), crop_coastlines = c(latmin = 27, latmax = 70, lonmin = -20, lonmax = 40)),
    name = 'VizRobinson_2a.png'
  )

  # polygon
  # NOTE: This test returns error in WS (R 4.1.2 (2021-11-01)) and works on HUB (4.2.1 (2022-06-23))
  # expect_snapshot_file(
  # save_VizRobinson(data2, lon = lon2, lat = lat2, dots = dots2, target_proj = 102014,
  #   brks = seq(-10, 0, 2), cols = c("#FDD49E", "#FDBB84", "#FC8D59", "#E34A33", "#B30000"), col_inf = "#FEF0D9",
  #   toptitle = 'VizRobinson', caption = 'unit test: 1a\n projection: Lambert Europe (ESRI:102014)', point_size = 1.1, title_size = 12,
  #   width = 6, vertical = F, bar_extra_margin = c(0.5, 4, 0.5, 4), crop_coastlines = c(latmin = 27, latmax = 70, lonmin = -20, lonmax = 40), style = 'polygon'),
  # name = 'VizRobinson_2b.png'
  # )
})
