%
% AUTH report class
%
% note: Requires compilation with XeLaTeX or LuaTeX
%
% author: Christos Choutouridis <cchoutou@ece.auth.gr>
%
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{AUThReport}[2024-12-07 AUTh Report document class]

% Pre-option packages

% Options
% ===============================================

% Declare option for short report version
\DeclareOption{short}{\def\auth@short{yes}}

\def\auth@short{no}

% Pass all other options to article
\DeclareOption*{%
    \PassOptionsToClass{\CurrentOption}{article}
}
\ProcessOptions\relax

\LoadClass{article}


% Requirements
% ===============================================
\RequirePackage{iftex}

% Font packages
\ifPDFTeX
    \RequirePackage[LGR, T1]{fontenc}   % ToDo: find a way to dynamicaly select encoding based on languages
    \RequirePackage[utf8]{inputenc}
    \RequirePackage{lmodern}
\else
    \RequirePackage{fontspec}
\fi

% Language packages
\ifPDFTeX
    % Postponed to Pre-Begin hook
    %    \usepackage{babel}
\else
    \typeout{[DEBUG] Polyglossia Included}
    \RequirePackage{polyglossia}%
\fi


\RequirePackage{xargs}      % Commands

\RequirePackage{geometry}   % Layout
\RequirePackage{fancyhdr}

\RequirePackage{graphicx}   % Figures

% Format packages
\RequirePackage[hang, small,labelfont=bf,up,textfont=it,up]{caption} % Custom captions under/above floats in tables or figures
\RequirePackage[nobottomtitles]{titlesec}
\RequirePackage{titling}
\RequirePackage[bottom]{footmisc} % Place footnotes always as much to the bottom as possible.

\RequirePackage{hyperref}   % Various
\RequirePackage[ddmmyyyy]{datetime}
\RequirePackage{enumitem}
\RequirePackage{multicol}

% Local Tools
% ===============================================

