% !TEX root = ../../../Main.tex

% Suppress all warnings; Only show errors
\usepackage{silence}
\WarningsOff*

% If not using XeTeX, print an error message and stop compiling
\ifx\XeTeXversion\undefined
    \PackageError{HSRTReport}{This class can only be used with XeLaTeX.}{}
    \stop
\fi

%----------------------------------------
%   PATHS
%----------------------------------------
\def\fontsPath{TeX/Assets/Fonts}
\def\imagesPath{TeX/Assets/Images}
\def\classPath{TeX/Classes/HSRTReport}

%----------------------------------------
%   CLASS DEFINITION AND PARAMETERS
%----------------------------------------
% Original: ZHAWReport.cls by Martin Oswald
\NeedsTeXFormat{LaTeX2e}
\newcommand{\classname}{TeX/Classes/HSRTReport/HSRTReport}
\ProvidesClass{\classname}[2024/07/08 HSRT Report Class]
\providecommand{\baseclass}{scrreprt}

% Default options
\PassOptionsToClass{pointlessnumbers}{\baseclass}

% Forward options to the base class
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{\baseclass}}
\ProcessOptions\relax

% Load the base class with options
\LoadClass{\baseclass}

%----------------------------------------
%   REQUIRED PACKAGES
%----------------------------------------
\RequirePackage[ngerman]{babel}
\RequirePackage{lmodern}
\RequirePackage{graphicx}
\RequirePackage{svg}
\RequirePackage[T1]{fontenc}
\RequirePackage{fontspec}
\RequirePackage{tikz}
\RequirePackage{fancyhdr}
\RequirePackage{bophook}
\RequirePackage{ifthen}
\RequirePackage{arrayjobx}
\RequirePackage{etoolbox}
\RequirePackage{transparent}

%----------------------------------------
%   REFS
%----------------------------------------
\RequirePackage{hyperref}
\hypersetup{
    pdfpagemode={UseOutlines},
    bookmarksopen=true,
    bookmarksopenlevel=0,
    hypertexnames=false,
    colorlinks=true,
    citecolor=[rgb]{0.286, 0.427, 0.537},
    linkcolor=[rgb]{0.161, 0.31, 0.427},
    urlcolor=[rgb]{0.071, 0.212, 0.322},
    pdfstartview={FitV},
    unicode,
    breaklinks=true
}

%----------------------------------------
%   FONT SETUP
%----------------------------------------
\renewcommand*\rmdefault{lmr}
\renewcommand*\sfdefault{lmss}

\newfontfamily\blenderfont[
    Path=\fontsPath/Blender/,
    Extension=.ttf,
    UprightFont=*-Medium,
    BoldFont=*-Bold,
    ItalicFont=*-MediumItalic,
    BoldItalicFont=*-BoldItalic
]{Blender}
\newfontfamily\dinfont[
    Path=\fontsPath/DIN/,
    Extension=.ttf,
    UprightFont=*-Regular,
    BoldFont=*-Bold,
    ItalicFont=*-Italic,
    BoldItalicFont=*-BoldItalic
]{DIN}

\setsansfont{Blender}[
    Path=\fontsPath/Blender/,
    Extension=.ttf,
    UprightFont=*-Medium,
    BoldFont=*-Bold,
    ItalicFont=*-MediumItalic,
    BoldItalicFont=*-BoldItalic
]
\setmainfont{DIN}[
    Path=\fontsPath/DIN/,
    Extension=.ttf,
    UprightFont=*-Regular,
    BoldFont=*-Bold,
    ItalicFont=*-Italic,
    BoldItalicFont=*-BoldItalic
]


%----------------------------------------
%   PAGE SETUP
%----------------------------------------

\pagestyle{fancy}

\renewcommand{\headrulewidth}{0pt}

%----------------------------------------
%   LOGOS
%----------------------------------------

% ==== Add more logos to be placed besides the main logo on the title page ====
\newcommand{\logosScale}{1}
\newcommand{\mainLogoScale}{1}

\newlength{\imageHeight}


