##############################################

# data1
shp_file1 <- paste0('/esarchive/shapefiles/NUTS3/NUTS_RG_60M_2021_4326.shp/', 
                    'NUTS_RG_60M_2021_4326.shp')
ref_grid1 <- paste0('/esarchive/recon/ecmwf/era5land/monthly_mean/',
                    'tas_f1h/tas_201006.nc')
ref_grid1_1 <- list(lon = seq(10, 40, 0.5), lat = seq(40, 85, 0.5))
NUTS_id1 <- paste0("FI1D", c(1:3, 5, 7:9))  
NUTS_name1 <- list(FI = c('Lappi', 'Kainuu'), SI = c('Pomurska', 'Podravska'))

# data2
shp_file2 <- "/esarchive/shapefiles/gadm_country_mask/gadm_country_ISO3166.shp"
ref_grid2 <- paste0('/esarchive/exp/ecmwf/s2s-monthly_ensfor/weekly_mean/',
                   'tas_f6h/tas_20191212.nc')
GADM_id2 <- c("ESP", "ITA")
GADM_name2 <- c("Spain", "Italy")

##############################################

test_that("1. Input checks", {
  # shp_file
  expect_error(
    ShapeToMask(NULL),
    "Parameter 'shp_file' cannot be NULL."
  )
  expect_error(
    ShapeToMask(1),
    "Parameter 'shp_file' must be a character string."
  )
  # ref_grid
  expect_error(
    ShapeToMask(shp_file1, ref_grid = NULL),
    "Parameter 'ref_grid' cannot be NULL."
  )
  expect_error(
    ShapeToMask(shp_file1, ref_grid = 1),
    "Parameter 'ref_grid' must be either a netCDF file or a list of lon and lat."
  )
  # lon_dim, lat_dim
  expect_error(
    ShapeToMask(shp_file1, lon_dim = 1, lat_dim = 'lat'),
    "Parameter 'lon_dim' must be a character string."
  )
  expect_error(
    ShapeToMask(shp_file1, lat_dim = 1, lon_dim = 'lat'),
    "Parameter 'lat_dim' must be a character string."
  )
  # shp_system, reg_ids, reg_names, shp_col_name_ids
  # reg_level
  # region
  # target_crs
  # check_valid
  # find_min_dist
  # max_dist
  # ncores
})

##############################################

test_that("2. Output", {
  mask1 <- ShapeToMask(shp_file1, ref_grid1, reg_ids = NUTS_id1)
  expect_equal(
    dim(mask1),
    c(lon = 3600, lat = 1801)
  )
  expect_equal(
    sum(mask1),
    c(20899)
  )
  mask2 <- ShapeToMask(shp_file1, ref_grid = ref_grid1_1, reg_ids = NUTS_id1)
  expect_equal(
    sum(mask2),
    830
  )
  expect_equal(
    dim(mask2),
    c(lon = 61, lat = 91)
  )
  mask3 <- ShapeToMask(shp_file1, ref_grid = ref_grid1_1, reg_names = NUTS_name1, 
                       region = TRUE)
  expect_equal(
    dim(mask3),
    c(lon = 61, lat = 91, region = 4)
  )
  expect_equal(
    sum(mask3),
    105
  )
  # Test a single region
  mask3_1 <- ShapeToMask(shp_file1, ref_grid = ref_grid1_1, 
                         shp_col_name_ids = "NUTS_ID", lon_dim = 'lon',
                         lat_dim = 'lat', reg_ids = "DE149",  region = TRUE)
  # Test GADM
  mask4 <- ShapeToMask(shp_file = shp_file2, ref_grid = ref_grid2, 
                       reg_ids = GADM_id2, shp_system = "GADM")
  expect_equal(
    dim(mask4),
    c(longitude = 240, latitude = 121)
  )
  expect_equal(
    sum(mask4),
    48
  )
  mask5 <- ShapeToMask(shp_file = shp_file2, ref_grid = ref_grid2, 
                       reg_names = GADM_name2, shp_system = "GADM", 
                       region = TRUE)
  expect_equal(
    dim(mask5),
    c(longitude = 240, latitude = 121, region = 4)
  )
  expect_equal(
    sum(mask5),
    56
  )
})