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

library(s2dv)

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

test_that("1. Sanity checks", {
  expect_error(
    CST_SelectPeriodOnData(1:10),
    paste0("Parameter 'data' must be of the class 's2dv_cube'.")
  )
  expect_error(SelectPeriodOnDates('x', start = list(1,1), end = list(1,1)), "invalid 'trim' argument")
})

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

test_that("2. Output checks", {
  # Lluis issue #8:
  dates <- c(seq(as.Date("02-05-1993", "%d-%m-%Y", tz = 'UTC'),
                 as.Date("01-12-1993","%d-%m-%Y", tz = 'UTC'), "day"),
             seq(as.Date("02-05-1994", "%d-%m-%Y", tz = 'UTC'),
                 as.Date("01-12-1994","%d-%m-%Y", tz = 'UTC'), "day"),
             seq(as.Date("02-05-1995", "%d-%m-%Y", tz = 'UTC'),
                 as.Date("01-12-1995","%d-%m-%Y", tz = 'UTC'), "day"))
  dim(dates) <- c(time = 214, file_date = 3)
  output <- c(seq(as.Date("21-06-1993", "%d-%m-%Y", tz = 'UTC'),
                  as.Date("21-09-1993","%d-%m-%Y", tz = 'UTC'), "day"),
              seq(as.Date("21-06-1994", "%d-%m-%Y", tz = 'UTC'),
                  as.Date("21-09-1994","%d-%m-%Y", tz = 'UTC'), "day"),
              seq(as.Date("21-06-1995", "%d-%m-%Y", tz = 'UTC'),
                  as.Date("21-09-1995","%d-%m-%Y", tz = 'UTC'), "day"))
  dim(output) <- c(time = 93, file_date = 3)
  expect_equal(
    SelectPeriodOnDates(dates, list(21,6),list(21,9), time_dim = 'time'), 
    output
  )
  dates <- s2dv::Reorder(dates, c('file_date', 'time'))
  output <- s2dv::Reorder(output, c('file_date', 'time'))
  expect_equal(
    SelectPeriodOnDates(dates, list(21,6),list(21,9), time_dim = 'time'), 
    output
  )
  # test different common dimensions
  
  exp <- array(1:61, dim = c(time = 61))
  Dates <- c(seq(as.Date("01-05-2000", format = "%d-%m-%Y"), 
                as.Date("30-06-2000", format = "%d-%m-%Y"), by = 'day'),
            seq(as.Date("01-05-2001", format = "%d-%m-%Y"), 
                as.Date("30-06-2001", format = "%d-%m-%Y"), by = 'day'),
            seq(as.Date("01-05-2002", format = "%d-%m-%Y"), 
                as.Date("30-06-2002", format = "%d-%m-%Y"), by = 'day'))
  dim(Dates) <- c(time = 61, sdate = 3)
  res <- SelectPeriodOnData(data = exp, dates = Dates, 
                            start = list(21, 4), end = list(21, 6))
  expect_equal(
    dim(res),
    c(time = 52)
  )
})

