% Beamer mtheme
%
% Copyright 2014 Matthias Vogelgesang
% Licensed under CC-BY-SA 4.0 International.
%
% The initial template comes from the HSRM beamer theme by Benjamin Weiss, which
% you can find at https://github.com/hsrmbeamertheme/hsrmbeamertheme.
%

\ProvidesPackage{beamerthemem}

%{{{ --- Packages ---------------------

\usepackage[no-math]{fontspec}
\usepackage{etoolbox}
\usepackage{tikz}

\usetikzlibrary{backgrounds}

%}}}
%{{{ --- Options ----------------------

\newif\if@doSectionPage
\@doSectionPagefalse
\DeclareOption{nosectionpages}{\@doSectionPagetrue}
\ProcessOptions

%}}}
%{{{ --- Colors -----------------------

% http://paletton.com/#uid=7050t0kkJkJsntwoyp6gYgoddc4

\definecolor{mDarkBrown}{HTML}{604c38}
\definecolor{mDarkTeal}{HTML}{23373b}

\definecolor{mLightBrown}{HTML}{EB811B}
\definecolor{mMediumBrown}{HTML}{C87A2F}

\setbeamercolor{background canvas}{bg=black!2}
\setbeamercolor{normal text}{fg=mDarkTeal}
\setbeamercolor{alerted text}{fg=mLightBrown}
\setbeamercolor{structure}{fg=mDarkTeal}
\setbeamercolor{palette primary}{fg=mDarkBrown}
\setbeamercolor{palette secondary}{fg=mDarkBrown}
\setbeamercolor{palette tertiary}{fg=white, bg=mMediumBrown}
\setbeamercolor{palette quaternary}{fg=white, bg=mDarkTeal}
\setbeamercolor{frametitle}{parent=palette quaternary}

\setbeamercolor{page number in head/foot}{fg=mDarkTeal}
%}}}
%{{{ --- Fonts ------------------------

\defaultfontfeatures{Mapping=tex-text}
\setsansfont[BoldFont={Fira Sans}]{Fira Sans Light}
\setmonofont{Fira Mono}
\newfontfamily\ExtraLight{Fira Sans ExtraLight}
\newfontfamily\Light{Fira Sans Light}
\newfontfamily\Book{Fira Sans}
\newfontfamily\Medium{Fira Sans Medium}

\AtBeginEnvironment{tabular}{\setsansfont[BoldFont={Fira Sans}, Numbers={Monospaced}]{Fira Sans Light}}

\setbeamerfont{title}{family=\Book, size=\Large}
\setbeamerfont{author}{family=\ExtraLight, size=\small}
\setbeamerfont{date}{family=\ExtraLight, size=\small}

\setbeamerfont{section title}{family=\Book, size=\LARGE}

\setbeamerfont{block title}{family=\Book,size=\normalsize}
\setbeamerfont{block title alerted}{family=\Book,size=\normalsize}

\setbeamerfont{subtitle}{family=\Light, size=\fontsize{12}{14}}
\setbeamerfont{frametitle}{family=\Book, series=\scshape, size=\large}

\setbeamerfont{alerted text}{family=\Light}

\setbeamerfont{caption}{size=\small}
\setbeamerfont{caption name}{family=\Book}

\setbeamerfont{description item}{family=\Book}
%}}}
%{{{ --- Titlepage --------------------

\def\maketitle{\ifbeamer@inframe\titlepage\else\frame[plain]{\titlepage}\fi}

\def\titlepage{\usebeamertemplate{title page}}
\setbeamertemplate{title page}
{
  \begin{minipage}[b][\paperheight]{\textwidth}
    \vfill
    \ifx\inserttitle\@empty%
    \else%
    {\raggedright\linespread{0.8}\usebeamerfont{title}\usebeamercolor[fg]{title}\scshape\MakeLowercase{\inserttitle}\par}%
    \vspace*{0.5em}
    \fi%
    \ifx\insertsubtitle\@empty%
    \else%
    {\usebeamerfont{subtitle}\usebeamercolor[fg]{subtitle}\insertsubtitle\par}%
    \vspace*{0.5em}
    \fi%
    \begin{tikzpicture}\draw[mLightBrown] (0, 0) -- (\textwidth, 0);\end{tikzpicture}%
    \vspace*{0.5em}
    \ifx\insertauthor\@empty%
    \else%
    {\usebeamerfont{author}\usebeamercolor[fg]{author}\insertauthor\par}%
    \vspace*{0.25em}
    \fi%
    \ifx\insertdate\@empty%
    \else%
    {\usebeamerfont{date}\usebeamercolor[fg]{date}\insertdate\par}%
    \fi%
    \ifx\insertinstitut\@empty%
    \else%
    \vspace*{3mm}
    {\usebeamerfont{institute}\usebeamercolor[fg]{institute}\insertinstitute\par}%
    \fi%
    \vfill
    \vspace*{5mm}
  \end{minipage}
}

%}}}
%{{{ --- Progressbar ------------------

\makeatletter
\def\progressbar@progressbar{}  % the progress bar
\newcount\progressbar@tmpcounta % auxiliary counter
\newcount\progressbar@tmpcountb % auxiliary counter
\newdimen\progressbar@pbht      % progressbar height
\newdimen\progressbar@pbwd      % progressbar width
\newdimen\progressbar@tmpdim    % auxiliary dimension

\progressbar@pbwd=20em
\progressbar@pbht=0.5pt

% the progress bar
\def\progressbar@progressbar{%
  \progressbar@tmpcounta=\insertframenumber
  \progressbar@tmpcountb=\inserttotalframenumber
  \progressbar@tmpdim=\progressbar@pbwd
  \multiply\progressbar@tmpdim by \progressbar@tmpcounta
  \divide\progressbar@tmpdim by \progressbar@tmpcountb

  \makebox[\textwidth][c]{
    \begin{tikzpicture}[tight background]

      \node[anchor=west, white, inner sep=0pt] at (0pt, 0pt) {\insertsectionHEAD};

      \draw[anchor=west, mDarkBrown, fill=mDarkBrown, inner sep=0pt]
      (2pt, -16pt) rectangle ++ (\progressbar@pbwd, \progressbar@pbht);

      \draw[anchor=west, mMediumBrown, fill=mMediumBrown, inner sep=0pt]
      (2pt, -16pt) rectangle ++ (\progressbar@tmpdim, \progressbar@pbht);
    \end{tikzpicture}%
  }
}

%}}}
%{{{ --- Commands ---------------------

\newcommand{\insertsectionHEAD}{%
  \expandafter\insertsectionHEADaux\insertsectionhead}
  \newcommand{\insertsectionHEADaux}[3]{\textsc{\MakeLowercase{#3}}
}

\newcommand{\rightTextArrow}{→}

\newcommand{\plain}[2]{%
  \begingroup
  \setbeamercolor{background canvas}{bg=mDarkTeal}
  \begin{frame}{#1}
    \centering
    \vfill\vspace{1em}\usebeamerfont{section title}\textcolor{white}{\scshape #2}\vfill
  \end{frame}
  \endgroup
}

%}}}
%{{{ --- Itemize ----------------------

\setbeamertemplate{itemize item}{\rightTextArrow}
\setbeamertemplate{itemize subitem}{\rightTextArrow}
\setbeamertemplate{itemize subsubitem}{\rightTextArrow}
\setlength{\parskip}{0.5em}

%}}}
%{{{ --- Sections ---------------------

\if@doSectionPage\@empty
\else
% Insert frame with section title at every section start
\AtBeginSection[]
{
  \begingroup
  \setbeamercolor{background canvas}{bg=mDarkTeal}
  \begin{frame}[plain]
    \vspace{2em}\usebeamerfont{section title}
    \progressbar@progressbar%
  \end{frame}
  \endgroup
}
\fi

%}}}
%{{{ --- Captions ---------------------

\setbeamertemplate{caption label separator}{: }

%}}}
%{{{ --- Footline ---------------------

\usenavigationsymbolstemplate{}
\setbeamertemplate{footline}
{%
\begin{beamercolorbox}[wd=\textwidth,ht=3ex,dp=1.5ex,leftskip=0.3cm,rightskip=0.3cm]{structure}%
\hfill\usebeamerfont{page number in head/foot}%
\insertframenumber%
\end{beamercolorbox}%
}

%}}}
%{{{ --- Frametitle -------------------
\setbeamertemplate{frametitle}{%
\nointerlineskip
\begin{beamercolorbox}[wd=\paperwidth,leftskip=0.3cm,rightskip=0.3cm,ht=2.5ex,dp=1.5ex]{frametitle}
  \usebeamerfont{frametitle}\MakeLowercase{\insertframetitle}%
\end{beamercolorbox}
}
%}}}

\mode
<all>

% {{{ misc
\let\otp\titlepage
\renewcommand{\titlepage}{\otp\addtocounter{framenumber}{-1}}

\linespread{1.15}
%}}}
