source("helpers.R")
using("tinysnapshot")

aq = airquality
aq$Month = as.factor(aq$Month)

op = par(no.readonly = TRUE)

# logical legend example

f = function() tinyplot(Temp ~ Day | Month, data = aq, legend = FALSE)
expect_snapshot_plot(f, label = "legend_false")

f = function() tinyplot(Temp ~ Day | Month, data = aq, legend = TRUE)
expect_snapshot_plot(f, label = "legend_true")

# position keyword examples 

f = function() with(aq, tinyplot(x = Day, y = Temp, by = Month, legend = "bottom!"))
expect_snapshot_plot(f, label = "legend_keyword_default")

if ((getRversion() <= "4.3.2")) {
  f = function() with(aq, tinyplot(x = density(Temp), by = Month, legend = "bottom!"))
  expect_snapshot_plot(f, label = "legend_keyword_density")
}

f = function() tinyplot(Temp ~ Day | Month, data = aq, legend = "bottom!")
expect_snapshot_plot(f, label = "legend_keyword_formula")

# test other outer keywords

f = function() tinyplot(Temp ~ Day | Month, data = aq, legend = "left!")
expect_snapshot_plot(f, label = "legend_keyword_outerleft")
f = function() tinyplot(Temp ~ Day | Month, data = aq, legend = "bottom!")
expect_snapshot_plot(f, label = "legend_keyword_outerbottom")
f = function() tinyplot(Temp ~ Day | Month, data = aq, legend = "top!")
expect_snapshot_plot(f, label = "legend_keyword_outertop")
f = function() tinyplot(Temp ~ Day | Month, data = aq, legend = "topright!")
expect_snapshot_plot(f, label = "legend_keyword_outertopright")
f = function() tinyplot(Temp ~ Day | Month, data = aq, legend = "bottomleft!")
expect_snapshot_plot(f, label = "legend_keyword_outerbottomleft")

# Horizontal and/or multicolumn legend spacing

aq$Month2 = factor(month.name[aq$Month], levels = month.name[5:9])
f = function() tinyplot(Temp ~ Day | Month2, data = aq, legend = "bottom!")
expect_snapshot_plot(f, label = "legend_spacing_horiz_label_bottom")

f = function() tinyplot(
  weight ~ Time | Chick,
  data = ChickWeight,
  type = "ribbon", # not necessary for plot but helps to check some internal logic
  legend = list("right!", ncol = 3)
)
expect_snapshot_plot(f, label = "legend_spacing_ncol_right")

f = function() tinyplot(
  weight ~ Time | Chick,
  data = ChickWeight,
  type = "l",
  legend = list("bottom!", ncol = 5)
)
expect_snapshot_plot(f, label = "legend_spacing_ncol_bottom")

# Long legend titles

f = function() tinyplot(
  Temp ~ Day | Month, data = aq, main = "Long title",
  legend = list("right!", title = "What month of the year is it?", bty = "o")
)
expect_snapshot_plot(f, label = "legend_long_right")

f = function() tinyplot(
  Temp ~ Day | Month, data = aq, main = "Multiline title",
  legend = list("left!", title = "What month of the year is it?", bty = "o")
)
expect_snapshot_plot(f, label = "legend_long_left")

# multi-line legend titles

f = function() tinyplot(
  Temp ~ Day | Month, data = aq, main = "Multiline title",
  legend = list("top!", title = "Month\nof\nthe\nyear", bty = "o")
)
expect_snapshot_plot(f, label = "legend_multiline_top")

f = function() tinyplot(
  Temp ~ Day | Month, data = aq, main = "Multiline title",
  legend = list("bottom!", title = "Month\nof\nthe\nyear", bty = "o")
)
expect_snapshot_plot(f, label = "legend_multiline_bottom")

# legend function examples

f = function() with(
  aq,
  tinyplot(
    x = Day, y = Temp, by = Month,
    legend = legend("bottom!", title = "Month of the year", bty = "o")
  )
)
expect_snapshot_plot(f, label = "legend_default_legend")

if ((getRversion() <= "4.3.1")) {
  f = function() with(
    aq,
    tinyplot(
      x = density(Temp), by = Month,
      legend = legend("bottom!", title = "Month of the year", bty = "o")
    )
  )
  expect_snapshot_plot(f, label = "legend_density_legend")
}

f = function() tinyplot(
  Temp ~ Day | Month, data = aq,
  legend = legend("bottom!", title = "Month of the year", bty = "o")
)
expect_snapshot_plot(f, label = "legend_formula_legend")

f = function() tinyplot(
  Temp ~ Day | Month, data = aq,
  legend = legend(title = NULL)
)
expect_snapshot_plot(f, label = "legend_title_null")

# title = FALSE should suppress the title, just like title = NULL (#652)
f = function() tinyplot(
  Temp ~ Day | Month, data = aq,
  legend = list(title = FALSE)
)
expect_snapshot_plot(f, label = "legend_title_null")

f = function() tinyplot(
  Temp ~ Day | Month, data = aq,
  legend = legend(legend = month.abb[5:9])
)
expect_snapshot_plot(f, label = "legend_user_labs")

f = function() tinyplot(
  Temp ~ Day | Month, data = aq,
  legend = legend(legend = month.abb[5:10])
)
expect_warning(expect_snapshot_plot(f, label = "legend_user_labs_override"))


# override default legend margins with tpar("lmar")