##############################################
test_that("3. Decadal", { 
 # -------- DECADAL ----------#
  # decadal: 1 sdate several consequtive years:
  dates <- seq(as.Date("01-01-2000", "%d-%m-%Y", tz = 'UTC'),
               as.Date("31-12-2005","%d-%m-%Y", tz = 'UTC'), "day")
  dim(dates) <- c(time = length(dates))
  # No dims -> test .position
  output <- c(
      seq(as.Date("2000-02-01", "%Y-%m-%d"), as.Date("2000-02-10", "%Y-%m-%d"), 'day'),
      seq(as.Date("2001-02-01", "%Y-%m-%d"), as.Date("2001-02-10", "%Y-%m-%d"), 'day'),
      seq(as.Date("2002-02-01", "%Y-%m-%d"), as.Date("2002-02-10", "%Y-%m-%d"), 'day'),
      seq(as.Date("2003-02-01", "%Y-%m-%d"), as.Date("2003-02-10", "%Y-%m-%d"), 'day'),
      seq(as.Date("2004-02-01", "%Y-%m-%d"), as.Date("2004-02-10", "%Y-%m-%d"), 'day'),
      seq(as.Date("2005-02-01", "%Y-%m-%d"), as.Date("2005-02-10", "%Y-%m-%d"), 'day'))
  dim(output) <- c(time = 60)
  expect_equal(
    SelectPeriodOnDates(dates, start = list(1, 2), end = list(10, 2)),
    output
  )
  data <- array(1:(length(dates)*3),
                c(memb = 1, time = length(dates), lon = 3))

  expect_equal(
    SelectPeriodOnData(data, dates, start = list(1, 2), end = list(10, 2)),
    array(c(c(32:41, 398:407, 763:772, 1128:1137, 1493:1502, 1859:1868),
            c(32:41, 398:407, 763:772, 1128:1137, 1493:1502, 1859:1868) + 2192,
            c(32:41, 398:407, 763:772, 1128:1137, 1493:1502, 1859:1868) + 2 * 2192),
          c(memb = 1, time = 60, lon = 3))
  )    
  
  output2 <- c(
      seq(as.Date("2000-02-01", "%Y-%m-%d"), as.Date("2000-04-10", "%Y-%m-%d"), 'day'),
      seq(as.Date("2001-02-01", "%Y-%m-%d"), as.Date("2001-04-10", "%Y-%m-%d"), 'day'),
      seq(as.Date("2002-02-01", "%Y-%m-%d"), as.Date("2002-04-10", "%Y-%m-%d"), 'day'),
      seq(as.Date("2003-02-01", "%Y-%m-%d"), as.Date("2003-04-10", "%Y-%m-%d"), 'day'),
      seq(as.Date("2004-02-01", "%Y-%m-%d"), as.Date("2004-04-10", "%Y-%m-%d"), 'day'),
      seq(as.Date("2005-02-01", "%Y-%m-%d"), as.Date("2005-04-10", "%Y-%m-%d"), 'day'))
  dim(output2) <- c(time = 416)
  expect_equal(
    SelectPeriodOnDates(dates, start = list(1, 2), end = list(10, 4)),
    output2
  )
  
  expect_equal(
    SelectPeriodOnData(data, dates, start = list(1, 2), end = list(10, 4)),
    array(c(c(32:101, 398:466, 763:831, 1128:1196, 1493:1562, 1859:1927),
            c(32:101, 398:466, 763:831, 1128:1196, 1493:1562, 1859:1927) + 2192,
            c(32:101, 398:466, 763:831, 1128:1196, 1493:1562, 1859:1927) + 2 * 2192),
          c(memb = 1, time = 416, lon = 3))
  )

  # 1 dim -> test Apply
  dim(dates) <- c(time = length(dates))
  expect_equal(
    SelectPeriodOnDates(dates, start = list(1, 2), end = list(10, 2)),
    output
  ) # no need to check on Data, repited
  expect_equal(
    SelectPeriodOnDates(dates, start = list(1, 2), end = list(10, 4)),
    output2
  ) # no need to check on Data, repited
  
  # decadal: 5 sdates several consequtive years
  dates <- rep(seq(as.Date("01-01-2000", "%d-%m-%Y", tz = 'UTC'),
               as.Date("31-12-2005","%d-%m-%Y", tz = 'UTC'), "day"), 5)
  dim(dates) <- c(time = 2192, sdate = 5)
  output3 <- rep(output, 5)
  dim(output3) <- c(time = 60, sdate = 5)
  expect_equal(
    SelectPeriodOnDates(dates, start = list(1, 2), end = list(10, 2)),
    output3)

  data <- array(1:(length(dates)*3),
                c(memb = 1, sdate = 5, time = length(dates)/5, lon = 3))
  expect_equal( #To be extended for all sdate dimensions:
    SelectPeriodOnData(data, dates, start = list(1, 2), end = list(10, 2))[1,1, ,1],
    c(1:10 * 5 + 151, 1:10 * 5 + 1981, 1:10 * 5 + 3806,
              1:10 * 5 + 5631, 1:10 * 5 + 7456, 1:10 * 5 + 9286)
  )
  output4 <- rep(output2, 5)
  dim(output4) <- c(time = 416, sdate = 5)
  expect_equal(
    SelectPeriodOnDates(dates, start = list(1, 2), end = list(10, 4)),
    output4
  )
  
  expect_equal( #To be extended for all time dimensions:
    SelectPeriodOnData(data, dates, start = list(1, 2), end = list(10, 4))[1, ,1,1],
    156:160
  )

  # Multiple dims: sdate, fyear, time
  dates <- CSTools::SplitDim(dates, indices = dates[,1],
                             split_dim = 'time', freq = 'year')
  dates <- as.POSIXct(dates * 24 * 3600, origin = '1970-01-01', tz = 'UTC')
  output5 <- CSTools::SplitDim(output3, indices = output3[,1], split_dim = 'time' , freq = 'year')
  output5 <- as.POSIXct(output5 * 24 * 3600, origin = '1970-01-01', tz = 'UTC')
  expect_equal(
    SelectPeriodOnDates(dates, start = list(1, 2), end = list(10, 2)),
    output5
  )
  data <- array(1:(366*6*5*3),
               c(memb = 1, sdate = 5, year = 6, time = 366, lon = 3))
  expect_equal(
    SelectPeriodOnData(data, dates, start = list(1, 2), end = list(10, 2)),
    InsertDim(Reorder(data[, , , 32:41, ], c('sdate', 'year', 'time', 'lon')),
           len = 1, pos = 1, name = 'memb')
  )
  output6 <- CSTools::SplitDim(output4, indices = output4[,1], split_dim = 'time' , freq = 'year')
  output6 <- as.POSIXct(output6 * 24 * 3600, origin = '1970-01-01', tz = 'UTC')
  expect_equal(
    SelectPeriodOnDates(dates, start = list(1, 2), end = list(10, 4)),
    output6
  )
  # expect_equal( # to be fixed:
  #  SelectPeriodOnData(data, dates, start = list(1, 2), end = list(10, 4)),
  #   (931:935), outer(seq(931, 3001, 30), 0:4, '+')
  #  InsertDim(Reorder(data[,,,32:41,], c('time', 'sdate', 'year', 'lon')),
  #         len = 1, pos = 2, name = 'memb'))
})