\DeclareRobustCommand{\SetLogosScale}[1]{
    \renewcommand{\logosScale}{#1}
}

\let\newglobalarray\newarray
\patchcmd{\newglobalarray}{\edef}{\xdef}{}{}

% AddLogo Command with automatic counter
% Array to store the logos and to be used in pgffor later
\newglobalarray\LogosPaths
\newglobalarray\LogosScales
\newglobalarray\LogosOpacities
\newglobalarray\LogosExtensions
\newcounter{logoCounter}
\setcounter{logoCounter}{0}
\expandarrayelementtrue
% Command to add a logo to the array
% Usage: \AddLogoToArray{<Name>}{<Scale>}{<Opacity>}
\DeclareRobustCommand{\AddLogo}[4]{
    \stepcounter{logoCounter}
    \LogosPaths(\thelogoCounter)={#1}
    \LogosScales(\thelogoCounter)={#2}
    \LogosOpacities(\thelogoCounter)={#3}
    \LogosExtensions(\thelogoCounter)={#4}
}

% Command to get opacity of a logo
\DeclareRobustCommand{\GetLogoOpacity}[1]{
    \expand{\LogosOpacities(#1)}
}

% =============================================================================
\def\skylinePath{TeX/Assets/Images/Skyline.svg}

\newcommand{\footerYShift}{1.5em}
\newcommand{\footerXShift}{0.7em}

% Define the logo and its position
\AtBeginPage{
    \setlength{\imageHeight}{2cm*\real{\mainLogoScale}*\real{\logosScale}*\real{0.45}}
    \begin{tikzpicture}[overlay, remember picture]
        % === Main Logo(s) ===
        \node[anchor=south east, inner sep=0pt, xshift=-\footerXShift, yshift=\footerYShift] (logo0) at (current page.south east) {
            \strcompare{\thepage}{1}{}{
                \includesvg[height=\imageHeight]{\imagesPath/DUMMY_FOOT.svg}
            }
        };
        \ifnum\thepage=1
        \else
        % For loop to place all logos
        \foreach \i in {1,...,\value{logoCounter}} {
            % Calculate name for i-1
            \pgfmathtruncatemacro{\prev}{\i-1}
            \node[anchor=east, inner sep=0pt, xshift=-0.1cm, opacity=0.3] (logo\i) at (logo\prev.west) {
                \makeatletter
                \testarray{LogosScales}(\i)
                \setlength{\imageHeight}{1.5cm*\real{\temp@macro}*\real{\logosScale}*\real{0.55}}
                \testarray{LogosExtensions}(\i)
                \let\extension\temp@macro
                \testarray{LogosOpacities}(\i)
                \let\opacity\temp@macro
                \testarray{LogosPaths}(\i)
                \ifthenelse{\equal{\extension}{svg}}{
                    \begin{tikzpicture}
                        \node[opacity=\opacity] {
                            \includesvg[height=\imageHeight]{TeX/Assets/Images/\temp@macro.\extension}
                        };
                    \end{tikzpicture}
                }{
                    \begin{tikzpicture}
                        \node[opacity=\opacity] {
                            \includegraphics[height=\imageHeight]{TeX/Assets/Images/\temp@macro.\extension}
                        };
                    \end{tikzpicture}
                }
                \makeatother
            };
        }
        \fi
        % Skyline
        \node[anchor=south west, inner sep=0pt, yshift=0em] at (current page.south west) {
            \includesvg[width=1.5\paperwidth]{\skylinePath}
        };
        \filldraw[white] (logo0.north east) rectangle (logo0.south west);
    \end{tikzpicture}
}

%----------------------------------------
%   TITLE PAGE
%----------------------------------------
\setkomafont{pagenumber}{\color{gray}\blenderfont\selectfont}

\fancyhf{}
\fancyhead[L]{\color{gray}\blenderfont \@title}
\fancyhead[R]{\pagemark}
\fancyfoot[C]{}

\fancypagestyle{fancy}{
    \fancyhf{}
    \fancyhead[L]{\color{gray}\blenderfont \@title}
    \fancyhead[R]{\pagemark}
    \fancyfoot[C]{}
}

% Set font for all headings
\setkomafont{disposition}{\sffamily\bfseries}
\setkomafont{chapter}{\Large\sffamily\bfseries}
\setkomafont{section}{\Large\sffamily\bfseries}
\setkomafont{subsection}{\large\sffamily\bfseries}
\setkomafont{subsubsection}{\large\sffamily\bfseries}

\RedeclareSectionCommand[
    beforeskip=1ex,
    afterskip=0.5ex,
    style=section
]{chapter}

\RedeclareSectionCommands[
    beforeskip=0.6ex,
    afterskip=0.3ex,
]{section,subsection,subsubsection}

\newcommand{\decoRule}{\rule{.8\textwidth}{.4pt}}

\input{\classPath/Pages/Titlepage.tex}

%----------------------------------------
%   TABLE OF CONTENTS
%----------------------------------------
%\usepackage{tocloft}
%\renewcommand{\cftpartleader}{\cftdotfill{\cftdotsep}} % for parts
%\renewcommand{\cftchapleader}{\cftdotfill{\cftdotsep}} % for chapters

% Set Chapter font size in TOC
%\renewcommand{\cftchapfont}{\sffamily\bfseries\large}