f = function() {
  olmar = tpar("lmar")
  tpar(lmar = c(1.5, 0.5))
  tinyplot(Temp ~ Day | Month, data = aq, legend = list("right!", bty = "o"))
  box("figure", lty = 2, col = "blue")
  tpar(lmar = olmar)
}
expect_snapshot_plot(f, label = "legend_lmar_right")
f = function() {
  olmar = tpar("lmar")
  tpar(lmar = c(1.5, 0.5))
  tinyplot(Temp ~ Day | Month, data = aq, legend = list("left!", bty = "o"))
  box("figure", lty = 2, col = "blue")
  tpar(lmar = olmar)
}
expect_snapshot_plot(f, label = "legend_lmar_left")
f = function() {
  olmar = tpar("lmar")
  tpar(lmar = c(1.5, 0.5))
  tinyplot(Temp ~ Day | Month, data = aq, legend = list("bottom!", bty = "o"))
  box("figure", lty = 2, col = "blue")
  tpar(lmar = olmar)
}
expect_snapshot_plot(f, label = "legend_lmar_bottom")
f = function() {
  olmar = tpar("lmar")
  tpar(lmar = c(1.5, 0.5))
  tinyplot(Temp ~ Day | Month, data = aq, legend = list("top!", bty = "o"))
  box("figure", lty = 2, col = "blue")
  tpar(lmar = olmar)
}
expect_snapshot_plot(f, label = "legend_lmar_top")


# ljust parameter

d = iris
d$SpeciesVeryLongName = d$Species

f = function() tinyplot(
  Sepal.Length ~ Petal.Length | SpeciesVeryLongName, data = d,
  legend = list("right!", bty = "o")
)
expect_snapshot_plot(f, label = "legend_ljust_left_long")

f = function() tinyplot(
  Sepal.Length ~ Petal.Length | Species, data = d,
  legend = list("right!", bty = "o")
)
expect_snapshot_plot(f, label = "legend_ljust_left_short")

f = function() tinyplot(
  Sepal.Length ~ Petal.Length | SpeciesVeryLongName, data = d,
  legend = list("right!", bty = "o", ljust = "center")
)
expect_snapshot_plot(f, label = "legend_ljust_center_long")

f = function() tinyplot(
  Sepal.Length ~ Petal.Length | Species, data = d,
  legend = list("right!", bty = "o", ljust = "c")
)
expect_snapshot_plot(f, label = "legend_ljust_center_short")

f = function() {
  tpar(ljust = "center")
  tinyplot(
    Sepal.Length ~ Petal.Length | SpeciesVeryLongName, data = d,
    legend = list("right!", bty = "o")
  )
  tpar(ljust = "left")
}
expect_snapshot_plot(f, label = "legend_ljust_tpar_center")

f = function() tinyplot(
  Sepal.Length ~ Petal.Length | SpeciesVeryLongName, data = d,
  cex = d$Petal.Width, legend = list("right!", bty = "o")
)
expect_snapshot_plot(f, label = "legend_ljust_dual_left")

f = function() tinyplot(
  Sepal.Length ~ Petal.Length | SpeciesVeryLongName, data = d,
  cex = d$Petal.Width, legend = list("right!", bty = "o", ljust = "c")
)
expect_snapshot_plot(f, label = "legend_ljust_dual_center")

rm(d)


# reset par
par(op)


# custom legend for new tinyplot.foo s3 method
f = function() {
  foo = data.frame(x = 1:2, y = 1:2, grp = c("A", "B"))
  class(foo) = c("foo", "data.frame")
  tinyplot.foo = function(x, ...) {
    legend = list(title = 'New Title')
    plt(y ~ x | grp, data = x, legend = legend, ...)
  }
  plt(foo) 
}
expect_snapshot_plot(f, label = "legend_custom_s3")


# top! + dynmar: title above legend, legend above plot

f = function() {
  tinyplot(
    Sepal.Length ~ Petal.Length | Species, data = iris,
    main = "Hello", legend = list("top!", bty = "o"), theme = "dynamic"
  )
  box("outer", lty = 2)
}
expect_snapshot_plot(f, label = "legend_top_dynmar_title")

f = function() {
  tinyplot(
    Sepal.Length ~ Petal.Length | Species, data = iris,
    legend = list("top!", bty = "o"), theme = "dynamic"
  )
  box("outer", lty = 2)
}
expect_snapshot_plot(f, label = "legend_top_dynmar_notitle")

f = function() {
  tinyplot(
    Sepal.Length ~ Petal.Length | Species, data = iris,
    main = "Hello", sub = "Subtitle", legend = list("top!", bty = "o"),
    theme = "dynamic"
  )
  box("outer", lty = 2)
}
expect_snapshot_plot(f, label = "legend_top_dynmar_title_sub")

# top! + dynmar: margin reset between successive plots
f = function() {
  tinyplot(Sepal.Length ~ Petal.Length | Species, data = iris,
           legend = list("right!", bty = "o"), theme = "dynamic")
  tinyplot(Sepal.Length ~ Petal.Length | Species, data = iris,
           main = "Hello", legend = list("top!", bty = "o"), theme = "dynamic")
  box("outer", lty = 2)
}
expect_snapshot_plot(f, label = "legend_top_dynmar_after_right")

f = function() {
  tinyplot(Sepal.Length ~ Petal.Length | Species, data = iris,
           main = "Hello", legend = list("top!", bty = "o"), theme = "dynamic")
  tinyplot(Sepal.Length ~ Petal.Length | Species, data = iris,
           legend = list("right!", bty = "o"), theme = "dynamic")
  box("outer", lty = 2)
}
expect_snapshot_plot(f, label = "legend_right_dynmar_after_top")

# The "continuous legend not supported" warning still fires for a continuous
# `by` on an unsupported type ...
expect_warning(
  plt(mpg ~ wt | cyl, mtcars, type = "l"),
  "Continuous"
)
# ... but not when the legend is suppressed (#656): the internal reversion to
# discrete grouping still happens, so only the (moot) warning is skipped.
expect_silent(
  plt(mpg ~ wt | cyl, mtcars, type = "l", legend = FALSE)
)