\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{chaacthesis}[2017/10/14 chaac]


%% Load base
\LoadClass[a4paper,12pt,numbers=noenddot,twoside,BCOR=12mm,toc=bib,DVI=15,titlepage=firstcover]{scrbook}
\RequirePackage[utf8]{inputenc}
\RequirePackage[T1]{fontenc}
\RequirePackage{microtype}
\RequirePackage{lmodern}
\RequirePackage{url}

\RequirePackage{expl3}

% Fuer Graphiken und Bilder
\RequirePackage{graphicx}
\RequirePackage{xcolor}
\RequirePackage{tikz}
\RequirePackage{pgf}

% Math
\RequirePackage{amsmath, amsfonts, amssymb, amsthm}
\RequirePackage[
	lambda,
	operators,
	advantage,
	sets,
	adversary,
	landau,
	probability,
	notions,
	logic,
	ff,
	mm,
	primitives,
	events,
	complexity,
	asymptotics,
	keys]{cryptocode}


\ExplSyntaxOn

\str_set:Nn \g_language_str {english}
\str_set:Nn \g_english_str {english}
\str_set:Nn \g_german_str {ngerman}

\str_set:Nn \g_kind_str {master}
\str_set:Nn \g_master_str {master}
\str_set:Nn \g_bachelor_str {bachelor}
\str_set:Nn \g_seminar_str {seminar}
\str_set:Nn \g_doctor_str {diss}

\str_set:Nn \g_level_str {chapter}
\str_set:Nn \g_section_str {section}

\bool_set_true:N \g_head_bool


