context("get_plot_time_labels.R")

source("tools/libs.R")
source("modules/Visualization/R/get_plot_time_labels.R")
###########################################

test_that("1. Subseasonal, time-aggregated data", {
  horizon <- "subseasonal"
  init_date <- "20231221"
  start_date <- init_date
  i_syear <- init_date

  time_bounds <- list(start = as.POSIXct("2023-12-21 09:00:00 UTC"),
                      end = as.POSIXct("2024-01-11 09:00:00 UTC"))
  attr(time_bounds, "plotting_attr") <- list(ini_ftime = 1,
                                             end_ftime = 4)

  dates <- as.POSIXct("2023-12-21 09:00:00 UTC")
  dim(dates) <- c(sday = 1, sweek = 1, syear = 1, time = 1)
  time_bounds[] <- lapply(time_bounds, 
                          function(x) {
                            dim(x) <- dim(dates)
                            return(x)
                          })

  labels <- .get_plot_time_labels(
    dates = dates,
    horizon = horizon,
    init_date = init_date,
    start_date = start_date,
    i_syear = i_syear,
    time_bounds = time_bounds
  )

  # Results
  expect_equal(
    labels$years,
    NULL
  )
  expect_equal(
    labels$time_labels[1],
    "Valid from 2023-12-25 to 2024-01-21"
  )
})

test_that("2. Subseasonal, week-by-week data", {
  horizon <- "subseasonal"
  init_date <- "20231214"
  start_date <- init_date
  i_syear <- init_date

  time_bounds <- NULL

  dates <- as.POSIXct(c("2023-12-21 09:00:00 UTC", "2023-12-28 09:00:00 UTC",
                        "2024-01-04 09:00:00 UTC", "2024-01-11 09:00:00 UTC"))
  dim(dates) <- c(sday = 1, sweek = 1, syear = 1, time = 4)

  labels <- .get_plot_time_labels(
    dates = dates,
    horizon = horizon,
    init_date = init_date,
    start_date = start_date,
    i_syear = i_syear,
    time_bounds = time_bounds
  )

  # Results
  expect_equal(
    labels$years,
    c(2023, 2023, 2024, 2024)
  )
  expect_equal(
    labels$time_labels[1],
    "Valid from 18-12 to 24-12 of "
  )
  expect_equal(
    labels$time_labels[2],
    "Valid from 25-12 to 31-12 of "
  )
  expect_equal(
    labels$time_labels[3],
    "Valid from 01-01 to 07-01 of "
  )
  expect_equal(
    labels$time_labels[4],
    "Valid from 08-01 to 14-01 of "
  )
})

test_that("3. Seasonal, time-aggregated data", {
  horizon <- "seasonal"
  init_date <- 10
  start_date <- "20231001"
  i_syear <- "20231001"

  time_bounds <- list(start = c(as.POSIXct("2023-10-16 12:00:00 UTC"),
                                as.POSIXct("2023-12-16 09:00:00 UTC")),
                      end = c(as.POSIXct("2023-12-16 09:00:00 UTC"),
                              as.POSIXct("2024-03-16 09:00:00 UTC")))
  attr(time_bounds, "plotting_attr") <- list(ini_ftime = c(1, 3),
                                             end_ftime = c(3, 6)
  )

  dates <- as.POSIXct(c("2023-10-16 12:00:00 UTC", "2023-12-16 09:00:00 UTC"))
  dim(dates) <- c(sday = 1, sweek = 1, syear = 1, time = 2)
  time_bounds[] <- lapply(time_bounds, 
                          function(x) {
                            dim(x) <- dim(dates)
                            return(x)
                          })

  labels <- .get_plot_time_labels(
    dates = dates,
    horizon = horizon,
    init_date = init_date,
    start_date = start_date,
    i_syear = i_syear,
    time_bounds = time_bounds
  )

  # Results
  expect_equal(
    labels$years,
    NULL
  )
  expect_equal(
    labels$time_labels[1],
    "October to December 2023"
  )
  expect_equal(
    labels$time_labels[2],
    "December 2023 to March 2024"
  )
})

