\usepackage{ifthen}
\usepackage{tikz}
\usetikzlibrary{math, calc, patterns}
\usepackage{xcolor}

% % %  % % %
%  DESIGN  %
% % %  % % %
\usepackage{fontspec}
\setmainfont[Path=fonts/]{RobotoSlab-Regular.ttf}
\setsansfont[Path=fonts/]{Roboto-Regular.ttf}

\definecolor{themaFarbe}{HTML}{000000}
\usepackage{fancyhdr}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{4pt}
\pagestyle{fancy}
\fancyhead{}
\fancyhead[C]{
	\begin{tikzpicture}
		\fill[fill=themaFarbe] (0pt,0pt) rectangle (\linewidth,8pt);
	\end{tikzpicture}
}
\fancyfoot{}
\fancyfoot[L]{Fachschaft Informatik}
\fancyfoot[C]{}
\fancyfoot[R]{TU Darmstadt}


% % % % % % %
%  BEFEHLE  %
% % % % % % %
\newenvironment{stundenplan}[2]{
	\newcounter{tagcounter}
	\newcounter{slotcounter}
	\newcounter{farbcounter}
	\newcounter{notizcounter}

	\centering \huge \textbf{#1}
	\vspace{0.5cm}
	\newcommand{\scaling}{#2}

	\begin{center}
	\begin{tikzpicture}[draw]
}{
		\drawstupla
	\end{tikzpicture}
	\end{center}
}

\tikzmath{
	function daywidth(\n) {
		return \linewidth / (\n + 1.0);
	};
	function xpos(\n, \t, \o) {
		return (\t + \o) * daywidth(\n);
	};
	function ypos(\s) {
		return -0.1 - (\s - 480) / \scaling * 1.15;
	};
}

\newcommand\farbe[3]{
	\definecolor{#1}{HTML}{#3}

	\stepcounter{farbcounter}
	\expandafter\gdef\csname slotfarbename\thefarbcounter \endcsname{#2}
	\expandafter\gdef\csname slotfarbe\thefarbcounter \endcsname{#1}
}

\newenvironment{tag}[1]{
	\stepcounter{tagcounter}
	\expandafter\gdef\csname tag\thetagcounter\endcsname{#1}
}{
}

\newcommand{\slot}[5]{
	\stepcounter{slotcounter}
	\expandafter\gdef\csname slotstart\theslotcounter \endcsname{\tominutes{#1}}
	\expandafter\gdef\csname slotend\theslotcounter \endcsname{\tominutes{#2}}
	\expandafter\gdef\csname slotname\theslotcounter \endcsname{#3}
	\expandafter\gdef\csname slotort\theslotcounter \endcsname{#4}
	\expandafter\gdef\csname slottyp\theslotcounter \endcsname{#5}
	\newcounter{slottag\theslotcounter}
	\setcounter{slottag\theslotcounter}{\thetagcounter}
}
\newcommand{\zeit}[2]{
	\slot{#1}{#2}{#1-#2}{}{zeit}
}
\definecolor{zeit}{HTML}{ffffff}

\newcommand{\notiz}[1]{
	\stepcounter{notizcounter}
	\expandafter\gdef\csname notiz\thenotizcounter \endcsname{#1}
}


\newcommand{\tominutes}[1]{\splittime#1!}
\def\splittime#1:#2!{\the\numexpr #1 * 60 + #2\relax}

\newcommand{\drawstupla}{

	\xdef\minslotstart{\csname slotstart1\endcsname}
	\xdef\maxslotend{\csname slotstart1\endcsname}
	\foreach \s in {1,...,\theslotcounter}{
		\ifnum \csname slotstart\s\endcsname<\minslotstart\relax
			\xdef\minslotstart{\csname slotstart\s \endcsname}
		\fi
		\ifnum \csname slotend\s\endcsname>\maxslotend\relax
			\xdef\maxslotend{\csname slotend\s \endcsname}
		\fi
	}

	% slots
	\sffamily
	\foreach \s in {1,...,\theslotcounter}{
		\ifnum \csname theslottag\s \endcsname=1 \normalsize \else \footnotesize \fi
		\draw[thin, fill=\csname slottyp\s \endcsname] (
			{xpos(\thetagcounter, \csname theslottag\s \endcsname, \ifnum \csname theslottag\s \endcsname=1 0.4 \else 0.0 \fi)},
			{ypos(\csname slotstart\s \endcsname)}
		) rectangle (
			{xpos(\thetagcounter, \csname theslottag\s \endcsname, 1.0)},
			{ypos(\csname slotend\s \endcsname)}
		) node[pos=0.5,align=center] {
			\noindent \csname slotname\s \endcsname
			\ifthenelse{\equal{\csname slotort\s \endcsname}{\empty}}{}{
				\\\noindent \csname slotort\s \endcsname
			}
		};
	}

	% header
	\draw[very thick,fill=black] (
		{xpos(\thetagcounter, 1, 0.4)},
		1.0
	) rectangle (
		{xpos(\thetagcounter, \thetagcounter, 1.0)},
		1.1
	);
	\draw[very thick,fill=black] (
		{xpos(\thetagcounter, 1, 0.4)},
		-0.1
	) rectangle (
		{xpos(\thetagcounter, \thetagcounter, 1.0)},
		0.0
	);
	\draw[very thick,fill=black] (
		{xpos(\thetagcounter, 1, 0.4)},
		{ypos(\maxslotend)}
	) rectangle (
		{xpos(\thetagcounter, \thetagcounter, 1.0)},
		{ypos(\maxslotend) - 0.1}
	);

	\rmfamily
	\normalsize
	\foreach \t in {1,...,\thetagcounter}{
		\draw[very thick,fill=white] (
			{xpos(\thetagcounter, \t, \ifnum \t=1 0.4 \else 0.0 \fi)},
			0.0
		) rectangle (
			{xpos(\thetagcounter, \t, 1.0)},
			1.0
		) node [align=center,pos=0.5] {
			\textbf{\csname tag\t \endcsname}
		};
	}

	% thicc left line
	\draw[very thick] (
		{xpos(\thetagcounter, 1, 0.4)},
		{ypos(\minslotstart)}
	) -- (
		{xpos(\thetagcounter, 1, 0.4)},
		{ypos(\maxslotend)}
	);
	\foreach \t in {2,...,\the\numexpr \thetagcounter + 1\relax}{
		\draw[very thick] (
			{xpos(\thetagcounter, \t, 0.0)},
			{ypos(\minslotstart)}
		) -- (
			{xpos(\thetagcounter, \t, 0.0)},
			{ypos(\maxslotend)}
		);
	}
	% thicc bottom line
	\draw[very thick] (
		{xpos(\thetagcounter, 1, 0.4)},
		{ypos(\maxslotend)}
	) -- (
		{xpos(\thetagcounter, \the\numexpr\thetagcounter + 1\relax, 0.0)},
		{ypos(\maxslotend)}
	);

	% colors / categories
	\footnotesize
	\foreach \i in {1,...,\thefarbcounter}{
		\draw[thin,fill=\csname slotfarbe\i \endcsname] (
			{1.02 * \linewidth},
			{ypos(\the\numexpr 535 + 55 * \i\relax)}
		) rectangle ++(
			0.5,
			0.5
		) node[align=left,below right, xshift=0.005\linewidth] {
			\csname slotfarbename\i \endcsname
		};
	}

	% notes
	\ifnum \thenotizcounter>1\relax
		\linespread{0.8}\selectfont
		\foreach \i in {1,...,\thenotizcounter}{
			\node[align=left,below right,text width=3.5cm,align=justify] at ({1.02 * \linewidth}, {ypos(\the\numexpr 535 + 60 * (\thefarbcounter + \i)\relax)}) {\i) \csname notiz\i \endcsname};
		}
	\fi

	% "Muster"-Text quer über die Seite, falls man es für Demo-Zwecke mit invaliden Daten nutzen möchte
	% \node[rotate=30,scale=17,text opacity=0.1] at (0.65\textwidth, -0.35\textheight) {Muster};
}
