\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{thesis}[2021/10/28 Master Thesis]

\ProcessOptions\relax
\LoadClass[a4paper,12pt,twoside,openright]{memoir}

% Colors
\RequirePackage{xcolor}
\include{rwth-colors.tex}

% Calc required for chapter header calculations
\RequirePackage{calc}

% Pagesize
% Memoir documentation: https://ctan.org/pkg/memoir?lang=de
\setbinding{5mm} 
\settypeblocksize{650pt}{1.2\lxvchars}{*}
\setlrmargins{*}{*}{1.5}
\setulmargins{3cm}{*}{*}
\setheadfoot{2\onelineskip}{2\onelineskip}
\setmarginnotes{17pt}{3.5cm}{\onelineskip}
\checkandfixthelayout

% Headings and Page Style
\pagestyle{ruled}
\copypagestyle{chapter}{plain}
\makeevenfoot{chapter}{\thepage}{}{}
\makeoddfoot{chapter}{}{}{\thepage}

% Fonts
\RequirePackage{amsfonts}
\RequirePackage{mathspec}
% undo the wrong changes made by mathspec
\let\RequirePackage\original@RequirePackage
\let\usepackage\RequirePackage
\RequirePackage{fontspec}
\RequirePackage{stmaryrd}

\setprimaryfont[
UprightFont={*},
ItalicFont={* Italic},
BoldFont={* Semibold},
BoldItalicFont={* Semibold Italic},
Scale=MatchLowercase]{Source Serif Pro}
%
% Sans Font: Source Sans Pro Light/Semibold
\setallsansfonts[
UprightFont={*},
ItalicFont={* Italic},
BoldFont={* Semibold},
BoldItalicFont={* Semibold Italic},
Scale=MatchLowercase]{Source Sans Pro}
%
% Mono Font: Source Code Pro Light/Semibold (No Italics available)
\setallmonofonts[
UprightFont={*},
BoldFont={* Semibold},
Scale=MatchLowercase]{Source Code Pro}

\setmathfont(Digits,Latin)[
UprightFont={*},
BoldFont={* Semibold},
ItalicFont={* Italic},
BoldItalicFont={* Semibold Italic} 
]{Source Serif Pro}

%
% Replace math greek fonts by Source Serif Pro (No Italics available -> Use fake italics)
\setmathfont(Greek)[
  UprightFont={*},
  ItalicFont={* Italic},
  BoldFont={* Semibold},
  BoldItalicFont={* Semibold Italic},
  Scale=MatchLowercase,
  % AutoFakeSlant=0.15
]{Source Serif Pro}

%%%
% Division Styles
\colorlet{chaptercolor}{rwth-50}
% helper ms
\newcommand\numlifter[1]{\raisebox{-2cm}[0pt][0pt]{\smash{#1}}}
\newcommand\numindent{\kern37pt}
\newlength\chaptertitleboxheight

\makechapterstyle{hansen}{
	\renewcommand\printchaptername{\raggedleft}
	\renewcommand\printchapternum{%
		\begingroup%
		\leavevmode%
		\chapnumfont%
		\strut%
		\numlifter{\thechapter}%
		\numindent%
		\endgroup%
	}
	\renewcommand*{\printchapternonum}{%
		\vphantom{\begingroup%
			\leavevmode%
			\chapnumfont%
			\numlifter{\vphantom{9}}%
			\numindent%
			\endgroup}
		\afterchapternum}
	\setlength\midchapskip{0pt}
	\setlength\beforechapskip{0.5\baselineskip}
	\setlength{\afterchapskip}{3\baselineskip}
	\renewcommand\chapnumfont{%
		\fontsize{4cm}{0cm}%
		\bfseries%
		\sffamily%
		\color{chaptercolor}%
	}
	\renewcommand\chaptitlefont{%
		\normalfont%
		\huge%
		\bfseries%
		\raggedleft%
	}%
	\settototalheight\chaptertitleboxheight{%
		\parbox{\textwidth}{\chaptitlefont \strut bg\\bg\strut}}
	\renewcommand\printchaptertitle[1]{%
		\parbox[t][\chaptertitleboxheight][t]{\textwidth}{%
			%\microtypesetup{protrusion=false}% add this if you use microtype
			\chaptitlefont\strut ##1\strut}%
	}
}
% Chapter Style
\chapterstyle{hansen}
% Section Style
\setsecheadstyle{\Large\bfseries\color{rwth-75}\raggedright}
%
%%%

% Section reference
\renewcommand*{\sectionrefname}{Section~}

% Ortography
\PassOptionsToPackage{ngerman,english}{babel}
\RequirePackage{babel}
%

\RequirePackage{url}

% Bibliography
\PassOptionsToPackage{backend=biber,style=numeric,citestyle=numeric-comp,sorting=ynt,defernumbers=true,url=false}{biblatex}
\RequirePackage{biblatex}
% \addbibresource{../literatur.bib} % Put this in the document

% Language
\PassOptionsToPackage{english,ngerman}{babel}
\RequirePackage{babel}
\PassOptionsToPackage{german=quotes}{csquotes}
\RequirePackage{csquotes}

% Superscripts for 1st, 2nd, 3rd, 4th
% \nth{3}
\usepackage[super]{nth}

% Figures
\RequirePackage{tikz}
% \newsubfloat{figure}
%

% Links in PDF
\PassOptionsToPackage{pdfversion=2.0,hidelinks}{hyperref}
\RequirePackage{hyperref}
%

% Acronyms
% must be loaded after hyperref
\PassOptionsToPackage{acronym,nomain,automake,nonumberlist,toc}{glossaries}
\RequirePackage{glossaries}
\RequirePackage{glossary-longragged} 

% Captions
\RequirePackage{subcaption}