##############################################
test_that("4. Seasonal", {
  # 1 start month, select the required 'time' of each 'sdate' in-between the entire timeseries 
    dates <- c(seq(as.Date("01-04-2000", format = "%d-%m-%Y"),
                   as.Date("31-10-2000", format = "%d-%m-%Y"), by = 'day'),
               seq(as.Date("01-04-2001", format = "%d-%m-%Y"),
                   as.Date("31-10-2001", format = "%d-%m-%Y"), by = 'day'),
               seq(as.Date("01-04-2002", format = "%d-%m-%Y"),
                   as.Date("31-10-2002", format = "%d-%m-%Y"), by = 'day'),
               seq(as.Date("01-04-2003", format = "%d-%m-%Y"),
                   as.Date("31-10-2003", format = "%d-%m-%Y"), by = 'day'))
    dim(dates) <- c(time = 214, sdate = 4)
    output <- c(seq(as.Date("21-04-2000", format = "%d-%m-%Y"),
                    as.Date("21-06-2000", format = "%d-%m-%Y"), by = 'day'),
                seq(as.Date("21-04-2001", format = "%d-%m-%Y"),
                     as.Date("21-06-2001", format = "%d-%m-%Y"), by = 'day'),
                seq(as.Date("21-04-2002", format = "%d-%m-%Y"),
                     as.Date("21-06-2002", format = "%d-%m-%Y"), by = 'day'),
                seq(as.Date("21-04-2003", format = "%d-%m-%Y"),
                   as.Date("21-06-2003", format = "%d-%m-%Y"), by = 'day'))
    dim(output) <- c(time = 62, sdate = 4)
    expect_equal(
      SelectPeriodOnDates(dates, start = list(21, 4), end = list(21, 6)),
      output
    )
  
    # following the above case, and select the data
    data <- array(1:(5 * 4 * 214 * 2),             
                  c(memb = 5, sdate = 4, time = 214, lon = 2))
    dim(dates) <- c(time = 214, sdate = 4)
  
    expect_equal(
      SelectPeriodOnData(data, dates, start = list(21, 4), end = list(21, 6))[1,1, ,1],
      data[1,1,21:82,1]
    )


    # when selecting the days across two years
    dates <- seq(as.Date("2000-01-01", "%Y-%m-%d"), as.Date("2003-12-31", "%Y-%m-%d"), 'day')
    dim(dates) <- c(time = 1461)
    
    output1 <- c(seq(as.Date("01-01-2000", format = "%d-%m-%Y"),
                     as.Date("31-01-2000", format = "%d-%m-%Y"), by = 'day'),
                 seq(as.Date("01-12-2000", format = "%d-%m-%Y"),
                     as.Date("31-01-2001", format = "%d-%m-%Y"), by = 'day'),
                 seq(as.Date("01-12-2001", format = "%d-%m-%Y"),
                     as.Date("31-01-2002", format = "%d-%m-%Y"), by = 'day'),
                 seq(as.Date("01-12-2002", format = "%d-%m-%Y"),
                     as.Date("31-01-2003", format = "%d-%m-%Y"), by = 'day'),
                 seq(as.Date("01-12-2003", format = "%d-%m-%Y"),
                     as.Date("31-12-2003", format = "%d-%m-%Y"), by = 'day'))
    dim(output1) <- c(time = 31 * 8)

    expect_equal(
      SelectPeriodOnDates(dates, start = list(1, 12), end = list(31, 1)),
      output1
    )

    # following the above case, and select the data
    data1 <- array(1:(length(dates) * 2),
                   c(memb = 1, time = length(dates), lon = 2))
    expect_equal(
      SelectPeriodOnData(data1, dates, start = list(1, 12), end = list(31, 1)),
      array(c(c(1:31, 336:397, 701:762, 1066:1127, 1431:1461),
              c(1:31, 336:397, 701:762, 1066:1127, 1431:1461) + 1461),
            c(memb = 1, time = 31 * 8, lon = 2))
    )
})

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

