---
classoption: landscape
sansfont: Calibri Light
author: "Talitha Speranza"
output:
  rmarkdown::pdf_document:
    includes:  
      in_header: fix_figs.tex
params: 
  text: !r NA
  author: !r NA
  email: !r NA
  url: !r NA
  logo: !r NA
  style: !r NA
  charts: !r NA
  charts.opts: !r NA
---

\fontfamily{lmss}\selectfont

```{r echo = FALSE, results = "hide"}
knitr::opts_chunk$set(warning=FALSE, message=FALSE)

text = params$text
author = params$author
email = params$email
logo = params$logo
url = params$url
```

```{r echo = FALSE, results = "hide"}
library(BETS)

list.charts = params$charts
num.charts <- length(list.charts)
charts.opts <- params$charts.opts
style <- params$style

mask = 1:num.charts
has.chart = as.integer(mask == 1:16)

if(!is.na(params$style)){
    
  if(params$style == "plotly"){
    ext <- ".png"
  } else {
    ext <- ".pdf"
  }
} else {
    ext = ""
}

dir.create("graphs")

chart.FUN <- function(num){
    if(has.chart[num] == 1){
        chart(ts = list.charts[[num]], 
                   file = paste0("chart", num, ext), params = charts.opts[[num]], 
                   style = style, open = FALSE)
    }
}

for(i in 1:16){
    chart.FUN(i)
}

has.text = !is.na(text)
```

\def\haslogo{`r as.integer(!is.na(logo))`}
\def\haswebsite{`r as.integer(!is.na(url))`} 
\def\hastext{`r as.integer(has.text)`}

\def\first{`r has.chart[1]`}
\def\second{`r has.chart[2]`}
\def\third{`r has.chart[3]`}
\def\fourth{`r has.chart[4]`}
\def\fifth{`r has.chart[5]`}
\def\sixth{`r has.chart[6]`}
\def\seventh{`r has.chart[7]`}
\def\eighth{`r has.chart[8]`}
\def\ninth{`r has.chart[9]`}
\def\tenth{`r has.chart[10]`}
\def\eleventh{`r has.chart[11]`}
\def\twelfth{`r has.chart[12]`}
\def\thirteenth{`r has.chart[13]`}
\def\fourteenth{`r has.chart[14]`}
\def\fifteenth{`r has.chart[15]`}
\def\sixteenth{`r has.chart[16]`}

