
library(CSTools)

# dat1
threshold <- 0.9
dat1 <- array(abs(rnorm(5 * 3 * 214 * 2)*50),
                  c(member = 5, sdate = 3, time = 214, lon = 2)) 
dates0 <- c(seq(as.Date("01-05-2000", format = "%d-%m-%Y"), 
                         as.Date("30-11-2000", format = "%d-%m-%Y"), by = 'day'),
                     seq(as.Date("01-05-2001", format = "%d-%m-%Y"), 
                         as.Date("30-11-2001", format = "%d-%m-%Y"), by = 'day'),
                     seq(as.Date("01-05-2002", format = "%d-%m-%Y"), 
                         as.Date("30-11-2002", format = "%d-%m-%Y"), by = 'day'))
dates1 <- dates0
dim(dates1) <- c(sdate = 3, time = 214)

##############################################
test_that("1. Sanity checks", {
  expect_error(
    Threshold(NULL),
    "Parameter 'data' cannot be NULL."
  )
  expect_error(
    Threshold('x'),
    "Parameter 'data' must be numeric."
  )
  data <- 1:20
  expect_error(
    Threshold(data, NULL),
    "Parameter 'threshold' cannot be NULL."
  )
  expect_error(
    Threshold(data, 'x'),
    "Parameter 'threshold' must be numeric."
  )
  threshold <- 0.9
  expect_equal(
    Threshold(data, threshold), 
    18.1
  )
  dim(data) <- c(2, 10)
  expect_error(
    Threshold(data, threshold),
    "Parameter 'data' must have named dimensions."
  )
  names(dim(data)) <- c('lat', 'sdate')
  expect_error(
    Threshold(data, threshold),
    "Could not find dimension 'member' in 1th object provided in 'data'."
  )
  expect_equal(
    Threshold(data, threshold, memb_dim = NULL),
    array(c(17.2, 18.2), c(lat = 2))
  )
  threshold <- c(0.1, 0.2)
  expect_equal(
    Threshold(data, threshold, memb_dim = NULL),
    array(c(2.8, 4.6, 3.8, 5.6), c(probs = 2, lat = 2))
  )
  data <- array(1:40, c(x = 2, time = 20))
  expect_error(
    Threshold(data, threshold),
    "Could not find dimension 'sdate' in 1th object provided in 'data'."
  )
  expect_equal(
    dim(Threshold(data, threshold, sdate_dim = 'time', memb_dim = NULL)),
    c(probs = 2, x = 2)
  )
  # threshold with dimensions ?
  dim(threshold) <- c(member = 2, time = 1)
  expect_equal(
    dim(Threshold(data, threshold, sdate_dim = 'time', memb_dim = NULL)),
    c(probs = 2, x = 2)
  )
  expect_equal(
    dim(Threshold(data, threshold, memb_dim = 'x', sdate_dim = 'time')),
    c(probs = 2)
  )
  # test start and end
  expect_warning(
    Threshold(dat1, threshold = 0.9, dates = dates0, start = list(21, 4), 
              end = list(21, 6)),
    paste0("Parameter 'dates' must have named dimensions if 'start' and ",
           "'end' are not NULL. All data will be used.")
  )
  expect_equal(
    dim(Threshold(dat1, threshold = 0.8, dates = dates1, start = list(21, 4), 
                  end = list(21, 6))),
    c(time = 52, lon = 2)
  )
  # start and end when dates is not provided
  expect_warning(
    Threshold(array(1:366, dim = c(time = 61, sdate = 3, member = 2)), 
              threshold = 0.8, start = list(05, 02), end = list(05, 09)),
    paste0("Parameter 'dates' is NULL and the average of the full data ", 
           "provided in 'data' is computed.")
  )
})

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

test_that("2. Seasonal forecasts", {
  exp <- CSTools::lonlat_temp$exp$data
  thresholdP <- Threshold(exp, threshold = 0.9)
  expect_equal(
    dim(exp)[4:6], 
    dim(thresholdP)[2:4]
  )
  expect_equal(
    round(thresholdP[1, , 2, 2]), 
    c(283, 281, 280)
  )
  exp1 <- exp[1, 1, 1, , , ] # no member
  exp1 <- s2dv::InsertDim(InsertDim(exp1, 1, 1, name = 'sdate'), 1, 1, name = 'member') # 1 member and 1 sdate
  exp1_thresholdP <- Threshold(exp1, threshold = 0.9)
  expect_equal(
    round(exp1_thresholdP[, 2, 2]), 
    c(281, 279, 276)
  )
})