test_that("5. Test sample data", {
  dates <- c(seq(as.POSIXct("10-03-2011", format = "%d-%m-%Y", tz = "UTC"),
                 as.POSIXct("20-03-2011", format = "%d-%m-%Y", tz = "UTC"), by = 'day'),
             seq(as.POSIXct("10-03-2012", format = "%d-%m-%Y", tz = "UTC"),
                 as.POSIXct("20-03-2012", format = "%d-%m-%Y", tz = "UTC"), by = 'day'),
             seq(as.POSIXct("10-03-2013", format = "%d-%m-%Y", tz = "UTC"),
                 as.POSIXct("20-03-2013", format = "%d-%m-%Y", tz = "UTC"), by = 'day'))
  dim(dates) <- c(ftime = 11, sdate = 3)
  expect_equal(
    SelectPeriodOnDates(dates = lonlat_prec$attrs$Dates, start = list(10, 03), 
                        end = list(20, 03),  time_dim = 'ftime'),
    dates
  )
  dates_lonlat <- lonlat_prec_st$attrs$Dates
  dates_lonlat <- Reorder(dates_lonlat-12*3600, c(2,1))
  expect_equal(
    SelectPeriodOnDates(dates = dates_lonlat, start = list(10, 03), 
                        end = list(20, 03),  time_dim = 'ftime'),
    dates
  )
  # test error
  dates <- CSTools::lonlat_prec_st$attrs$Dates
  out_sdates <- c(as.POSIXct("10-03-2011", format = "%d-%m-%Y", tz = "UTC"),
                  as.POSIXct("10-03-2012", format = "%d-%m-%Y", tz = "UTC"),
                  as.POSIXct("10-03-2013", format = "%d-%m-%Y", tz = "UTC"))
  dim(dates) <- c(a = 3, len = 31)
  expect_error(
    SelectPeriodOnDates(dates = dates, start = list(10, 03), 
                        end = list(20, 03), time_dim = 'ftime'),
    "Could not find dimension 'ftime' in 1th object provided in 'data'."
  )
  dim(dates) <- c(sdate = 3, ftime = 31)
  expect_equal(
    SelectPeriodOnDates(dates = dates-12*3600, start = list(10, 03), 
                        end = list(20, 03), time_dim = 'ftime')[, 1],
    out_sdates
  )
  dates <- Reorder(dates, c(2,1))
  expect_equal(
    SelectPeriodOnDates(dates = dates-12*3600, start = list(10, 03), 
                        end = list(20, 03), time_dim = 'ftime')[1, ],
    out_sdates
  )
})
