% Template for Statale presentations with LaTeX beamer.
%
% Adapted by Srikar Sharma Sadhu, srikar.sadhu@research.iiit.ac.in for IIIT-Hyderabad.
%
% Adapted by Giorgio Marchetti, ciao@gio.im for Statale presentations.
% 
% Original template by Federico Zenith, federico.zenith@sintef.no.
% Derived (through several iterations) from Håvard Berland's
% beamerthementnu class.
%
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program.  If not, see <http://www.gnu.org/licenses/>.

% TODO: remove top-right logo from title page

\ProvidesPackage{beamertheme_statale}[2023/11/01]

\RequirePackage{etoolbox}
\RequirePackage{tikz}
\RequirePackage{pgfplots}

% Force 16:9 aspect ratio
\RequirePackage{geometry}
\geometry{paperwidth=16cm,paperheight=9cm}

% Caladea is a Cambria clone, Carlito a Calibri clone
\RequirePackage{caladea,carlito}
\renewcommand{\familydefault}{\sfdefault}

\RequirePackage{graphicx,statale_colors}

% No navigation symbols
\setbeamertemplate{navigation symbols}{}

\newcommand{\@footlineauthor}{\insertauthor \enspace$\vert$\enspace}
\newcommand{\@footlinepayoff}{\@footlineauthor\inserttitle}
\newcommand{\footlinepayoff}[1]{\renewcommand{\@footlinepayoff}{#1}}
% Define footline content
\setbeamertemplate{footline}{%
  \begin{beamercolorbox}[wd=\textwidth,ht=5mm,dp=3mm,rightskip=1cm,leftskip=1cm]{footline}
  \insertframenumber/\inserttotalframenumber
  \ifstrempty{\@footlinepayoff}{}{%
    \usebeamerfont{footline}\hfill\@footlinepayoff
  }
  \end{beamercolorbox}
}

% Set footline AND coordinate blocks with it
\newcommand{\footlinecolor}[1]{% if #1 is empty, makes footline transparent
  \ifstrempty{#1}{%
    \footlinepayoff{}
    \setbeamercolor{footline}{fg=maincolor, bg=}
    \setbeamercolor{block title}{fg=toctxt,bg=frmtxt}
    \setbeamercolor{block body}{fg=blkc,bg=maincolor}
  }{
    \footlinepayoff{\@footlineauthor\inserttitle}
    \setbeamercolor{footline}{fg=toctxt,bg=#1}
    \setbeamercolor{block title}{fg=toctxt,bg=#1}
    \setbeamercolor{block body}{fg=toctxt,bg=#1}
  }%
}
\footlinecolor{} % Default: no footline

\pgfdeclareimage[width=0.09\paperwidth]{logo}{assets/logo_RGB}
\pgfdeclareimage[width=0.09\paperwidth]{toctxtlogo}{assets/logo_RGB_negative}
\newcommand{\@logo}{logo}

% Set colours
% NOTE! Beamer colors should be set in the preamble. It often works fine in the 
%       document itself, but NOT for normal text foreground.
%       \themecolor should only be called from the preamble.
\newcommand{\themecolor}[1]{
	\ifstrequal{#1}{main}{%
        \setbeamercolor{footline}{fg=toctxt}
		\setbeamercolor{normal text}{fg=toctxt,bg=maincolor}
		\setbeamercolor{structure}{fg=toctxt}
		\renewcommand{\@logo}{toctxtlogo}
		\setbeamercolor{block title}{fg=maincolor,bg=blkc}
		\setbeamercolor{block body}{fg=toctxt,bg=statalegrey}
	}{%
        \setbeamercolor{footline}{fg=maincolor}
		\setbeamercolor{normal text}{fg=maincolor,bg=toctxt}
		\setbeamercolor{structure}{fg=frmtxt}
		\renewcommand{\@logo}{logo}
		\setbeamercolor{block title}{fg=maincolor,bg=blkc}
		\setbeamercolor{block body}{fg=toctxt,bg=statalegrey}
	}
}
\themecolor{white} % Default: white theme

\setbeamercolor{title}{fg=frmtxt,bg=}
\setbeamercolor{alerted text}{fg=statalered}
\setbeamercolor{author}{fg=maincolor}
\setbeamercolor{date}{fg=maincolor}

\setbeamerfont{author}{size=\scriptsize}
\setbeamerfont{date}{size=\scriptsize}
\setbeamerfont{title}{series=\bfseries}
\setbeamerfont{subtitle}{series=\mdseries,size=\footnotesize}
\setbeamerfont{frametitle}{series=\bfseries}
\setbeamerfont{framesubtitle}{series=\mdseries}
\setbeamerfont{footline}{size=\scriptsize}
\setbeamerfont{block title}{series=\centering\bfseries}

%\setbeamercolor{frametitle}{fg=black}

% Code to get prettier boxes
 \setbeamertemplate{blocks}[rounded]


% Bullets in several levels
\setbeamertemplate{itemize item}{\textbullet}
\setbeamertemplate{itemize subitem}{\textemdash}
\setbeamertemplate{itemize subsubitem}{\ensuremath{\circ}}

\newenvironment{colorblock}[3][toctxt]{%
	\begingroup
	\setbeamercolor{block title}{fg=#1,bg=#2}
	\setbeamercolor{block body} {fg=#1,bg=#2}
	\begin{block}{#3}
	}{%
	\end{block}
	\endgroup
}

% Put the logo in each slide's top left area
\setbeamertemplate{headline}{\hspace{0.06\textwidth}\pgfuseimage{\@logo}}
% Put the logo in each slide's top right area
%\setbeamertemplate{headline}{\hspace{0.86\textwidth}\pgfuseimage{\@logo}}

% Define frame title and subtitle layout
\setbeamertemplate{frametitle}{%
  \vspace*{-3.5ex}
  \begin{beamercolorbox}[leftskip=2cm]{frametitle}%
    \usebeamerfont{frametitle}\insertframetitle\\
    \usebeamerfont{framesubtitle}\insertframesubtitle
  \end{beamercolorbox}
}

\def\@courseLabel{}
\def\@IDnumber{}

\newcommand{\course}[1]{\def\@courseLabel{#1}}
\newcommand{\IDnumber}[1]{\def\@IDnumber{#1}}

% Define the title page
\setbeamertemplate{title page}{%
  \vskip0pt plus 1filll%
  % Reposition the box in an inelegant way - but it works!
  \hspace{-12mm}
  \vspace{20mm}
  \begin{beamercolorbox}[wd=0.72\textwidth,sep=10pt,leftskip=8mm]{title}%
    {\usebeamerfont{title}\inserttitle}
  
    {\usebeamerfont{subtitle}\insertsubtitle}

    {\usebeamerfont{subtitle}\@courseLabel}
    
    {\usebeamerfont{author}\usebeamercolor[fg]{author}\textbf{\insertauthor} \ifdefempty{\@IDnumber}{}{(\@IDnumber)}}
  
    {\usebeamerfont{date}\usebeamercolor[fg]{date}\insertdate}
  \end{beamercolorbox}
}

% Define slide splitting dimensions (e.g. title and chapter slides)
\newcommand{\TikzSplitSlide}[1]{%
  \rule{0.56\paperwidth}{0pt}%
  \begin{tikzpicture}
    \clip (-0.1\paperwidth,-0.5\paperheight) -- 
          ( 0.5\paperwidth,-0.5\paperheight) -- 
          ( 0.5\paperwidth, 0.5\paperheight) -- 
          ( 0.1\paperwidth, 0.5\paperheight) -- cycle; 
    \node at (0.2\paperwidth,0) {%
      \includegraphics[height=\paperheight]{#1}%
    };
  \end{tikzpicture}
}

\newbool{splittitle}
\newcommand{\@TitleBackground}{}
\newcommand{\titlebackground}{\@ifstar{\SplitBackground}{\FullBackground}}
\newcommand{\FullBackground}[1]{\renewcommand{\@TitleBackground}{#1}}
\newcommand{\SplitBackground}[1]{%
	\booltrue{splittitle}%
	\renewcommand{\@TitleBackground}{#1}%
}

% Overwrite the maketitle command for the title page
\renewcommand{\maketitle}{
\begingroup
  \ifdefempty{\@TitleBackground}{}{% Skip if \@TitleBackground is empty
    \setbeamertemplate{background}{%
      \ifbool{splittitle}{%
        \TikzSplitSlide{\@TitleBackground}
      }{% Normal, full-screen background
        \includegraphics[height=\paperheight]{\@TitleBackground}
      }
    }
  }
  \begin{frame}%
  \titlepage%
  \end{frame}%
\endgroup
}

\makeatletter
    \newenvironment{withoutheadline}{
        \setbeamertemplate{headline}[default]
        \def\beamer@entrycode{\vspace*{-\headheight}}
    }{}
\makeatother


% Define chapter environment
\newenvironment{chapter}[3][]{% Args: image (optional), color, frame title
  \begingroup
  \themecolor{main}
  \setbeamertemplate{footline}{}
  \ifstrempty{#2}{
    \setbeamercolor{frametitle}{fg=toctxt}
    \setbeamercolor{normal text}{fg=toctxt,bg=maincolor}
  }{
    \setbeamercolor{frametitle}{fg=toctxt}
    \setbeamercolor{normal text}{fg=toctxt,bg=#2}
  }
  \ifstrempty{#1}{}{\setbeamertemplate{background}{\TikzSplitSlide{#1}}}
  \setbeamertemplate{frametitle}{%
    \vspace*{8ex}
    \begin{beamercolorbox}[wd=0.45\textwidth]{frametitle}
      \usebeamerfont{frametitle}\insertframetitle\\
      \usebeamerfont{framesubtitle}\insertframesubtitle
    \end{beamercolorbox}
  }
  \begin{frame}{#3}
  \hspace*{0.05\textwidth}%
  \minipage{0.35\textwidth}%
  \usebeamercolor[fg]{normal text}%
}{%
  \endminipage
  \end{frame}
  \endgroup
}

% Define sidepic environment
\newenvironment{sidepic}[2]{% Args: image, frame title
  \begingroup
  \setbeamertemplate{background}{%
  \hspace*{0.6\paperwidth}%
  \includegraphics[height=\paperheight]{#1}%
  }
  \setbeamertemplate{frametitle}{% Same as normal, but with right skip
    \vspace*{-3.5ex}
    \begin{beamercolorbox}[leftskip=2cm,rightskip=0.4\textwidth]{frametitle}%
      \usebeamerfont{frametitle}\insertframetitle\\
      \usebeamerfont{framesubtitle}\insertframesubtitle
    \end{beamercolorbox}
  }
  \begin{frame}{#2}
  \minipage{0.6\textwidth}%
}{%
  \endminipage
  \end{frame}
  \endgroup
}

% Define backmatter
\newcommand{\backmatter}[1][]{
\begingroup
  %\themecolor{main}
  \begin{frame}[c]
    \centering
    \begin{minipage}{\textwidth}
      \usebeamercolor[fg]{normal text}
      \centering
      \ifstrequal{#1}{notitle}{}{
        \huge \inserttitle
        \vspace{5mm}
      }
      \Large \textsl{\\Gracias por escucharnos}
    \end{minipage}
  \end{frame}
\endgroup
}

% Define sectioning and table of contents
\AtBeginSection[]
{
    \begingroup
    \themecolor{main}
   \begin{frame}[allowframebreaks]{Contenidos}
       \tableofcontents[currentsection]
    \end{frame}
   \endgroup
}

% Automate frame with allowframebreaks and create subsections
\newcommand{\automateframe}[2]{%
  \subsection{#1}
  \begin{frame}[allowframebreaks]{#1}
    #2
  \end{frame}
}


% style of section presented in the table of contents
\setbeamertemplate{section in toc}{$\blacktriangleright$~\inserttocsection}
\setbeamercolor{section in toc}{fg=toctxt, bg=toctxt}
%Hide subsection in toc
\setbeamertemplate{subsection in toc}{}

% automate subtitle of each frame
\makeatletter
    \pretocmd\beamer@checkframetitle{\framesubtitle{\thesection \, \secname}}
\makeatother

% avoid numbering of frames that are breaked into multiply slides
\setbeamertemplate{frametitle continuation}{}