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

# Data 1: Terciles
probs_1 <- array(rep(c(0.3, 0.4, 0.3), 4), c(cat = 3, ftime = 4))
probs_1_2 <- array(rep(c(0.3), 4), c(ftime = 4))
probs_1_3 <- array(rep(c(0.3, 0.4), 4), c(cat = 2, ftime = 4))
lims_1 <- array(rep(c(2, 4), 4), c(cat = 2, ftime = 4))
lims_1_2 <- array(rep(c(2), 4), c(ftime = 4))
panel_title_1 = c('march', 'june', 'oct', 'dec')
category_names_1 <- c('bn', 'norn', 'an')

# Data 2: Three panels
probs_2 <- array(rep(c(0.5, 0.4, 0.3), 3), c(cat = 3, ftime = 3))
lims_2 <- array(rep(c(2, 8), 3), c(cat = 2, ftime = 3))
panel_title_2 = c('march', 'june', 'oct')
category_names_2 <- c('bn', 'norn', 'an')

# Data 3: Quintiles
probs_3 <- array(rep(c(0.1, 0.4, 0.3, 0.1, 0.1), 3), c(cat = 5, ftime = 3))
lims_3 <- array(rep(c(2, 4, 8, 10), 3 ), c(cat = 4, ftime = 3))
panel_title_3 = c('march', 'june', 'oct')
category_names_3 = c('exb', 'bn', 'norn', 'an', 'ean')

# Test 4: Add extremes
probs_4 <- array(rep(c(0.5, 0.4, 0.3), 3), c(cat = 3, ftime = 3))
lims_4 <- array(rep(c(2, 8), 3), c(cat = 2, ftime = 3))
panel_title_4 = c('march', 'june', 'oct')
category_names_4 <- c('bn', 'norn', 'an')
extremes <- array(rep(c(0.1, 0.9), 3), c(cat = 2, ftime = 3))


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

test_that("1. Input checks", {
  
  expect_error(
    BarPlotCat(probs = "string", lims = lims_1),
    "Parameter 'probs' and 'lims' must be both vectors or arrays."
  )
  expect_error(
    BarPlotCat(probs = probs_1_2, lims = lims_1),
    "Parameter 'probs' must have 2 dimensions."
  )
  expect_error(
    BarPlotCat(probs = probs_1_3, lims = lims_1),
    "Parameter 'probs' must have one more element than parameter 'lims' in 'cat_dim' dimension."
  )
  expect_error(
    BarPlotCat(probs = probs_1, lims = "string"),
    "Parameter 'lims' must have 2 dimensions maximum."
  ) 
  expect_error(
    BarPlotCat(probs = probs_1, lims = lims_1_2),
    "Expected both 'lims' and 'probs' parameter have same dimensions."
  )
  expect_error(
    BarPlotCat(probs = probs_1_2, lims = lims_1_2),
    "'cat_dim' in parameter lims is needed."
  )
  expect_error(
    BarPlotCat(probs = probs_1, lims = lims_1, extreme_lims = "string"),
    "'probs' dimensions do no match 'extreme_lims'."
  )
  expect_error(
    BarPlotCat(probs = probs_1, lims = lims_1, skill = "string"),
    "Parameter 'skill' must be a numeric vector."
  )
  expect_error(
    BarPlotCat(probs = probs_1, lims = lims_1, toptitle = 1),
    "Parameter 'toptitle' must be a character string."
  )
  expect_error(
    BarPlotCat(probs = probs_1, lims = lims_1, legend_title = 1),
    "Parameter 'legend_title' must be a character string."
  )
  expect_error(
    BarPlotCat(probs = probs_1, lims = lims_1, cat_dim = 1),
    "Parameter 'order' do not match the dimension names of parameter 'data'."
  )
  expect_error(
    BarPlotCat(probs = probs_1, lims = lims_1, panel_dim = 1),
    "Parameter 'order' do not match the dimension names of parameter 'data'."
  )
  expect_error(
    BarPlotCat(probs = probs_1, lims = lims_1, color.set = 1),
    "Parameter 'color.set' should be one of ggplot/s2s4e/hydro"
  )
  expect_error(
    BarPlotCat(probs = probs_1, lims = lims_1, category_names = 1),
    "Parameter 'category_names' must be a character vector."
  )
  expect_error(
    BarPlotCat(probs = probs_1, lims = lims_1, panel_title = c(1, 2, 3, 4, 5)),
    "Parameter 'panel_title' must be a character string or vector of strings."
  )
  expect_error(
    BarPlotCat(probs = probs_1, lims = lims_1, panel_subtitle = c(1, 2, 3, 4, 5)),
    "Parameter 'panel_subtitle' must be a character string or vector of strings."
  )
  expect_error(
    BarPlotCat(probs = probs_1, lims = lims_1, panel_bottom_name = c(1, 2, 3, 4, 5)),
    "Parameter 'panel_bottom_name' must be a character string or vector of strings."
  )
  
})


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

test_that("2. Output checks", {
  
  expect_snapshot_file(
    save_fun(probs_1, lims_1, toptitle = "Subseasonal Forecast",
             cat_dim = 'cat', panel_dim = 'ftime', color.set = 's2s4e',
             category_names = c('bn', 'norn', 'an'),
             legend_width = 3,
             panel_title = '', panel_subtitle = '',
             panel_bottom_name = '', lims_pos = -4,
             toptitle_size = 16, toptitle_pos = "center",
             width = 10, height = 8,
             size_units = NULL),
    name = "BarPlotCat_1.png"
  )
  
  expect_snapshot_file(
    save_fun(probs_2, lims_2, toptitle = "Seasonal Forecast",
             cat_dim = 'cat', panel_dim = 'ftime', color.set = "hydro",
             category_names = c('bn', 'norn', 'an'),
             legend_width = 3, xaxis_title = 'Precipitation (mm)',
             panel_title = panel_title_2, panel_subtitle = panel_title_2,
             panel_bottom_name = '', lims_pos = -3,
             toptitle_size = 16, toptitle_pos = "center",
             width = 10, height = 8,
             size_units = NULL),
    name = "BarPlotCat_2.png"
  )
  
  expect_snapshot_file(
    save_fun(probs_3, lims_3, toptitle = "Seasonal Forecast",
             cat_dim = 'cat', panel_dim = 'ftime', color.set = "hydro",
             category_names = c('exb', 'bn', 'norn', 'an', 'ean'),
             legend_width = 3, xaxis_title = 'Precipitation (mm)',
             panel_title = panel_title_3, panel_subtitle = panel_title_3,
             panel_bottom_name = '', lims_pos = -3,
             toptitle_size = 16, toptitle_pos = "center",
             width = 10, height = 8,
             size_units = NULL),
    name = "BarPlotCat_3.png"
  )

  expect_snapshot_file(
    save_fun(probs_4, lims_4, extreme_probs = extremes, extreme_lims = extremes,
             toptitle = "Seasonal Forecast",
             cat_dim = 'cat', panel_dim = 'ftime', color.set = "hydro",
             category_names = c('bn', 'norn', 'an'),
             legend_width = 3, xaxis_title = 'Precipitation (mm)',
             panel_title = panel_title_4, panel_subtitle = panel_title_4,
             panel_bottom_name = '', lims_pos = -3,
             toptitle_size = 16, toptitle_pos = "center",
             width = 10, height = 8,
             size_units = NULL),
    name = "BarPlotCat_4.png"
  )
  
})
