\RequirePackage{tikz, datetime, fontspec, xstring}
\usetikzlibrary{shadows.blur, patterns, shapes.misc, fadings, fit}
\newfontfamily{\structurefont}[
	Path=fonts/,
	UprightFont = *-Medium,
	BoldFont = *-Black
]
{Roboto}
\setmainfont[
	Path=fonts/,
	UprightFont = *-Regular,
	BoldFont = *-Bold
]
{Roboto}

\newcommand{\advisor}{\@dblarg\beamer@advisor}
\long\def\beamer@advisor[#1]#2{%
  \def\insertadvisor{#2}  %
  \def\insertshortadvisor{#1}%
}
\renewenvironment{theorem}{\begin{block}{Theorem}}{\end{block}} 

\def\drawprogressbar#1{%
    \begingroup
    \tikzset{sectionnode/.style={
            circle,
            ultra thick,
            fill=white,
            text centered,
            inner sep=0,
            minimum size=2ex,
            font=\scriptsize\bfseries
        }
    }
    \makeatletter
    \def\beamer@endinputifotherversion##1{}
    \def\headcommand##1{##1}
    \def\beamer@framepages##1##2{}
    \def\beamer@documentpages##1{
        \xdef\totalpages{##1}
    }
    \def\sectionentry##1##2##3##4##5{}
    \def\beamer@subsectionentry##1##2##3##4##5{}
    \def\beamer@subsubsectionentry##1##2##3##4##5##6{}
    \def\slideentry##1##2##3##4##5##6{
        \xdef\currentsection{##1}
    }
    \def\beamer@sectionpages##1##2{
        \ifnum##1>1
            \ifnum\c@page<##1
                \draw[gray, ultra thick](##1,1)--(##2+1,1);
                \node[sectionnode, draw=gray, text=gray] at (##1,1) {\currentsection};
            \else
                \ifnum\c@page>##2
                    \draw[color\currentsection, ultra thick] (##1,1)--(##2+1,1);
                \else
                    \draw[color\currentsection, ultra thick] (##1,1)--(\value{page},1)
                        (##1,1)--+(4mm,-0.5)--(#1,0.5);
                    \draw[gray, ultra thick](\value{page},1)--(##2+1,1);
                \fi
                \node[sectionnode, draw=color\currentsection, text=color\currentsection] at (##1,1) {\currentsection};
            \fi
        \fi
    }
    \def\beamer@partpages##1##2{}
    \def\beamer@subsectionpages##1##2{}
    \IfFileExists{\jobname.nav}{\input{\jobname.nav}}{}
    \makeatother
    \endgroup
}

\ProcessOptionsBeamer

\colorlet{color1}{MidnightBlue}
\colorlet{color2}{WildStrawberry}
\colorlet{color3}{SeaGreen!50!black}
\colorlet{color4}{BurntOrange}
\colorlet{color5}{Fuchsia}
\colorlet{color6}{BrickRed}
\colorlet{color7}{OliveGreen}
\colorlet{color8}{Aquamarine}
\colorlet{color9}{CadetBlue}

\colorlet{themecolor}{darkgray}
\colorlet{themecolorlight}{themecolor!50}
\colorlet{themecolorultralight}{themecolor!25}

\mode<presentation>

%\AtBeginSection[]{\sectionframe}

\usdate

\tikzset{
  rectangle with rounded corners north west/.initial=4pt,
  rectangle with rounded corners south west/.initial=4pt,
  rectangle with rounded corners north east/.initial=4pt,
  rectangle with rounded corners south east/.initial=4pt,
}

\titlegraphic{}

\makeatletter
\patchcmd{\beamer@sectionintoc}
  {\vfill}
  {}
  {}
  {}
\makeatother

\makeatletter
\pgfdeclareshape{rectangle with rounded corners}{
	\inheritsavedanchors[from=rectangle] % this is nearly a rectangle
	\inheritanchorborder[from=rectangle]
	\inheritanchor[from=rectangle]{center}
	\inheritanchor[from=rectangle]{north}
	\inheritanchor[from=rectangle]{south}
	\inheritanchor[from=rectangle]{west}
	\inheritanchor[from=rectangle]{east}
	\inheritanchor[from=rectangle]{north east}
	\inheritanchor[from=rectangle]{south east}
	\inheritanchor[from=rectangle]{north west}
	\inheritanchor[from=rectangle]{south west}
	\backgroundpath{% this is new 
		\southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
		\northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
    \pgfkeysgetvalue{/tikz/rectangle with rounded corners north west}{\pgf@rectc}
    \pgfsetcornersarced{\pgfpoint{\pgf@rectc}{\pgf@rectc}}
    \pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@yb}}
    \pgfkeysgetvalue{/tikz/rectangle with rounded corners north east}{\pgf@rectc}
    \pgfsetcornersarced{\pgfpoint{\pgf@rectc}{\pgf@rectc}}
    \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yb}}
    \pgfkeysgetvalue{/tikz/rectangle with rounded corners south east}{\pgf@rectc}
    \pgfsetcornersarced{\pgfpoint{\pgf@rectc}{\pgf@rectc}}
    \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya}}
    \pgfkeysgetvalue{/tikz/rectangle with rounded corners south west}{\pgf@rectc}
    \pgfsetcornersarced{\pgfpoint{\pgf@rectc}{\pgf@rectc}}
    \pgfpathclose
	}%
}%
\makeatother

\tikzset{framepicture/.style={remember picture, overlay, shift={(current page.south west)},
	every node/.style={inner sep=0, outer sep=0, font=\vphantom{Ag}}}}

\AtBeginSection[]{
	\colorlet{themecolor}{color\thesection}
	\colorlet{themecolorlight}{themecolor!20}
    \colorlet{themecolorultralight}{themecolor!10}
	\bgroup
	\setbeamertemplate{background}{}
	\frame[noframenumbering, c]{\sectionpage}
	\egroup
}

\setbeamercolor*{title page header}{fg=white}
\setbeamercolor*{title}{fg=white, bg=black}
\setbeamercolor*{subtitle}{fg=themecolor, bg=white}
\setbeamercolor*{author}{fg=themecolor, bg=darkgray}
\setbeamercolor*{date}{fg=darkgray}
\setbeamercolor*{item}{fg=themecolor}
\setbeamercolor*{frametitle}{fg=themecolor}
\setbeamercolor*{block title}{fg=white, bg=themecolor}
\setbeamercolor*{block title example}{fg=white, bg=themecolor}
\setbeamercolor*{block body}{fg=black, bg=themecolorultralight}
\setbeamercolor*{block body example}{fg=black, bg=themecolorultralight}
\setbeamercolor*{section in toc}{fg=black}
\setbeamercolor*{section page}{fg=white}
\setbeamercolor*{frame number}{fg=gray}
\setbeamercolor*{alerted text}{fg=themecolor}

\usefonttheme{default}
\setbeamerfont{structure}{family=\structurefont}
\setbeamerfont{section page}{size=\LARGE}
\setbeamerfont{title}{series=\bfseries, size=\LARGE}
\setbeamerfont{subtitle}{series=\bfseries, size=\large}
\setbeamerfont{author}{parent=structure, series=\bfseries}
\setbeamerfont{institute}{parent=structure, size=\footnotesize}
\setbeamerfont{date}{series=\bfseries, size=\small}
\setbeamerfont{frametitle}{size=\Large}
\setbeamerfont{block title}{size=\normalsize}
\setbeamerfont{below}{size=\small}
\setbeamerfont{footer}{size=\footnotesize}
\setbeamerfont{frame number}{size=\small}
\setbeamerfont{section in toc}{size=\large}

\newlength{\margins}
\setlength{\margins}{0.05\paperwidth}

\setbeamersize{text margin left=2\margins,text margin right=2\margins} 

\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{sidebar right}{}
\setbeamertemplate{items}[circle]
\setbeamertemplate{itemize item}{\tikz{\draw[color=themecolor, very thick] circle (3pt);}}
\setbeamertemplate{itemize subitem}{\tikz{\fill[color=themecolor, very thick] circle (3pt);}}
\setbeamertemplate{headline}{\vspace*{6mm}}
\setbeamertemplate{footline}{\vspace*{8mm}}
%\setbeamertemplate{sections/subsections in toc}[circle]

\setbeamertemplate{block begin}{
  \par\vskip\medskipamount%
  \IfStrEq{\insertblocktitle}{}{}{
      \begin{beamercolorbox}[colsep*=.75ex]{block title}
        \usebeamerfont*{block title}\insertblocktitle%
      \end{beamercolorbox}%
  }
  {\parskip0pt\par}%
  \ifbeamercolorempty[bg]{block title}
  {}
  {\ifbeamercolorempty[bg]{block body}{}{\nointerlineskip\vskip-0.5pt}}%
  \usebeamerfont{block body}%
    \begin{beamercolorbox}[colsep*=.75ex,vmode]{block body}%
        \ifbeamercolorempty[bg]{block body}{\vskip-.25ex}{\vskip-.75ex}\vbox{}%
}

\newenvironment{xplainframe}{\bgroup\setbeamertemplate{background}{}\begin{frame}[plain]}{\end{frame}\egroup}

\setbeamertemplate{background}{
	\begin{tikzpicture}[framepicture]
	\ifnum\thepage>1\relax% Not the title page
		\fill[color=themecolor] (\margins,\paperheight-12mm) rectangle (\margins+1mm,\paperheight-4mm)
			(\margins,4mm) rectangle (\margins+1mm,12mm)
			(\paperwidth-24mm,4mm) rectangle (\paperwidth-23mm,12mm);
		\node[anchor=south west] (LOGO) at (\margins+4mm,4mm) {\insertlogo};
	\else% Title page
		\node at (\paperwidth/2,\paperheight/2) {\pgfbox[center,center]{\inserttitlegraphic}};
		\fill[color=themecolor, opacity=0.8] (0,0.4\paperheight) rectangle (\paperwidth,\paperheight);
		\fill[color=white, opacity=0.8] (0,0) rectangle (\paperwidth,0.4\paperheight);
  	\fi
  \end{tikzpicture}
}
\setbeamertemplate{title page}{
    \begin{tikzpicture}[framepicture, every node/.append style={anchor=north west}]
		\node[anchor=south west, text width=\paperwidth-4\margins] (T)
	        at (2\margins, 0.4\paperheight+5mm) {
			\usebeamerfont{title}\usebeamercolor[fg]{title}\inserttitle%
		};
		\node[anchor=south west, fill=white, rounded rectangle, inner sep=0.5em]
		    at ($(T.north west) + (1em, 1em)$) {
		    \usebeamerfont{subtitle}\usebeamercolor[fg]{subtitle}\insertsubtitle
		};
        \node[anchor=south west] (G) at (2\margins, 5mm) {\insertlogo};
		\draw[themecolor, thick] ($(G.east) + (2ex,-4mm)$) -- coordinate (L1) ++(0,8mm);
		\node[right=2ex of L1, anchor=west, text width=.25\paperwidth] {
			\baselineskip=8pt%
			\usebeamerfont{institute}\usebeamercolor[fg]{author}\insertinstitute%
	    };
	    \draw[themecolor, thick] (0.5\paperwidth,0.4\paperheight-5mm) -- ++ (0,-14mm);
	    \node[anchor=east] at (0.5\paperwidth-2ex,0.4\paperheight-9mm) {
	        \usebeamerfont{structure}\usebeamercolor[fg]{author}\footnotesize Ph.D. Candidate
	    };
    	\node[anchor=west, text width=.5\paperwidth-2\margins]
    	    at (0.5\paperwidth+2ex,0.4\paperheight-9mm) {
			\usebeamerfont{author}\usebeamercolor[fg]{author}\insertauthor%
		};
		\node[anchor=east] at (0.5\paperwidth-2ex,0.4\paperheight-15mm) {
	        \usebeamerfont{structure}\usebeamercolor[fg]{author}\footnotesize Advisor
	    };
    	\node[anchor=west, text width=.5\paperwidth-2\margins]
    	    at (0.5\paperwidth+2ex,0.4\paperheight-15mm) {
			\usebeamerfont{structure}\usebeamercolor[fg]{author}\small\insertadvisor%
		};
		\node[anchor=south east] at (\paperwidth-2\margins, 5mm) {
			\usebeamerfont{date}\usebeamercolor[fg]{date}\insertdate%
	    };
    \end{tikzpicture}
}

\setbeamertemplate{section in toc}{
	\begin{tikzpicture}%[overlay]
	\useasboundingbox (0,-12pt) rectangle (\textwidth,12pt);
	\ifodd\inserttocsectionnumber\relax%
    	\node[fill=color\inserttocsectionnumber,
	    	regular polygon,regular polygon sides=6,
	    	minimum size=50pt, text=white] (C) at (0.5\textwidth-22pt,0)
    		{\Huge\inserttocsectionnumber};
		\node[left=1ex of C, text=color\inserttocsectionnumber, text width=0.5\textwidth-45pt, align=right]
			{\inserttocsection};
	\else
    	\node[fill=color\inserttocsectionnumber,
	    	regular polygon,regular polygon sides=6,
	    	minimum size=50pt, text=white] (C) at (0.5\textwidth+22pt,0)
    		{\Huge\inserttocsectionnumber};
		\node[right=1ex of C, text=color\inserttocsectionnumber, text width=0.5\textwidth-45pt] {\inserttocsection};
	\fi
	\end{tikzpicture}
}

\setbeamertemplate{frametitle}{
	\begin{tikzpicture}[framepicture]
   		\node[text width=\paperwidth - \margins, baseline, anchor=west] at (\margins+4mm, \paperheight-8.5mm) {
			\usebeamercolor[fg]{frametitle}\strut\insertframetitle
		};
		\node[text width=30mm, anchor=west] at (\paperwidth-20mm, 8mm) {
			\usebeamerfont{frame number}\usebeamercolor[fg]{frame number} \insertframenumber\ / \inserttotalframenumber
		};
		\pgfmathsetmacro\progresswidth{%
		    (0.5\paperwidth-\margins-28mm-1ex)/\inserttotalframenumber%
		}
		\pgfmathsetmacro\bottomlineend{%
		    (\paperwidth-\margins+\progresswidth-52mm-1ex)/\progresswidth
		}
		\begin{scope}[shift={(\margins-\progresswidth+28mm,0)},x=\progresswidth,y=8mm]
		    \drawprogressbar{\bottomlineend}
		\end{scope}
		\node[anchor=west, text=color\thesection, text width=0.5\paperwidth-20mm-4ex] at (0.5\paperwidth+2ex,8mm) {\baselineskip=0pt\usebeamerfont{frame number}\insertsection\par};
	\end{tikzpicture}
}

\setbeamertemplate{section page}{
    \vfill\vfill
    \tableofcontents[currentsection]
}

\mode<all>