% Copyright 2013-2024 Stefan Widgren and Maria Noremark,
% National Veterinary Institute, Sweden
%
% Licensed under the EUPL, Version 1.1 or - as soon they
% will be approved by the European Commission - subsequent
% versions of the EUPL (the "Licence");
% You may not use this work except in compliance with the
% Licence.
% You may obtain a copy of the Licence at:
%
% http://ec.europa.eu/idabc/eupl
%
% Unless required by applicable law or agreed to in
% writing, software distributed under the Licence is
% distributed on an "AS IS" basis,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
% express or implied.
% See the Licence for the specific language governing
% permissions and limitations under the Licence.

<<echo=false, results=tex>>=
    SummaryTable <- function(contacts, direction) {
        if (identical(direction, "in")) {
            arrow <- "\\leftarrow"
        } else {
            arrow <- "\\rightarrow"
        }

        cat(sprintf("\\begin{longtable}[l]{r %s}\n", paste(rep("c r", max(contacts$distance-1L) + 1), collapse=" ")))
        cat("\\hline\n")

        for (i in seq_len(nrow(contacts))) {
            # Pad with empty cells to the left
            cat(paste(rep("&", 2L*(contacts$distance[i]-1L)), collapse=""))

            cat(sprintf("%s & $%s$ & %s", contacts$lhs[i], arrow, contacts$rhs[i]))

            # Pad with empty cells to the right
            cat(paste(rep("&", 2L*(max(contacts$distance-1L) - (contacts$distance[i]-1L))), collapse=""))
            cat("\\\\\n")
        }

        cat("\\hline\n")
        cat("\\end{longtable}\n")
    }
@

<<echo=false, results=tex>>=
    DetailedTable <- function(contacts, direction) {
        if (identical(direction, "in")) {
            arrow <- "\\leftarrow"
        } else {
            arrow <- "\\rightarrow"
        }

        contacts <- contacts[order(contacts$t, contacts$id, decreasing=FALSE), ]

        contacts$id <- as.character(contacts$id)
        contacts$id[is.na(contacts$id)] <- ""

        contacts$n <- as.character(contacts$n)
        contacts$n[is.na(contacts$n)] <- ""

        contacts$category <- as.character(contacts$category)
        contacts$category[is.na(contacts$category)] <- ""

        by(contacts, sprintf("%s - %s", contacts$lhs, contacts$rhs), function(x) {
            cat(sprintf("\\subsection{%s $%s$ %s}\n", x$lhs[1], arrow, x$rhs[1]))

            cat("\\begin{longtable}[l]{l r r r r r}\n")

            # Header
            cat("\\hline\n")
            cat("Date & Id & N & Category & Source & Destination\\\\\n")
            cat("\\hline\n")

            for (i in seq_len(nrow(x))) {
                cat(sprintf("%s & %s & %s & %s & %s & %s\\\\\n", x$t[i],
                                                                 x$id[i],
                                                                 x$n[i],
                                                                 x$category[i],
                                                                 x$source[i],
                                                                 x$destination[i]))
            }

            cat("\\hline\n")
            cat("\\end{longtable}\n")
        })

        invisible(NULL)
    }
@

\documentclass[a4paper]{article}

\title{Contact Tracing\\Root: \Sexpr{ReportObject()@root}}

\author{EpiContactTrace\\Version: \Sexpr{packageVersion("EpiContactTrace")}}

\usepackage[cm]{fullpage}
\usepackage{float}
\usepackage{hyperref}
\usepackage[table]{xcolor}
\usepackage{booktabs}
\usepackage{longtable}

\floatstyle{ruled}
\newfloat{contacts}{thp}{lop}
\floatname{contacts}{Contacts}

\definecolor{ltgray}{gray}{0.80}

\begin{document}

\maketitle

\clearpage

\section{Summary ingoing contacts}

\begin{tabular}{ l  r }
  In begin date: & \Sexpr{ReportObject()@ingoingContacts@tBegin}\\
  In end date: & \Sexpr{ReportObject()@ingoingContacts@tEnd}\\
  In days: & \Sexpr{ReportObject()@ingoingContacts@tEnd - ReportObject()@ingoingContacts@tBegin}\\
  In degree: & \Sexpr{InDegree(ReportObject()@ingoingContacts)}\\
  Ingoing contact chain: & \Sexpr{IngoingContactChain(ReportObject()@ingoingContacts)}\\
\end{tabular}

<<echo=false, results=tex>>=
    if (length(ReportObject()@ingoingContacts@source) > 0L) {
        # Get network structure. The distance is used for indentation.
        df <- NetworkStructure(ReportObject()@ingoingContacts)

        # Add lhs and rhs, with respect to direction
        df$lhs <- df$destination
        df$rhs <- df$source

        SummaryTable(df, "in")
    } else {
        cat("\\noindent\n")
        cat("\\\\\n")
        cat("\\\\\n")
        cat("No ingoing contacts during the search period.\n")
    }
@

\clearpage

\section{Summary outgoing contacts}

\begin{tabular}{ l  r }
  Out begin date: & \Sexpr{ReportObject()@outgoingContacts@tBegin}\\
  Out end date: & \Sexpr{ReportObject()@outgoingContacts@tEnd}\\
  Out days: & \Sexpr{ReportObject()@outgoingContacts@tEnd - ReportObject()@outgoingContacts@tBegin}\\
  Out degree: & \Sexpr{OutDegree(ReportObject()@outgoingContacts)}\\
  Outgoing contact chain: & \Sexpr{OutgoingContactChain(ReportObject()@outgoingContacts)}\\
\end{tabular}

<<echo=false, results=tex>>=
    if (length(ReportObject()@outgoingContacts@source) > 0L) {
        # Get network structure. The distance is used for indentation.
        df <- NetworkStructure(ReportObject()@outgoingContacts)

        # Add lhs and rhs, with respect to direction
        df$lhs <- df$source
        df$rhs <- df$destination

        SummaryTable(df, "out")
    } else {
        cat("\\noindent\n")
        cat("\\\\\n")
        cat("\\\\\n")
        cat("No outgoing contacts during the search period.\n")
    }
@

<<echo=false, results=tex>>=
    if (length(ReportObject()@ingoingContacts@source) > 0L) {
        cat("\\clearpage\n")
        cat("\\section{Direct ingoing contacts}\n")

        df <- as(ReportObject()@ingoingContacts, "data.frame")

        # Add lhs and rhs, with respect to direction
        df$lhs <- df$destination
        df$rhs <- df$source

        DetailedTable(df[df$destination == df$root, ], "in")

        df <- df[df$destination != df$root, ]
        if (nrow(df) > 0) {
            cat("\\clearpage\n")
            cat("\\section{Indirect ingoing contacts}\n")

            DetailedTable(df, "in")
        }
    }
@

<<echo=false, results=tex>>=
    if (length(ReportObject()@outgoingContacts@source) > 0L) {
        cat("\\clearpage\n")
        cat("\\section{Direct outgoing contacts}\n")

        df <- as(ReportObject()@outgoingContacts, "data.frame")

        # Add lhs and rhs, with respect to direction
        df$lhs <- df$source
        df$rhs <- df$destination

        DetailedTable(df[df$source == df$root, ], "out")

        df <- df[df$source != df$root, ]
        if (nrow(df) > 0) {
            cat("\\clearpage\n")
            cat("\\section{Indirect outgoing contacts}\n")

            DetailedTable(df, "out")
        }
    }
@

\end{document}
