---
title: |
  \begin{center} 
  \vspace*{-2cm}  % Adjust the negative value to bring the title closer to the top
  \large \textbf{Injury and Illness Surveillance} \\
  \textit{ORGANIZATION NAME}
  \end{center}
  \vspace{-0.5cm}
  

date: "`r format(Sys.Date(), '%B %d, %Y')`"  # Dynamic date with the same format

header-includes:
  - \usepackage{fancyhdr}
  - \usepackage{enumitem}

geometry: 
  - top=0.5in   # Adjust the top margin size as desired
  - margin=0.5in  # Adjust the other margin sizes as desired

output:
  pdf_document:
    latex_engine: pdflatex
    keep_tex: FALSE

mainfont: ArialMT
---

\pagestyle{fancyplain}

\lhead{\raisebox{-1\height}
{\includegraphics[height=3cm]{/filepathToHeaderImage1.png}}}

\rhead{ \raisebox{-1\height}
{\includegraphics[height=3cm]{/filepathToHeaderImage2.png}}}

\renewcommand{\headrulewidth}{0pt}

```{r setup, echo = FALSE, include = FALSE, warning = FALSE, message = FALSE}
#load libraries
knitr::opts_chunk$set(echo = TRUE)
options(dplyr.summarise.inform = FALSE)
library(tidyverse)
library(htmltools)
library(knitr)
library(extrafont)
library(png)
library(cowplot)
library(magick)
library(treemapify)
library(ggalluvial)
library(ggpubr)
library(readxl)
library(scales)
```

```{r colors, echo = FALSE, include = FALSE, warning = FALSE, message = FALSE}
#load colors
color_scheme <- c("red", "blue", "green")
organization_font <- "ArialMT"
```

```{r load, echo = FALSE, include = FALSE, warning = FALSE, message = FALSE}
# Get a list of files in the directory and find the most recent date

#files <- list.files("filepathToFiles", pattern = "InjuryData_cumulative_\\d{8}\\.csv", full.names = TRUE)
#file_dates <- as.Date(sub(".*_(\\d{8})\\.csv", "\\1", files), format = "%Y%m%d")
#latest_date <- max(file_dates)
#latest_injury <- format(latest_date, '%Y%m%d')
#latest_illness <- format(latest_date, '%Y%m%d')

#load data
#df.injuries <- read.csv(paste0("filepath_", latest_injury, ".csv"))
#df.illnesses <- read.csv(paste0("filepath_", latest_illness, ".csv"))

df.injuries <- data.frame()
df.illnesses <- data.frame()
```

```{r clean, echo = FALSE, include = FALSE, warning = FALSE, message = FALSE}
#clean data

```

```{r filterTime1Month, echo = FALSE, include = FALSE, warning = FALSE, message = FALSE}
#filter data on the previous 1 month
# df.injuries.acute <- df.injuries %>%
#  filter(RecordedDate >= as.Date(Sys.Date()) - 28) #Filter for last month

# df.illnesses.acute <- df.illnesses %>%
#  filter(RecordedDate >= as.Date(Sys.Date()) - 28) #Filter for last month
```

```{r filterTime6MonthsExclusive, echo = FALSE, include = FALSE, warning = FALSE, message = FALSE}
#filter data on the previous 168 days
# df.injuries.chronic <- df.injuries %>%
#  filter(RecordedDate >= as.Date(Sys.Date()) - 168) %>% #chronic is last 6 months
#  filter(RecordedDate < as.Date(Sys.Date()) - 28) #exclude the acute timeframe

# df.illnesses.chronic <- df.illnesses %>%
#  filter(RecordedDate >= as.Date(Sys.Date()) - 168) %>% #chronic is last 6 months
#  filter(RecordedDate < as.Date(Sys.Date()) - 28) #exclude the acute timeframe
```

\begin{center}
\LARGE \textbf{Executive Summary}
\end{center}

\begin{itemize}
  \item \textbf{Injury Trends:} EXECUTIVE SUMMARY POINT 1

  \item \textbf{Illness Trends:} EXECUTIVE SUMMARY POINT 2
  
  \item \textbf{Time Loss:} EXECUTIVE SUMMARY POINT 3
\end{itemize}

\rule{\textwidth}{2pt}

\begin{center}
\LARGE \textbf{Injuries}
\end{center}

