############################
# Cid Edson Mendonça Póvoas#
############################

#library(devtools)
#devtools::install_github("laresbernardo/lares")
library(lares)
library(ggplot2)
library(extrafont)
## Registering fonts with R
library(showtext)
## Loading required package: sysfonts
## Loading required package: showtextdb
## 
## Attaching package: 'showtextdb'
## The following object is masked from 'package:extrafont':
## 
##     font_install
#font_import(pattern = "Arial", prompt = F)
#windowsFonts(Arial = windowsFont("TT Arial"))
#windowsFonts(sans="Arial Narrow")
#loadfonts(device="win")
font_add_google("Open Sans", "open")
showtext_auto()


plot_timeline2 <-
  function(event,
           start,
           end = start + 1,
           label = NA,
           group = NA,
           title = "Curriculum Vitae Timeline",
           subtitle = "",
           size = 7,
           colour = "orange",
           save = FALSE,
           subdir = NA,
           ...) {
    df <- data.frame(
      Role = as.character(event),
      Place = as.character(label),
      Start = lubridate::date(start),
      End = lubridate::date(end),
      Type = group
    )
    cvlong <- data.frame(
      pos = rep(as.numeric(rownames(df)),
                2),
      name = rep(as.character(df$Role), 2),
      type = rep(factor(df$Type,
                        ordered = TRUE), 2),
      where = rep(as.character(df$Place),
                  2),
      value = c(df$Start, df$End),
      label_pos = rep(df$Start +
                        floor((df$End - df$Start) / 2), 2)
    )
    maxdate <- max(df$End)
    p <- ggplot(cvlong, aes(
      x = value,
      y = reorder(name, -pos),
      label = where,
      group = pos
    )) + geom_vline(xintercept = maxdate,
                    alpha = 0.8,
                    linetype = "dotted") + labs(
                      title = title,
                      subtitle = subtitle,
                      x = NULL,
                      y = NULL,
                      colour = NULL
                    ) +
      lares::theme_lares() + theme(
        panel.background = element_rect(fill = "white",
                                        colour = NA),
        axis.ticks = element_blank(),
        panel.grid.major.x = element_line(size = 0.25,
                                          colour = "grey80")
      )
    if (!is.na(cvlong$type)[1] | length(unique(cvlong$type)) >
        1) {
      p <- p + geom_line(aes(color = type), size = size) +
        facet_grid(type ~ ., scales = "free", space = "free") +
        guides(colour = "none") +
        scale_colour_hue()
    }
    else {
      p <- p + geom_line(size = size, colour = colour)
    }
    p <- p + geom_label(
      aes(x = label_pos),
      colour = "black",
      size = 2.5,
      alpha = 0.3
    )
    if (save) {
      file_name <- "cv_timeline.png"
      if (!is.na(subdir)) {
        dir.create(file.path(getwd(), subdir), recursive = T)
        file_name <- paste(subdir, file_name, sep = "/")
      }
      p <- p + ggsave(file_name, width = 8, height = 6)
      message(paste("Saved plot as", file_name))
    }
    return(p)
    }

order <- c("Role", "Place", "Type", "Start", "End")
today <- as.character(Sys.Date())

###

# cv <- data.frame(rbind(
#   
#   c("BSc in Agronomy", "UESC", "Academic", "2015-07-19", "2020-01-27"),
#   c("Tech in OHS", "IFBA", "Academic", "2012-01-01", "2014-03-01"),
#   c("Agronomist Intern", "MARS", "Work Experience", "2019-09-23", "2019-12-20"),
#   c("Agronomist Intern", "CEPLAC", "Work Experience", "2017-06-01", "2018-07-01"),
#   c("Telecom Technical", "CSM Telecom", "Work Experience", "2005-04-01", "2010-07-01"),
#   c("Technical Manager", "CSM Telecom", "Work Experience", "2010-07-01", "2015-07-01"),
#   c("Telecom Technical", "TeleData", "Work Experience", "2002-06-01", "2004-02-01"),
#   c("Tech in OHS Intern", "SETMA", "Work Experience", "2013-12-01", "2014-02-01"),
#   c("Intern", "BitWay", "Work Experience", "2001-03-01", "2001-09-01"),
#   c("R Language", "Advanced", "Skills", "2016-01-01", today),
#   c("ArcGIS", "Intermediate", "Skills", "2016-01-01", today),
#   c("Excel", "Advanced", "Skills", "2000-01-01", today),
#   c("Power BI", "Intermediate", "Skills", "2018-01-01", today),
#   c("Web Developer", "Ação Ilhéus", "Voluntary", "2008-01-01", "2012-01-01"),
#   c("Intern", "NUPER", "Voluntary", "2017-05-01", "2018-04-01")))
# 
# ###
# 
# colnames(cv) <- order
# colour <- c("red", "blue", "green")
# 
# plot_timeline2(
#   event = cv$Role,
#   start = cv$Start,
#   end = cv$End,
#   label = cv$Place,
#   group = cv$Type,
#   save = FALSE,
#   subtitle = "Cid Edson Póvoas"
# )
# 


cv <- data.frame(rbind(
  
  c("BSc in Agronomy", "UESC", "Academic", "2015-07-19", "2020-01-27"),
  c("Tech in OHS", "IFBA", "Academic", "2012-01-01", "2014-03-01"),
  c("Agronomist - Data Scientist", "ANOVAGRO", "Work Experience", "2021-09-29", today),
  c("Data Analyst", "Consult & Agro", "Work Experience", "2020-02-17", "2021-09-28"),
  c("Agronomist Intern", "MARS", "Work Experience", "2019-09-23", "2019-12-20"),
  c("Agronomist Intern", "CEPLAC", "Work Experience", "2017-06-01", "2018-07-01"),
  c("Telecom Technical", "CSM Telecom", "Work Experience", "2005-04-01", "2010-07-01"),
  c("Tech in OHS Intern", "SETMA", "Work Experience", "2013-12-01", "2014-02-01"),
  c("Technical Manager", "CSM Telecom", "Work Experience", "2010-07-01", "2015-07-01"),
  c("Telecom Technical", "TeleData", "Work Experience", "2002-06-01", "2004-02-01"),
  c("Intern", "BitWay", "Work Experience", "2001-03-01", "2001-09-01"),
  c("R Language", "Advanced", "Skills", "2016-01-01", today),
  c("ArcGIS", "Intermediate", "Skills", "2016-01-01", today),
  c("Excel", "Advanced", "Skills", "2000-01-01", today),
  c("Power BI", "Intermediate", "Skills", "2018-01-01", today),
  c("Web Developer", "Ação Ilhéus", "Voluntary", "2008-01-01", "2012-01-01"),
  c("Intern", "NUPER", "Voluntary", "2017-05-01", "2018-04-01")))




###

colnames(cv) <- order
colour <- c("red", "blue", "green")

a<- plot_timeline2(
  event = cv$Role,
  start = cv$Start,
  end = cv$End,
  label = cv$Place,
  group = cv$Type,
  save = FALSE,
  subtitle = "Cid Edson Póvoas"
)
## Warning in .font_global(font, quiet = FALSE): Font 'Arial Narrow' is not
## installed, has other name, or can't be found