% Tool to check equality
\newcommand{\isequal}[2]{ \pdf@strcmp{#1}{#2} }

% syntactic suggar for horizaontal rule
\newcommand{\Hrule}{ \rule{\linewidth}{0.1mm} }

\newcommand{\authAuthorTitleText}{%
    \ifnum \isequal{\auth@mainlanguage}{greek}=0
        \ifdefined \auth@coauthorName Συντάκτες\else Συντάκτης\fi
    \else
        \ifdefined \auth@coauthorName Authors\else Author\fi
    \fi
}


\newcommand{\authInstructorTitleText}{%
    \ifnum \isequal{\auth@mainlanguage}{greek}=0
        \ifdefined \auth@coinstructorName Διδάσκοντες\else Διδάσκων\fi
    \else
        \ifdefined \auth@coinstructorName Instructors\else Instructor\fi
    \fi
}

\newcommand{\authMailText}[1]{%
    \ifnum \isequal{\auth@mainlanguage}{greek}=0
        \href{mailto:#1}{\eng{#1}}
    \else
        \href{mailto:#1}{#1}
    \fi
}

% Pre-Begin hook
% =====================================================
\newcommand{\authPreBeginHook}{}
\newcommand{\authAddToPreBeginHook}[1]{%
    \expandafter\gdef\expandafter\authPreBeginHook\expandafter{\authPreBeginHook#1}%
}

% Language
% ===============================================
% Init language variables
\newcommand{\auth@languages}{english}       % Pre-selected language
\newcommand{\auth@mainlanguage}{english}    % Pre-selected main language

\newcommand{\authSetupLanguageEnv}{%
    \ifPDFTeX
        \usepackage[\auth@languages, \auth@mainlanguage]{babel}
    \else
        % Set main language
        \setdefaultlanguage{\auth@mainlanguage}%
        \typeout{[DEBUG] setdefaultlanguage \auth@mainlanguage}
        % Set other languages
        \@for\auth@lang:=\auth@languages\do{%
            \setotherlanguage{\auth@lang}
            \typeout{[DEBUG] setotherlanguage \auth@lang}
        }%
    \fi
}

% Hook the babel setup
\authAddToPreBeginHook{%
    \authSetupLanguageEnv
}

% Theme
% ===============================================

% Layout
% =====================================================

% Page core layout settings (include head and foot)
% A4 will result to:
% 	Top margin:       12mm
% 	Bottom margin:    11mm
% 	Head hight:       14mm
% 	Foot skip:        15mm
% 	Left margin:      15mm
% 	Right margin:     12mm
\newcommand{\authTopMargin}{12mm}
\newcommand{\authBottomMargin}{12mm}
\newcommand{\authHeadHeight}{10mm}
\newcommand{\authFootSkip}{10mm}
\newcommand{\authLeftMargin}{15mm}
\newcommand{\authRightMargin}{15mm}


%
% Sets page layout. (include head and foot)
\newcommand*{\authSetPageLayout}{%
    \newgeometry{%
        includeheadfoot,
        top= \authTopMargin,
        bottom= \authBottomMargin,
        headheight= \authHeadHeight,
        footskip= \authFootSkip,
        lmargin= \authLeftMargin,
        rmargin= \authRightMargin
    }
}

% Fonts
% =====================================================

%
\newcommand{\authSetupFontEnv}{%
    \ifPDFTeX
        % PDFLaTeX fonts
    \else
%        \setmainfont{CMU-Serif-Roman.ttf}[%
%            Path=cmu/,
%            ItalicFont    = CMU-Serif-Italic.ttf,
%            BoldFont      = CMU-Serif-Bold.ttf,
%            BoldItalicFont= CMU-Serif-BoldItalic.ttf
%        ]
%        \setsansfont{CMU-Sans-Medium.ttf}[%
%            Path=cmu/,
%            ItalicFont    = CMU-Sans-Oblique.ttf,
%            BoldFont      = CMU-Sans-Bold.ttf,
%            BoldItalicFont= CMU-Sans-BoldOblique.ttf
%        ]
%        %\setsansfont{CMU-Concrete-Roman.ttf}[%
%        %    Path=cmu/,
%        %    ItalicFont    = CMU-Concrete-Italic.ttf,
%        %    BoldFont      = CMU-Concrete-Bold.ttf,
%        %    BoldItalicFont= CMU-Concrete-BoldItalic.ttf
%        %]
%        \setmonofont{CMU-Typewriter-Regular.ttf}[%
%            Path=cmu/,
%            ItalicFont    = CMU-Typewriter-Oblique.ttf,
%            BoldFont      = CMU-Typewriter-Bold.ttf,
%            BoldItalicFont= CMU-Typewriter-Oblique.ttf
%        ]
        \typeout{[DEBUG] set fonts}
        \setmainfont{CMU Serif}
        \setsansfont{CMU Sans Serif}
    \fi

}


% Header and Footer
% =====================================================

\newcommand*{\authSetFooterEmpty}{%
    % Clear styles
    \fancyfoot{}
    \renewcommand{\footrulewidth}{0pt}
}

\newcommand*{\authSetFooter}{%
    % Clear styles
    \fancyfoot{}
    \fancyfoot[C]{\thepage}
    \renewcommand{\footrulewidth}{0pt}
    \pagenumbering{arabic}
}

\newcommand{\authSetHeaderEmpty}{
    % Clear styles
    \fancyhead{}
    \renewcommand{\headrulewidth}{0pt}
}

\newcommand{\authSetHeader}{
    % Clear styles
    \fancyhead{}
    \fancyhead[L]{\auth@className}
    \fancyhead[R]{\auth@docTitle}
    \renewcommand{\headrulewidth}{0.5pt}
}


%
% Page styles
%
\fancypagestyle{plain}{%
    % Style for the main document (including TOC)
    \authSetFooter
    \authSetHeader
}

\fancypagestyle{authTitlePageStyle}{%
    % Style for Title page
    \authSetHeaderEmpty
    \ifnum \isequal{\auth@short}{no}=0
        \authSetFooterEmpty
    \else
        \authSetFooter
    \fi
}


% Common Page style
\newcommand{\authPageStyle}{%

    \renewcommand{\bottomtitlespace}{0.12\textheight}

    % Change the look of the section and subsection titles
    \titleformat{\section}[block]{\large\scshape}{\thesection.}{1em}{}
    \titleformat{\subsection}[block]{\large}{\thesubsection.}{1em}{}
    \titleformat{\subsubsection}[block]{\normalsize}{\thesubsubsection.}{1em}{}

    %\setlist[itemize]{noitemsep}        % Make itemize lists more compact
    \setlist[itemize]{topsep=6pt, partopsep=0pt, itemsep=3pt, parsep=3pt}

    % Set paragraph identation and skip
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{0.3em}

}


% Setup theme
% =====================================================

\hypersetup{
    colorlinks=true,
    linkcolor=red,   % Internal links, those generated by cross-referenced elements
    filecolor=blue,  % Links to local files
    urlcolor=blue    % Links to web sites
}

\AtBeginDocument{%
%    % Language
%    \authSetupLanguageEnv
    % Fonts
    \authSetupFontEnv

    % Page layout (check above for the geometry setup)
    \authSetPageLayout

	%hyphenation settiongs
	\lefthyphenmin=2 	% minimum characters before
	\righthyphenmin=2 	% minimum characters after

    % Header - Footer
  	\pagestyle{plain}
    \thispagestyle{authTitlePageStyle}

    % Page styles and formating
    \authPageStyle

    \normalsize
} % End \AtBeginDocument



% Public environments/commands
% =====================================================

% Common fields
% =====================================================
\newcommand*{\AuthorName}[1]{\def\auth@authorName{#1}}
\newcommand*{\AuthorMail}[1]{\def\auth@authorMail{#1}}
\newcommand*{\AuthorAEM}[1]{\def\auth@authorAEM{#1}}

\newcommand*{\CoAuthorName}[1]{\def\auth@coauthorName{#1}}
\newcommand*{\CoAuthorMail}[1]{\def\auth@coauthorMail{#1}}
\newcommand*{\CoAuthorAEM}[1]{\def\auth@coauthorAEM{#1}}

\newcommand*{\WorkGroup}[1]{\def\auth@workGroup{#1}}

\newcommand*{\DocTitle}[1]{\def\auth@docTitle{#1}}
\newcommand*{\DocSubTitle}[1]{\def\auth@docSubTitle{#1}}
\newcommand*{\Department}[1]{\def\auth@department{#1}}
\newcommand*{\ClassName}[1]{\def\auth@className{#1}}

\newcommand*{\InstructorName}[1]{\def\auth@instructorName{#1}}
\newcommand*{\InstructorMail}[1]{\def\auth@instructorMail{#1}}
\newcommand*{\CoInstructorName}[1]{\def\auth@coinstructorName{#1}}
\newcommand*{\CoInstructorMail}[1]{\def\auth@coinstructorMail{#1}}

\newcommand*{\CurrentDate}[1]{\def\auth@date{#1}}


% Language
% =====================================================
\newcommand{\authSetMainLanguage}[1]{%
    \renewcommand{\auth@mainlanguage}{#1}
}

\newcommand{\authSetLanguages}[1]{%
    \renewcommand{\auth@languages}{#1}
}

% \eng{}
% syntactic suggar for english text inside greek
% example \eng{A quick brown fox jumpedover the lazy dog}
\newcommand{\eng}[1]{%
    \ifPDFTeX
        \foreignlanguage{english}{#1}
    \else
        #1
    \fi
}

% \gr{}
% syntactic suggar for greek text inside english
% example \gr{Άσπρη πέτρα ξέξασπρη κι απτον ήλιο ξεξασπρότερη}
\newcommand{\gr}[1]{%
    \ifPDFTeX
        \foreignlanguage{greek}{#1}
    \else
        #1
    \fi
}

% \setEnglish
% Selects english for the rest of the text
\newcommand{\setEnglish}{%
    \ifPDFTeX \selectlanguage{english} \typeout{[DEBUG] set Eng} \else \typeout{[DEBUG] No set Eng}\fi
}

% \setGreek
% Selects greek for the rest of the text
\newcommand{\setGreek}{%
    \ifPDFTeX \selectlanguage{greek} \typeout{[DEBUG] set Gre}\else \typeout{[DEBUG] NO set Gre} \fi
}


% Content commands
% =====================================================

\newcommand{\authInsertAuthsInstructors}{%
    \center
    \noindent
    % Authors
    \hspace*{0.05\textwidth}
    \begin{minipage}[t]{0.35\textwidth}
        \begin{flushleft}
            \large
            \ifdefined \auth@workGroup \emph{\textsc{\auth@workGroup}} \\[2ex]\fi
            \emph{\authAuthorTitleText:}  \\
            \auth@authorName\hspace{1ex}[\auth@authorAEM] \\[0.35ex]
            \normalsize	\authMailText{\auth@authorMail}  \\[2ex]
            \large
            \ifdefined \auth@coauthorName
                \auth@coauthorName\hspace{1ex}[\auth@coauthorAEM] \\[0.35ex]
                \normalsize \authMailText{\auth@coauthorMail}  \\[2ex]
            \fi
        \end{flushleft}
    \end{minipage}
    \hfill
    % Instructors
    \begin{minipage}[t]{0.35\textwidth}
        \begin{flushright}
            \large
            \emph{\authInstructorTitleText:}  \\
            \auth@instructorName \\[0.35ex]
            \normalsize \authMailText{\auth@instructorMail} \\[2ex]
            \large
            \ifdefined \auth@coinstructorName
            \auth@coinstructorName \\[0.35ex]
            \normalsize \authMailText{\auth@coinstructorMail}  \\[2ex]
            \fi
        \end{flushright}
    \end{minipage}
    \hspace*{0.05\textwidth}
}
% \FirstPage
\newcommand{\authTitlePage}{%
    \begin{titlepage}
        % Logo
        \begin{figure}
            \begin{flushleft}
                \includegraphics[width=5cm]{img/auth_logogr.png}
            \end{flushleft}
        \end{figure}
        % Titles
        \center                                 % Center everything on the page
        \textbf{}                            \\[3.5cm]   % vertical space
        \textsc{\Large \auth@department}     \\[0.5cm]   % Department
        \textsc{\Large \auth@className}      \\[1.5cm]   % Class/Course name
        \Hrule                               \\[0.4cm]   % -------
        {\huge \bfseries \auth@docTitle}     \\[0.4cm]   % Document/Assignement Title
        {\LARGE \bfseries \auth@docSubTitle} \\[0.1cm]   % Document/Assignement Sub-Title
        \Hrule                               \\[3.5cm]   % -------
        % Authors-Instructors
        \authInsertAuthsInstructors
        \vfill
        {\large \auth@date}  \\ % date

    \end{titlepage}
    \newpage
}

% \TitleHeader
\newcommand{\authTitleHeader}{%
    \begin{center}                      % Center everything on the page
        % Logo
        \noindent
        \begin{figure}
            \vspace{-0.8cm}
            \center
            \includegraphics[width=5cm]{img/auth_logogr.png}
        \end{figure}
        % Titles
        \textsc{\Large \auth@department}     \\[0.4cm]   % Department
        \textsc{\Large \auth@className}      \\[0.1cm]   % Class/Course name
        {\Large \bfseries \auth@docTitle}    \\[0.2cm]   % Document/Assignement Title
        {\large \bfseries \auth@docSubTitle} \\[0.2cm]   % Document/Assignement Sub-Title
        {\large \auth@date}                  \\[0.4cm]   % date
        % Authors-Instructors
        \authInsertAuthsInstructors
    \end{center}
}

\newcommand{\InsertTitle}{%
    \ifnum \isequal{\auth@short}{no}=0
        \authTitlePage
    \else
        \authTitleHeader
    \fi
}

% \InsertFigure{}{}{}{}{}
% [1]: Position specifier
% [2]: scale
% [3]: Label
% [4]: Figure file
% [5]: Caption text
% example: \InsertFigure{!h}{0.8}{fig:lala}{lala.png}{\eng{Makes lala}}
\newcommand{\InsertFigure}[5]{
    \begin{figure}[#1]
        \captionsetup{format=plain}
        \centering
        \includegraphics[width=#2\textwidth]{#4}
        \caption{#5}
        \label{#3}
    \end{figure}
}

% \WrapFigure{}{}{}{}
% [1]: scale
% [2]: place left or right r,l,R,L
% [3]: Label
% [4]: Figure file
% [5]: Caption text
% example: \WrapFigure{0.8}{r}{fig:lala}{lala.png}{\eng{Makes lala}}
\newcommand{\WrapFigure}[5]{
    \begin{wrapfigure}{#2}{#1\textwidth}
        \vspace{-20pt}
        \begin{center}
            \captionsetup{format=plain}
            \includegraphics[width=#1\textwidth]{#4}
            \caption{#5}
            \label{#3}
        \end{center}
        \vspace{-20pt}
    \end{wrapfigure}
}