\fancypagestyle{plain}{
  \fancyhf{}%
  \setlength{\headheight}{45pt} 
  \fancyhead[L]{
    \includegraphics[width=\linewidth,height=40pt]{logo_custom} }
  \ifnum\hastext = 1
  \fancyhead[R]{
    {\fontfamily{lmss}\selectfont
    {\footnotesize Analysis by} \\
    \ifnum\haslogo = 1
      \includegraphics[width=\linewidth,height=35pt]{`r logo`} \\
    \else
      `r author` \\
    \fi
    \ifnum\haswebsite = 1
      {\small \url{`r url`}}
    \fi
    }
  }
  \fi
  \fancyfoot[L]{
     {\fontfamily{lmss}\selectfont
     {\small Charts generated by the \textbf{BETS R Package} on `r Sys.Date()` $\diamond$ \textbf{\url{https://github.com/pedrocostaferreira/BETS}}}}
  }
  \fancyfoot[R]{
    \thepage\  / \pageref{LastPage} }
}
\pagestyle{plain}


\begin{figure}[h]
\ifnum\first = 1
    \begin{subfigure}{.49\textwidth}
      \centering
      \includegraphics[width=11.0cm,keepaspectratio]{graphs/`r paste0("chart1",ext)`}
      \label{chart1}
    \end{subfigure}
\fi
~
\ifnum\second = 1
    \begin{subfigure}{.49\textwidth}
      \centering
      \includegraphics[width=11.0cm,keepaspectratio]{graphs/`r paste0("chart2",ext)`}
      \label{chart2}
    \end{subfigure}
\fi
\end{figure}


```{r, echo = FALSE, results='asis'}
text.len = 0

if(!is.na(text)){
  text <- paste(readLines(text), collapse=" ")
  text = gsub(pattern = "\\\\n", replacement = "  \n", x = text)
  text <- strsplit(text, "##")[[1]]
  cat(text[1])
  
  text.len = length(text) - 1
}
```


\ifnum\hastext = 1
  \newpage
  \pagestyle{plain}
\fi


\begin{figure}[h]
\ifnum\third = 1
    \begin{subfigure}{.49\textwidth}
      \centering
      \includegraphics[width=11.0cm,keepaspectratio]{graphs/`r paste0("chart3",ext)`}
      \label{chart3}
    \end{subfigure}
\fi
~
\ifnum\fourth = 1
    \begin{subfigure}{.49\textwidth}
      \centering
      \includegraphics[width=11.0cm,keepaspectratio]{graphs/`r paste0("chart4",ext)`}
      \label{chart4}
    \end{subfigure}
\fi
\end{figure}


```{r, echo = FALSE, results='asis'}
if(!is.na(text) && text.len != 0){
  cat(text[2])
  text.len <- text.len - 1
} 
```

\def\hastext{`r as.integer(text.len != 0)`}

\ifnum\fifth = 1
  \newpage
  \pagestyle{plain}


\begin{figure}[h]
\ifnum\fifth = 1
    \begin{subfigure}{.49\textwidth}
      \centering
      \includegraphics[width=11.0cm,keepaspectratio]{graphs/`r paste0("chart5",ext)`}
      \label{chart5}
    \end{subfigure}
\fi
~
\ifnum\sixth = 1
    \begin{subfigure}{.49\textwidth}
      \centering
      \includegraphics[width=11.0cm,keepaspectratio]{graphs/`r paste0("chart6",ext)`}
      \label{chart6}
    \end{subfigure}
\fi
\end{figure}

```{r, echo = FALSE, results='asis'}
if(!is.na(text) && text.len != 0){
  cat(text[3])
  text.len <- text.len - 1
} 
```

\def\hastext{`r as.integer(text.len != 0)`}

\ifnum\hastext = 1
  \newpage
  \pagestyle{plain}
\fi

\begin{figure}[h]
\ifnum\seventh = 1
    \begin{subfigure}{.49\textwidth}
      \centering
      \includegraphics[width=11.0cm,keepaspectratio]{graphs/`r paste0("chart7",ext)`}
      \label{chart7}
    \end{subfigure}
\fi
~
\ifnum\eighth = 1
    \begin{subfigure}{.49\textwidth}
      \centering
      \includegraphics[width=11.0cm,keepaspectratio]{graphs/`r paste0("chart8",ext)`}
      \label{chart8}
    \end{subfigure}
\fi
\end{figure}

```{r, echo = FALSE, results='asis'}
if(!is.na(text) && text.len != 0){
  cat(text[4])
  text.len <- text.len - 1
} 
```

\def\hastext{`r as.integer(text.len != 0)`}

\ifnum\ninth = 1
    \newpage
    \pagestyle{plain}


\begin{figure}[h]
\ifnum\ninth = 1
    \begin{subfigure}{.49\textwidth}
      \centering
      \includegraphics[width=11.0cm,keepaspectratio]{graphs/`r paste0("chart9",ext)`}
      \label{chart9}
    \end{subfigure}
\fi
~
\ifnum\tenth = 1
    \begin{subfigure}{.49\textwidth}
      \centering
      \includegraphics[width=11.0cm,keepaspectratio]{graphs/`r paste0("chart10",ext)`}
      \label{chart10}
    \end{subfigure}
\fi
\end{figure}

```{r, echo = FALSE, results='asis'}
if(!is.na(text) && text.len != 0){
  cat(text[5])
  text.len <- text.len - 1
} 
```

\def\hastext{`r as.integer(text.len != 0)`}

\ifnum\hastext = 1
  \newpage
  \pagestyle{plain}
\fi

\begin{figure}[h]
\ifnum\eleventh = 1
    \begin{subfigure}{.49\textwidth}
      \centering
      \includegraphics[width=11.0cm,keepaspectratio]{graphs/`r paste0("chart11",ext)`}
      \label{chart11}
    \end{subfigure}
\fi
~
\ifnum\twelfth = 1
    \begin{subfigure}{.49\textwidth}
      \centering
      \includegraphics[width=11.0cm,keepaspectratio]{graphs/`r paste0("chart12",ext)`}
      \label{chart12}
    \end{subfigure}
\fi
\end{figure}


```{r, echo = FALSE, results='asis'}
if(!is.na(text) && text.len != 0){
  cat(text[6])
  text.len <- text.len - 1
} 
```

\def\hastext{`r as.integer(text.len != 0)`}

\ifnum\thirteenth = 1
    \newpage
    \pagestyle{plain}


\begin{figure}[h]
\ifnum\thirteenth = 1
    \begin{subfigure}{.49\textwidth}
      \centering
      \includegraphics[width=11.0cm,keepaspectratio]{graphs/`r paste0("chart13",ext)`}
      \label{chart13}
    \end{subfigure}
\fi
~
\ifnum\fourteenth = 1
    \begin{subfigure}{.49\textwidth}
      \centering
      \includegraphics[width=11.0cm,keepaspectratio]{graphs/`r paste0("chart14",ext)`}
      \label{chart14}
    \end{subfigure}
\fi
\end{figure}

```{r, echo = FALSE, results='asis'}
if(!is.na(text) && text.len != 0){
  cat(text[7])
  text.len <- text.len - 1
} 
```

\def\hastext{`r as.integer(text.len != 0)`}

\ifnum\hastext = 1
  \newpage
  \pagestyle{plain}
\fi


\begin{figure}[h]
\ifnum\fifteenth = 1
    \begin{subfigure}{.49\textwidth}
      \centering
      \includegraphics[width=11.0cm,keepaspectratio]{graphs/`r paste0("chart15",ext)`}
      \label{chart15}
    \end{subfigure}
\fi
~
\ifnum\sixteenth = 1
    \begin{subfigure}{.49\textwidth}
      \centering
      \includegraphics[width=11.0cm,keepaspectratio]{graphs/`r paste0("chart16",ext)`}
      \label{chart16}
    \end{subfigure}
\fi
\end{figure}

```{r, echo = FALSE, results='asis'}
if(!is.na(text) && text.len != 0){
  cat(text[8])
} 
```

\fi
\fi
\fi


