library(testthat)
library(esviz)

path <- "/home/Earth/abatalla/ESVIZ_RS_GIT/esviz/R/" ## change
ff <- lapply(list.files(path), function(x) paste0(path, x))
invisible(lapply(ff, source))

#test_check("test-VizEquiMap.R")
#testthat::test_file()
#testthat::test_file("tests/testthat/test-VizEquiMap.R")


#===============================================================
# data
#===============================================================

# data1
set.seed(0)
data1 <- array(rep(0:10, each = 120),dim = c(lat = 30, lon = 44))
lons1 <- -10:33
lats1 <- 31:60
cols_white_red1 <- s2dv::clim.palette("bluered")(41)[21:41]



#===============================================================
# tests
#===============================================================


test_that("1. Input checks", {
  
  expect_error(
    VizEquiMap(lon = c()), 
    "Parameters 'lon' and 'lat' must be numeric vectors."
  )
  expect_error(
    VizEquiMap(lon = lons1, lat = c()), 
    "Parameters 'lon' and 'lat' must be numeric vectors."
  )
  expect_error(
    VizEquiMap(var = NULL, lon = lons1, lat = lats1), 
    "Parameter 'var' cannot be NULL."
  )
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, varu = "test1"), 
    "Only one of the components 'varu' or 'varv' has been provided. Both must be provided."
  )
  expect_error(
    VizEquiMap(var = data1, lon = lons1, lat = lats1, varu = "test1", varv = "test2"), 
    "Parameter 'varu' must be a numerical array with two dimensions."
  )
  
})



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

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


test_that("2. Output checks", {

  expect_snapshot_file(
    save_fun(var = data1, lon = lons1, lat = lats1, filled.continents = F,
             bar_limits = c(0, 10), toptitle = "plot 1",
             cols = cols_white_red1, colNA = "grey", triangle_ends = c(F, F)),
    name = 'VizEquiMap_1a.png'
  )
  expect_snapshot_file(
    save_fun(var = data1, lon = lons1, lat = lats1, filled.continents = F,
             bar_limits = c(0, 10), toptitle = "plot 1",
             cols = cols_white_red1, colNA = "grey", triangle_ends = c(F, F),
             include_lower_boundary = FALSE),
    name = 'VizEquiMap_1b.png'
  )
  expect_snapshot_file(
    save_fun(var = data1, lon = lons1, lat = lats1, filled.continents = F,
             bar_limits = c(0, 10), toptitle = "plot 1",
             cols = cols_white_red1, colNA = "grey", triangle_ends = c(F, F),
             include_upper_boundary = FALSE),
    name = 'VizEquiMap_1c.png'
  )
  expect_snapshot_file(
    save_fun(var = data1, lon = lons1, lat = lats1, filled.continents = F,
             bar_limits = c(0, 10), toptitle = "plot 1",
             cols = cols_white_red1, colNA = "grey", triangle_ends = c(F, F),
             include_lower_boundary = FALSE, include_upper_boundary = FALSE),
    name = 'VizEquiMap_1d.png'
  )

})

# testthat::test_file("tests/testthat/test-VizEquiMap.R")
  # [ FAIL 0 | WARN 0 | SKIP 0 | PASS 9 ]
# testthat::snapshot_review('VizEquiMap/')
  # No snapshots to update



# #----------------------------------------------------------------
# OTHER TESTS ####
# #----------------------------------------------------------------
# 
# test_that("2. Output checks", {
#   
#   # Save the plot to a file
#   png(plot_file)
#   VizEquiMap(var = data1, lon = lons1, lat = lats1, filled.continents = F,
#              bar_limits = c(0, 10), toptitle = "plot 1",
#              cols = cols_white_red1, colNA = "grey", triangle_ends = c(F, F))
#   dev.off()
#   
#   # Expect the plot file to match the snapshot
#   expect_snapshot_file(plot_file, name = 'VizEquiMap_1.png')
# })
# 
# #----------------------------------------------------------------
# 
# test_that("2. Output checks", {
#   
#   expect_snapshot_file(
#     VizEquiMap(var = data1, lon = lons1, lat = lats1, filled.continents = F,
#                bar_limits = c(0, 10), toptitle = "plot 1",
#                cols = cols_white_red1, colNA = "grey", triangle_ends = c(F, F)),
#     name = 'VizEquiMap_1.png'
#   )
#   
# })
# 
# #----------------------------------------------------------------
# 
# save_fun <- function(fun, height = NULL, width = NULL, ...) {
#   path <- tempfile(fileext = ".png")
#   png(path, height = height, width = width)
#   do.call(fun, list(...))
#   dev.off()
#   path
# }
# 
# 
# test_that("2. Output checks", {
#   
#   expect_snapshot_file(
#     save_fun(fun = VizEquiMap, var = data1, lon = lons1, lat = lats1, filled.continents = F,
#              bar_limits = c(0, 10), toptitle = "plot 1",
#              cols = cols_white_red1, colNA = "grey", triangle_ends = c(F, F)),
#     name = 'VizEquiMap_1.png'
#   )
#   
#   expect_snapshot_file(
#     save_fun(fun = ColorBarDiscrete, height = 180, width = 360,
#              brks = 0:4, cols = paste0("green", 1:4), title = "Greeness", vertical = F,
#              labels = paste0("level ", 1:4)),
#     name = 'ColorBarDiscrete_1a.png' 
#   )
#   
# })
# 
# 
# #----------------------------------------------------------------
# 
# expect_snapshot_file(
#   VizEquiMap(var = data1, lon = lons1, lat = lats1, filled.continents = F,
#              bar_limits = c(0, 10), toptitle = "plot 1",
#              cols = cols_white_red1, colNA = "grey", triangle_ends = c(F, F)),
#   name = 'VizEquiMap_1a.png'
# )
# 

