\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{UiO_profile/UiO_profile}[2023/01/01 Package that tries to follow the UiO profile]

% Required packages
\RequirePackage[T1]{fontenc}
\RequirePackage{xcolor}

% Options
\DeclareOption{binding}{\def \uiop@bind {24pt}}
\DeclareOption{font=cmr}{\def \uiop@font {\RequirePackage{lmodern}
    \RequirePackage[scaled]{helvet}}}
\DeclareOption{font=garamond}{\def \uiop@font {\RequirePackage[garamond]{mathdesign}
    \RequirePackage[scaled]{helvet}}}
\DeclareOption{font=times}{\RequirePackage{txfonts}}

\def \uiop@bind {0pt}
\def \uiop@base {report}

% UiO Brand colors
% Blue
\definecolor{UiOBlue}{RGB}{67, 101, 226}
\definecolor{UiODarkBlue}{RGB}{124, 155, 206}
\definecolor{UiOLightBlue}{RGB}{223, 231, 243}
% Red
\definecolor{UiORed}{RGB}{182, 0, 0}
\definecolor{UiODarkRed}{RGB}{251, 102, 102}
\definecolor{UiOLightRed}{RGB}{253, 224, 225}
% Green
\definecolor{UiOGreen}{RGB}{182, 0, 0}
\definecolor{UiODarkGreen}{RGB}{116, 198, 165}
\definecolor{UiOLightGreen}{RGB}{197, 228, 207}

\definecolor{UiOFrontpageBackground}{RGB}{255, 255, 255}
\definecolor{UiOFrontpagePrimary}{RGB}{220, 221, 223}
\DeclareOption{red}{
  \colorlet{UiOFrontpagePrimary}{UiODarkRed}
  \colorlet{UiOFrontpageBackground}{UiOLightRed}
}
\DeclareOption{blue}{
  \colorlet{UiOFrontpagePrimary}{UiODarkBlue}
  \colorlet{UiOFrontpageBackground}{UiOLightBlue}
}
\DeclareOption{green}{
  \colorlet{UiOFrontpagePrimary}{UiODarkGreen}
  \colorlet{UiOFrontpageBackground}{UiOLightGreen}
}

\newif\iflangno
\DeclareOption{no}{\langnotrue}

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{\uiom@base}}

\ExecuteOptions{font=cmr}

\ProcessOptions\relax

\LoadClass[a4paper,11pt,twoside,openright]{report}

% Margins
\RequirePackage[vmargin=3cm,bindingoffset=\uiop@bind]{geometry}

% Adjustements
\pretolerance = 3000
\tolerance = 6000   \hbadness = \tolerance
\setlength{\parskip}{0cm plus 1mm}

% Font
\uiop@font
\renewcommand*\familydefault{\sfdefault} 

% Other required packages for frontpage and stuff
\RequirePackage{titling}

%% Chapter and section headers:

%% Redefine \begin{abstract}...\end{abstract}
\renewenvironment{abstract}{%
  \ifthenelse{\equal{\uiom@base}{report}}
    {\cleardoublepage
     \markboth{\abstractname}{}%           
     \vspace*{5\baselineskip}}%
    {\bigskip}%
  \small
  \begin{center}%
    {\sf\bfseries \abstractname\vspace{-.5em}\vspace{\z@}}%
  \end{center}%
  \quotation}%
{\endquotation \bigskip}

%% Define \begin{preface}
\newenvironment{preface}{%
  \chapter*{\prefacename}
    \markboth{\prefacename}{}}
  {}

%% Headers and footers:
\RequirePackage{fancyhdr}
\fancyhead{}
\setlength{\headheight}{15pt}
\fancyhead[LE]{\textsf{\small \nouppercase{\leftmark}}}
\fancyhead[RO]{\textsf{\small \nouppercase{\rightmark}}}
\renewcommand{\headrulewidth}{0pt}
\fancyfoot{}
\fancyfoot[LE,RO]{\textsf{\thepage}}
\renewcommand{\footrulewidth}{0pt}
\pagestyle{fancy}
\fancypagestyle{plain}{\fancyhead{}}

%% Figurer og tabeller
\RequirePackage[labelfont=sf,font=small]{caption}