```{r filterTime6MonthsInclusive, echo = FALSE, include = TRUE, warning = FALSE, message = FALSE, fig.width=12, fig.height=4}
#Display injuries across time with cumulative line

data.frame(Blank_Injuries = "PLOT INJURIES OVER TIME HERE") %>%
   ggplot(aes(x = Blank_Injuries, y = 0, fill = Blank_Injuries, label = Blank_Injuries)) %>% +
    geom_line(stat = "identity", color = "black", position = position_dodge2(width = 0.9, preserve = "single")) +
    geom_text(aes(label = Blank_Injuries), family = organization_font, vjust = -1, color = color_scheme[2], size = 8, position = position_dodge(width = 0.9)) +
    scale_y_continuous(limits = c(0, 1), breaks = seq(0, 1, by = 1)) +
    ylab("Injury Count (#)") +
    labs(fill = "Time Loss?") +
    scale_fill_manual(values = c(color_scheme[1], color_scheme[2])) +
    theme_bw() +
    theme(plot.title = element_text(hjust = 0.5, family = organization_font),
          legend.title = element_text(size = 16),
          legend.text = element_text(size = 16),
          legend.position = "none",
          text = element_text(family = organization_font),
          axis.title.x = element_blank(),
          axis.title.y = element_text(size = 16),
          axis.text.y = element_text(size = 16),
          axis.text.x = element_blank(),
          panel.grid = element_blank())
```

```{r dataVisualizationCountBySport, echo = FALSE, include = TRUE, warning = FALSE, message = FALSE, fig.width=12, fig.height=3}
#Display injuries by sport

data.frame(Blank_Injuries = "PLOT INJURIES BY SPORT HERE") %>%
   ggplot(aes(x = Blank_Injuries, y = 0, fill = Blank_Injuries, label = Blank_Injuries)) %>% +
    geom_line(stat = "identity", color = "black", position = position_dodge2(width = 0.9, preserve = "single")) +
    geom_text(aes(label = Blank_Injuries), family = organization_font, vjust = -1, color = color_scheme[2], size = 8, position = position_dodge(width = 0.9)) +
    scale_y_continuous(limits = c(0, 1), breaks = seq(0, 1, by = 1)) +
    ylab("Injury Count (#)") +
    labs(fill = "Time Loss?") +
    scale_fill_manual(values = c(color_scheme[1], color_scheme[2])) +
    theme_bw() +
    theme(plot.title = element_text(hjust = 0.5, family = organization_font),
          legend.title = element_text(size = 16),
          legend.text = element_text(size = 16),
          legend.position = "none",
          text = element_text(family = organization_font),
          axis.title.x = element_blank(),
          axis.title.y = element_text(size = 16),
          axis.text.y = element_text(size = 16),
          axis.text.x = element_blank(),
          panel.grid = element_blank())
```

```{r dataVisualizationPieCharts, echo = FALSE, include = TRUE, warning = FALSE, message = FALSE, fig.width=12, fig.height=3}
#Plot binary variable injury counts
#Sex pie chart
plot1 <- data.frame(Blank_Injuries = "GAMES COMPARISON\nPIE CHART") %>%
  ggplot(aes(x = Blank_Injuries, y = 100, fill = Blank_Injuries)) +
    geom_bar(stat = "identity", color = "black", linewidth = 1) +
    ggtitle("Olympic vs. Paralympic") +
    geom_text(aes(label = Blank_Injuries), position = position_stack(vjust = 0.5), size = 5, family = organization_font, color = "white") +
    coord_polar(theta = "y") +
    scale_fill_manual(values = color_scheme) +
    theme_void() +
    theme(legend.position = "none",  # Hide the legend
          legend.title = element_blank(),
          text = element_text(family = organization_font),
          legend.text = element_text(family = organization_font, size = 16),
          plot.title = element_text(hjust = 0.5))

#Olympic Paralympic Pie Chart
plot2 <- data.frame(Blank_Injuries = "SEX COMPARISON\nPIE CHART") %>%
  ggplot(aes(x = Blank_Injuries, y = 100, fill = Blank_Injuries)) +
    geom_bar(stat = "identity", color = "black", linewidth = 1) +
    ggtitle("Male vs. Female") +
    geom_text(aes(label = Blank_Injuries), position = position_stack(vjust = 0.5), size = 5, family = organization_font, color = "white") +
    coord_polar(theta = "y") +
    scale_fill_manual(values = color_scheme) +
    theme_void() +
    theme(legend.position = "none",  # Hide the legend
          legend.title = element_blank(),
          text = element_text(family = organization_font),
          legend.text = element_text(family = organization_font, size = 16),
          plot.title = element_text(hjust = 0.5))

#Time loss pie chart
plot3 <- data.frame(Blank_Injuries = "TIME LOSS\nCOMPARISON\nPIE CHART") %>%
  ggplot(aes(x = Blank_Injuries, y = 100, fill = Blank_Injuries)) +
    geom_bar(stat = "identity", color = "black", linewidth = 1) +
    ggtitle("Times Loss vs. Non-Time Loss") +
    geom_text(aes(label = Blank_Injuries), position = position_stack(vjust = 0.5), size = 5, family = organization_font, color = "white") +
    coord_polar(theta = "y") +
    scale_fill_manual(values = color_scheme) +
    theme_void() +
    theme(legend.position = "none",  # Hide the legend
          legend.title = element_blank(),
          text = element_text(family = organization_font),
          legend.text = element_text(family = organization_font, size = 16),
          plot.title = element_text(hjust = 0.5))

#plot on same row
plotlist <- list(plot1, plot2, plot3)
ggarrange(plotlist = plotlist, nrow = 1)
```