test_that("4. Seasonal, month-by-month data", {
  horizon <- "seasonal"
  init_date <- 10
  start_date <- "20231001"
  i_syear <- "20231001"

  time_bounds <- NULL

  dates <- as.POSIXct(c("2023-10-16 12:00:00 UTC", "2023-11-15 12:00:00 UTC",
                        "2023-12-16 09:00:00 UTC", "2024-01-15 12:00:00 UTC"))
  dim(dates) <- c(sday = 1, sweek = 1, syear = 1, time = 4)

  labels <- .get_plot_time_labels(
    dates = dates,
    horizon = horizon,
    init_date = init_date,
    start_date = start_date,
    i_syear = i_syear,
    time_bounds = time_bounds
  )

  # Results
  expect_equal(
    labels$years,
    c(2023, 2023, 2023, 2024)
  )
  expect_equal(
    as.vector(labels$time_labels),
    c("October", "November", "December", "January")
  )
})

test_that("5. Decadal, time-aggregated data", {

  horizon <- "decadal"
  init_date <- 1
  start_date <- c("2020", "2021")

  time_bounds <- list(start = as.POSIXct(c("2020-11-16 UTC", "2021-11-16 UTC",
                                           "2020-11-16 UTC", "2021-11-16 UTC")),
                      end = as.POSIXct(c("2021-02-15 00:00:00 UTC",
                                         "2022-02-15 00:00:00 UTC",
                                         "2020-12-16 12:00:00 UTC",
                                         "2021-12-16 12:00:00 UTC")))
  attr(time_bounds, "plotting_attr") <- list(ini_ftime = c(1, 1),
                                             end_ftime = c(4, 2)
  )

  dates <- as.POSIXct(c("2020-11-16 UTC", "2021-11-16 UTC", "2020-11-16 UTC", "2021-11-16 UTC"))
  dim(dates) <- c(sday = 1, sweek = 1, syear = 2, time = 2)
  time_bounds[] <- lapply(time_bounds, 
                          function(x) {
                            dim(x) <- dim(dates)
                            return(x)
                          })

  labels <- vector(mode = "list", length = 2)
  for (i in 1:length(start_date)) {
    labels[[i]] <- .get_plot_time_labels(
      dates = dates,
      horizon = horizon,
      init_date = init_date,
      start_date = start_date,
      i_syear = start_date[i],
      time_bounds = time_bounds
    )
  }

  # Results
  expect_equal(
    labels[[1]]$years,
    labels[[2]]$years,
    NULL
  )

  expect_equal(
    labels[[1]]$time_labels[1],
    "November 2020 to February 2021"
  )
  expect_equal(
    labels[[1]]$time_labels[2],
    "November to December 2020"
  )
  expect_equal(
    labels[[2]]$time_labels[1],
    "November 2021 to February 2022"
  )
  expect_equal(
    labels[[2]]$time_labels[2],
    "November to December 2021"
  )
})


test_that("6. Decadal, month-by-month data", {

  horizon <- "decadal"
  init_date <- 1
  start_date <- c("2020", "2021")

  dates <- as.POSIXct(c("2020-11-16 00:00:00 UTC", "2021-11-16 00:00:00 UTC",
                        "2020-12-16 12:00:00 UTC", "2021-12-16 12:00:00 UTC",
                        "2021-01-16 12:00:00 UTC", "2022-01-16 12:00:00 UTC",
                        "2021-02-15 00:00:00 UTC", "2022-02-15 00:00:00 UTC"))
  dim(dates) <- c(sday = 1, sweek = 1, syear = 2, time = 4)

  labels <- vector(mode = "list", length = 2)
  for (i in 1:length(start_date)) {
    labels[[i]] <- .get_plot_time_labels(
      dates = dates,
      horizon = horizon,
      init_date = init_date,
      start_date = start_date,
      i_syear = start_date[i],
      time_bounds = NULL
    )
  }

  # Results
  expect_equal(
    labels[[1]]$years,
    c(2020, 2020, 2021, 2021)
  )
  expect_equal(
    labels[[2]]$years,
    c(2021, 2021, 2022, 2022)
  )

  expect_equal(
    as.vector(labels[[1]]$time_labels),
    as.vector(labels[[2]]$time_labels),
    c("November", "December", "January", "February")
  )
})
