\usepackage{expl3}
\usepackage{xparse}
\usepackage{regexpatch}
\usepackage[utf8]{inputenx}
\usepackage[ngerman,english]{babel} % english is the default language
\usepackage[autostyle=true]{csquotes}
\usepackage{import}

% document setup
\usepackage[pass]{geometry}
\usepackage[backend=biber,style=authoryear,bibstyle=apa,%
sorting=nyt,url=false,doi=false,isbn=false,eprint=false]{biblatex}
%TODO imposes performance penalty, enable for final version!
%\usepackage{microtype} % nicer font typesetting
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}

\usepackage{setspace}
\usepackage{subcaption}


\usepackage{graphicx}
\usepackage[svgnames]{xcolor}

\usepackage[binary-units=true]{siunitx}

%debugging and drafting options
\setlength\overfullrule{2mm}
\usepackage{todonotes}
\usepackage[color]{showkeys}
\definecolor{refkey}{rgb}{1,0,0}
\definecolor{labelkey}{rgb}{1,0,0}
\newcommand{\todoref}[1]{\todo[color=LawnGreen]{#1}}
\newcommand{\todocont}[1]{\todo[color=Yellow,inline]{#1}}
\newcommand{\todohint}[1]{\todo[color=Yellow]{#1}}
\newcommand{\todofix}[1]{\todo[color=Coral]{#1}}


\usepackage[hidelinks]{hyperref} % clickable hyperrefs
\usepackage[nameinlink]{cleveref} % nicer reference management


%
% Bibliography resources
%
\addbibresource{bibliography.bib}

%
% header/footer definition
%
\fancypagestyle{plain}{
\fancyhf{}
\fancyfoot[OR,EL]{\thepage}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
}
\pagestyle{plain}


%
% Custom environments and commands
%

\newcommand{\thethesis}{Master's Thesis}


% Abstract environment with optional argument for the headline
% Mainly taken from report.cls but clears page before the abstract starts
\makeatletter
\newenvironment{abstract}[1][Abstract]{%
    \if@openright%
        \cleardoublepage%
    \else%
        \clearpage%
    \fi%
    \small%
    \begin{center}%
      \pdfbookmark{#1}{#1}%
      {\bfseries #1\vspace{-.5em}\vspace{\z@}}%
    \end{center}%
    \quotation
}{
    \endquotation
}
\makeatother

% Make chapter headlines appear in the same style as sections
\makeatletter
\renewcommand{\@makechapterhead}[1]{%
    \vspace*{50 \p@}%
    {\setlength{\parindent}{\z@} \raggedright \normalfont
    \bfseries\Huge
    \ifnum \value{secnumdepth}>1
       \if@mainmatter\thechapter\ \fi%
    \fi
    #1\par\nobreak\vspace{40 \p@}}%
}

\DeclareDocumentCommand \author {m}{
    \gdef\@author{#1}
    \DeclareDocumentCommand{\theauthor}{m}{#1}
    \hypersetup{pdfauthor={#1}}
}

\DeclareDocumentCommand \title {m}{
    \gdef\@title{#1}
    \DeclareDocumentCommand{\thetitle}{m}{#1}
    \hypersetup{pdftitle={#1}}
}

\DeclareDocumentCommand \date {m}{
    \gdef\@date{#1}
    \DeclareDocumentCommand{\thedate}{m}{#1}
}

\DeclareDocumentCommand \university {m}{
    \gdef\@thesis@university{#1}
    \DeclareDocumentCommand{\theuniversity}{m}{#1}
}
\def\@thesis@university{\@latex@warning@no@line{No \noexpand\university given}}

\DeclareDocumentCommand \department {m}{
    \gdef\@thesis@department{#1}
    \DeclareDocumentCommand{\thedepartment}{m}{#1}
}
\def\@thesis@department{\@latex@warning@no@line{No \noexpand\department given}}

\DeclareDocumentCommand \workgroup {m}{
    \gdef\@thesis@workgroup{#1}
    \DeclareDocumentCommand{\theworkgroup}{m}{#1}
}
\def\@thesis@workgroup{\@latex@warning@no@line{No \noexpand\workgroup given}}


\xpretocmd{\tableofcontents}{
    \if@openright\cleardoublepage\else\clearpage\fi
    \pdfbookmark{\contentsname}{toc}
}{}{\ddt}


\xpretocmd{\printbibliography}{
    \if@openright\cleardoublepage\else\clearpage\fi
    \pdfbookmark{\bibname}{bibliography}
}{}{\ddt}

\xpretocmd{\listoffigures}{
    \if@openright\cleardoublepage\else\clearpage\fi
    \phantomsection
    \addcontentsline{toc}{chapter}{\listfigurename}
    \pdfbookmark{\listfigurename}{list_of_figures}
    \pdfbookmark{\listfigurename}{list_of_figures}
    \pdfbookmark{\listfigurename}{list_of_figures}
}{}{\ddt}

\xpretocmd{\listoftables}{
    \if@openright\cleardoublepage\else\clearpage\fi
    \pdfbookmark{\listtablename}{list_of_tables}
}{}{\ddt}

\makeatother


\makeatletter
\DeclareDocumentCommand{\maketitle}{}{

% limit the scope of thesesis@bindingoffset macro
\begingroup
% clear all margins for layouting except for bindingoffset
\edef\@thesis@bindingoffset{\the\Gm@bindingoffset}
\newgeometry{bindingoffset=\@thesis@bindingoffset,margin=0pt}

\setlength{\fboxsep}{0cm}
\begin{titlepage}
\begin{center}

\vspace*{\fill}
\parbox[t]{12cm}{\centering\thethesis\\
\vspace*{\dimexpr\baselineskip/2\relax}
\huge\@title}

\vspace*{\baselineskip}
by \@author

\vspace{1cm}
\@date

\vspace*{\fill}

\parbox[t]{10cm}{\centering
\LARGE\@thesis@university\\
\bigskip
\Large\@thesis@department\\
\smallskip
\large\@thesis@workgroup}

\vspace*{3.5cm}

\end{center}
\end{titlepage}
\restoregeometry
\endgroup
}
\makeatother