\keys_define:nn {details} {
    logoChair .tl_set:N = \g_logo_chair_tl,
    title .tl_set:N = \g_title_tl,
    author.tl_set:N = \g_author_tl,
		advisor .clist_set:N = \g_advisor_cl,
    date .tl_set:N = \g_date_tl,
    place .tl_set:N = \g_place_tl,
    logoUni .tl_set:N = \g_logo_uni_tl,
    header .bool_set:N = \g_head_bool,
    language .code:n = { \str_set:Nn \g_language_str {#1}},
    kind .code:n = { \str_set:Nn \g_kind_str {#1}},
    level .code:n ={\str_set:Nn \g_level_str {#1}},
		firstAdvisor .clist_set:N = \g_firstAdv_cl,
		secondAdvisor .clist_set:N = \g_secondAdv_cl,
		thirdAdvisor .clist_set:N = \g_thirdAdv_cl,
		fourthAdvisor .clist_set:N = \g_fourthAdv_cl,
		abstractFile .tl_set:N = \g_abstract_tl
}




\RequirePackage{bibgerm}
\RequirePackage[numbers]{natbib}
\bibliographystyle{alphadin}



\NewDocumentCommand\languageSetup{}{
\RequirePackage[\g_language_str]{babel}


 \str_if_eq:NNT \g_language_str \g_german_str
 {
\tl_set:Nn \g_ns_chairname {Angewandte~Kryptographie}
\tl_new:N \g_ns_kind
\tl_set:Nn \g_ns_kind{\g_kind_str}
\str_if_eq:NNT \g_kind_str \g_master_str
{\str_set:Nn \g_ns_kind {Masterarbeit~in~Informatik}}
\str_if_eq:NNT \g_kind_str \g_bachelor_str
{\str_set:Nn \g_ns_kind {Bachelorarbeit~in~Informatik}}
\str_if_eq:NNT \g_kind_str \g_seminar_str
{\str_set:Nn \g_ns_kind {Seminararbeit~in~Informatik}}
\str_if_eq:NNT \g_kind_str \g_doctor_str
{\str_set:Nn \g_ns_kind {Dissertation~in~Informatik}}

\tl_set:Nn \l_female {f}
\tl_set:Nn \l_male {m}

%---- From here it's just to gender in German ----

\clist_reverse:N \g_advisor_cl
\clist_get:NN  \g_advisor_cl \g_gender
\tl_if_eq:NNTF \g_gender \l_male
	{\clist_gpop:NN \g_advisor_cl \g_gender
	\tl_set:Nn \g_ns_advisor {Betreuer:}}
	{\int_compare:nNnTF {\clist_count:N \g_advisor_cl} > {1}
		{\tl_set:Nn \g_ns_advisor {Betreuerinnen:}}
		{\tl_set:Nn \g_ns_advisor {Betreuerin:}}}
\clist_reverse:N \g_advisor_cl

\clist_reverse:N \g_firstAdv_cl
\clist_get:NN \g_firstAdv_cl \g_gender
\tl_if_eq:NNTF \g_gender \l_male
{\str_if_eq:NNTF \g_kind_str \g_doctor_str
	{\tl_set:Nn \g_ns_firstAdvisor {Erstprüfer:}}
	{\tl_set:Nn \g_ns_firstAdvisor {Erster~Betreuer:}}
}
{\str_if_eq:NNTF \g_kind_str \g_doctor_str
	{\tl_set:Nn \g_ns_firstAdvisor {Erstprüferin:}}
	{\tl_set:Nn \g_ns_firstAdvisor {Erste~Betreuerin:}}
}
\clist_reverse:N \g_firstAdv_cl

\clist_reverse:N \g_secondAdv_cl
\clist_get:NN \g_secondAdv_cl \g_gender
\tl_if_eq:NNTF \g_gender \l_male
{\str_if_eq:NNTF \g_kind_str \g_doctor_str
	{\tl_set:Nn \g_ns_secondAdvisor {Zweitprüfer:}}
	{\tl_set:Nn \g_ns_secondAdvisor {Zweiter~Betreuer:}}
}
{\str_if_eq:NNTF \g_kind_str \g_doctor_str
	{\tl_set:Nn \g_ns_secondAdvisor {Zweitprüferin:}}
	{\tl_set:Nn \g_ns_secondAdvisor {Zweite~Betreuerin:}}
}
\clist_reverse:N \g_secondAdv_cl

\clist_reverse:N \g_thirdAdv_cl
\clist_get:NN \g_thirdAdv_cl \g_gender
\tl_if_eq:NNTF \g_gender \l_male
{\str_if_eq:NNTF \g_kind_str \g_doctor_str
	{\tl_set:Nn \g_ns_thirdAdvisor {Drittprüfer:}}
	{\tl_set:Nn \g_ns_thirdAdvisor {Dritter~Betreuer:}}
}
{\str_if_eq:NNTF \g_kind_str \g_doctor_str
	{\tl_set:Nn \g_ns_thirdAdvisor {Drittprüferin:}}
	{\tl_set:Nn \g_ns_thirdAdvisor {Dritte~Betreuerin:}}
}
\clist_reverse:N \g_thirdAdv_cl

\clist_reverse:N \g_fourthAdv_cl
\clist_get:NN \g_fourthAdv_cl \g_gender
\tl_if_eq:NNTF \g_gender \l_male
{\str_if_eq:NNTF \g_kind_str \g_doctor_str
	{\tl_set:Nn \g_ns_fourthAdvisor {Viertprüfer:}}
	{\tl_set:Nn \g_ns_fourthAdvisor {Vierter~Betreuer:}}
}
{\str_if_eq:NNTF \g_kind_str \g_doctor_str
	{\tl_set:Nn \g_ns_fourthAdvisor {Viertprüferin:}}
	{\tl_set:Nn \g_ns_fourthAdvisor {Vierte~Betreuerin:}}
}
\clist_reverse:N \g_fourthAdv_cl

%---- End gendering ----
}

\str_if_eq:NNT \g_language_str \g_english_str
{
\tl_set:Nn \g_ns_chairname {Applied~Cryptography}
\tl_new:N \g_ns_kind
\tl_set:Nn \g_ns_kind{\g_kind_str}
\str_if_eq:NNT \g_kind_str \g_master_str
  {\str_set:Nn  \g_ns_kind {Master~ Thesis~in~Computer~Science}}
\str_if_eq:NNT \g_kind_str \g_bachelor_str
  {\str_set:Nn \g_ns_kind {Bachelor~Thesis~in~Computer~Science}}
\str_if_eq:NNT \g_kind_str \g_seminar_str
{\str_set:Nn \g_ns_kind {Seminar~Thesis~in~Computer~Science}}
\str_if_eq:NNT \g_kind_str \g_doctor_str
{\str_set:Nn \g_ns_kind {Dissertation~in~Computer~Science}}

\int_compare:nNnTF {\clist_count:N \g_advisor_cl} > {1}
	{\tl_set:Nn \g_ns_advisor {Advisors:}}
	{\tl_set:Nn \g_ns_advisor {Advisor:}}

\tl_set:Nn \g_ns_firstAdvisor {First~Advisor:}
\tl_set:Nn \g_ns_secondAdvisor {Second~Advisor:}
\tl_set:Nn \g_ns_thirdAdvisor {Third~Advisor:}
\tl_set:Nn \g_ns_fourthAdvisor {Fourth~Advisor:}
}
}

\NewDocumentCommand\setDetails{m}{
\IfNoValueF {#1}{\keys_set:nn {details}{#1}}
\languageSetup{}


\RedeclareSectionCommand[tocnumwidth=1.3em]{chapter}
\RedeclareSectionCommand[tocnumwidth=2em]{section}
\RedeclareSectionCommand[tocnumwidth=2.8em]{subsection}
\RedeclareSectionCommand[tocnumwidth=3.6em]{subsubsection}


\bool_if:NTF \g_head_bool {\RequirePackage[automark,headsepline]{scrlayer-scrpage}
\automark[section]{chapter}
\cfoot{}
\ofoot{\pagemark}
  \str_if_eq:NNT \g_level_str \g_section_str
  {
  \automark{section}
	\renewcommand\thesection{\arabic{section}}
	\renewcommand\thesubsection{\arabic{subsection}}
  \RedeclareSectionCommand[tocnumwidth=1.3em]{section}
  \RedeclareSectionCommand[tocnumwidth=2em]{subsection}
  \RedeclareSectionCommand[tocnumwidth=2.8em]{subsubsection}
	\DeclareTOCStyleEntry[entryformat=\bfseries]{tocline}{section}
  }
}{\pagestyle{plain}}

\RequirePackage{hyperref}
\hypersetup{
hidelinks=true,
backref=true,
pdfauthor=\g_author_tl,
pdftitle=\g_title_tl,
breaklinks=true
}
\RequirePackage{cleveref}
}

\NewDocumentCommand\frontpart{o}{
\frontmatter
\frontpage


\pagestyle{empty}

\vfill
\erklaerung{\g_place_tl}{\g_date_tl}


\IfFileExists{#1}{\include{#1}}{\include{#1}}

\cleardoubleoddemptypage

\tableofcontents
\mainmatter

\bool_if:NT \g_head_bool
{\pagestyle{headings} \automark[section]{chapter}}

}

\NewDocumentCommand\frontpage{}{
\storeareas\mystoredvalues
\areaset[current]{\textwidth}{\dimexpr \textheight + 1.8cm}
\begin{titlepage}
\vspace*{\fill}
\begin{center}
\LARGE{\textsc{Friedrich-Alexander-Universit\"at}}\\
\LARGE{\textsc{Erlangen-N\"urnberg}}\par \normalsize\vspace{2\baselineskip}

\IfFileExists{\g_logo_chair_tl}{\includegraphics[width=7cm]{\g_logo_chair_tl}}{}\par \vspace{\baselineskip}
\large{\textbf{\g_ns_kind}}
\par\vspace{1.2\baselineskip}
\rule{\textwidth}{1pt}\par
\normalsize
\vspace{1.2\baselineskip}
\Huge{\textbf{\g_title_tl}}\par
\normalsize
\vspace{1.2\baselineskip}
\rule{\textwidth}{1pt}\par
\vspace{1.2\baselineskip}
\Large{\textbf{\g_author_tl}}\par \vspace{\baselineskip}

\IfFileExists{\g_logo_uni_tl}{\includegraphics[width=4cm]{\g_logo_uni_tl}}{}\par \normalsize\vspace{1.5\baselineskip}
\clist_if_empty:NTF \g_advisor_cl
{\begin{tabular}{rl}
%\tl_if_eq:NNF \g_firstAdv_tl \g_advisorDiss_tl
\clist_if_empty:NF \g_firstAdv_cl
{\large{\g_ns_firstAdvisor} & \large{\clist_item:Nn \g_firstAdv_cl {1}}\\[0.25\baselineskip] }

%\tl_if_eq:NNF \g_secondAdv_tl \g_advisorDiss_tl
\clist_if_empty:NF \g_secondAdv_cl
{\large{\g_ns_secondAdvisor} & \large{\clist_item:Nn \g_secondAdv_cl {1}}
\\[0.25\baselineskip]}

%\tl_if_eq:NNF \g_thirdAdv_tl \g_advisorDiss_tl
\clist_if_empty:NF \g_thirdAdv_cl
{\large{\g_ns_thirdAdvisor}&\large{\clist_item:Nn \g_thirdAdv_cl {1}}
\\[0.25\baselineskip]}

 %\tl_if_eq:NNF \g_fourthAdv_tl \g_advisorDiss_tl
 \clist_if_empty:NF \g_fourthAdv_cl
 {\large{\g_ns_fourthAdvisor} & \large{\clist_item:Nn \g_fourthAdv_cl {1}}}

\end{tabular}}
{\large{\g_ns_advisor}~ \large{\clist_use:Nnnn \g_advisor_cl {,~} {,~}{,~} }}

\par \normalsize\vspace{1.5\baselineskip}
\large{ \g_place_tl, ~\g_date_tl}
\end{center}
\vspace*{\fill}
\end{titlepage}
\cleardoublepage
\mystoredvalues
}



\ExplSyntaxOff

\newcommand{\erklaerung}[2]{
 ~
 \vfill
 Hiermit versichere ich, dass ich die Arbeit selbständig und ohne Benutzung anderer als der angegebenen Quellen angefertigt habe und dass die Arbeit in gleicher oder ähnlicher Form noch keiner anderen Prüfungsbehöre vorgelegen hat und dieser als Teil einer Prüfungsleistung angenommen wurde.
 Alle Stellen, die dem Wortlaut oder dem Sinn nach anderen Werken entnommen sind, habe ich durch Angabe der Quelle als Entlehnung kenntlich gemacht. \vspace{1.7\baselineskip}

\noindent #1, #2 \hspace{1cm} \underline{\hspace{7cm}}
}

\newcommand{\abstract}[1]{
\begingroup
\let\clearpage\relax
\chapter*{#1}
\endgroup
\pagestyle{plain}
}