%% Document parts:
\newcommand{\frontmatter}{\cleardoublepage \pagenumbering{roman}}
\newcommand{\mainmatter}{\cleardoublepage \pagenumbering{arabic}}
\newcommand{\backmatter}{\cleardoublepage}

\providecommand{\preface}{\chapter*{}\markboth{}{}}
\providecommand{\endpreface}{}

% Titlepage
\RequirePackage{tikz}
\RequirePackage[export]{adjustbox}
\newcommand{\subtitle}[1]{
  \def\@subtitle{#1}
}
\newcommand{\deliverableversion}[1]{
  \def\@deliverableversion{#1}
}
\newcommand{\programname}[1]{
  \def\@programname{#1}
}
\newcommand{\studypoints}[1]{
  \def\@studypoints{#1}
}
\newcommand{\branch}[2]{
  \def\@institute{#1}
  \def\@faculty{#2}
}
\newcommand{\uiotitlepage}{
  \thispagestyle{empty}
  \begin{titlepage}
    \begin{tikzpicture}[remember picture,overlay]
      % Bakgrunn
      \node (back names) [shape=rectangle, fill=UiOFrontpageBackground, minimum height=\paperheight, minimum width=\paperwidth, anchor=south west] at (current page.south west) {};

      % Segl
      \node[inner sep=0pt, anchor=south east] (uioname) at ([shift={(-10mm,10mm)}]back names.south east) {
        \includegraphics[width=23.7mm,right]{UiO_profile/profiles/03_uio_segl_pos.png}
      };

      % Dato
      \node[anchor=south west] (uioname) at ([shift={(10mm,10mm)}]back names.south west) {
        \ifdefined\thedate
          \thedate
        \else
        \fi
      };

      % "Ribbon" bakgrunn
      \node (names) [shape=rectangle, fill=UiOFrontpagePrimary, minimum height=.25\paperheight, minimum width=\paperwidth, anchor=north west] at (current page.north west) {};

      % Navnetrekk
      \node[inner sep=0pt, anchor=north west] (uioname) at ([shift={(10mm,-10mm)}]names.north west) {
        \iflangno
          \includegraphics[width=63mm]{UiO_profile/profiles/03_uio_navnetrekk_no_pos.png}
        \else
          \includegraphics[width=63mm]{UiO_profile/profiles/03_uio_navnetrekk_eng_pos.png}
        \fi
      };

      % Chip 1
      \node[anchor=north west, fill=black, text=white, inner sep=5pt] (chip1) at ([shift={(10mm,15mm)}]names.south west) {
        \ifdefined\@deliverableversion
          \@deliverableversion
        \else
        \fi
      };

      % Tittel
      \node[anchor=north west] (title) at ([shift={(10mm,-10mm)}]names.south west) {
        \ifdefined\thetitle
          {\sf\Huge\thetitle}
        \else
        \fi
      };

      % Undertittel
      \node[anchor=north west] (subtitle) at ([shift={(0,\ifdefined\thetitle -5mm\else 0\fi)}]title.south west) {
        \ifdefined\@subtitle
          \@subtitle
        \else
        \fi
      };

      % Forfatter
      \node[anchor=north west] (author) at ([shift={(0,-5mm)}]\ifdefined\@subtitle subtitle\else title\fi.south west) {
        \theauthor
      };

      % Studieprogram
      \node[anchor=north west] (program) at ([shift={(0,-5mm)}]\ifdefined\theauthor author\else subtitle\fi.south west) {
        \ifdefined\@programname
          \@programname
        \else
        \fi
      };

      % Studiepoeng
      \node[anchor=north west] (points) at ([shift={(0,0)}]program.south west) {
        \ifdefined\@studypoints
          \@studypoints\ \iflangno Studiepoeng\else Study points\fi
        \else
        \fi
      };

      % Institutt
      \node[anchor=north west] (institute) at ([shift={(0,-5mm)}]\ifdefined\@studypoints points\else program\fi.south west) {
        \ifdefined\@institute
         \@institute
        \else
        \fi
      };

      % Fakultet
      \node[anchor=north west] (faculty) at ([shift={(0,0)}]institute.south west) {
        \ifdefined\@faculty
          \@faculty
        \else
        \fi
      };

    \end{tikzpicture}
    \newpage
  \end{titlepage}
}
