\usepackage{tikz}

\mode<presentation>

\DeclareOptionBeamer{logo}{\def\beamer@ukaea@logo{#1}}
\ExecuteOptionsBeamer{logo=ukaea_logo.png}
\DeclareOptionBeamer{extratitletext}{\def\beamer@ukaea@extratitletext{#1}}
\ExecuteOptionsBeamer{extratitletext=}
\ProcessOptionsBeamer

\newcommand{\ukaeatriangle}[1] {
  \begin{tikzpicture}
    \useasboundingbox (0,0) rectangle(\the\paperwidth,0.2\paperwidth);
    \node (t1) at (\paperwidth, 0.2\paperwidth) {};
    \node (t2) at (0.8\paperwidth, 0.2\paperwidth) {};
    \node (t3) at (\paperwidth, 0.) {};
    \fill[fill=#1] (t1.center)--(t2.center)--(t3.center);
    \if \beamer@ukaea@logo\@empty
    \else
    \node (logo) at (0.9415\paperwidth, 0.1415\paperwidth) {
      \includegraphics[height=0.073\paperwidth]{\beamer@ukaea@logo}
    };
    \fi
  \end{tikzpicture}
}


\setbeamertemplate{title page}
{%
  \newdimen\title@graphic@height
  \begin{minipage}[t]{\paperwidth}
    \hspace*{-1cm}\inserttitlegraphic{}
  \end{minipage}

  \settoheight{\title@graphic@height}{\inserttitlegraphic{}}
  \vspace*{-\title@graphic@height minus 0.004\paperheight}
  \hspace*{-1cm}%
  \begin{minipage}{\paperwidth}
  \usebeamercolor[bg]{headline}
    \ukaeatriangle{bg}\par
  \end{minipage}

 \vspace*{-2.59cm}
 \hspace*{-1.085cm}
 \begin{tikzpicture}
    \useasboundingbox (0,0) rectangle(\the\paperwidth,\the\paperheight);
    
    \node (q1) at (0,0.54\paperheight) {};
    \node (q2) at (\the\paperwidth,0.32\paperheight) {};
    \node (q3) at (0,0) {};
    \node (q4) at (\the\paperwidth,0) {};
    \fill[fill=bg] (q1.center)--(q2.center)--(q4.center)--(q3.center);
    \node[anchor=north west,text width=0.9\paperwidth] (titletext) at (0.4,0.44\paperheight) {
      \usebeamerfont{institute}{\usebeamercolor[fg]{institute}\insertinstitute}\par
      \smallskip
      \usebeamerfont{title}{\usebeamercolor[fg]{title}\inserttitle}\par
      \usebeamerfont{subtitle}{\usebeamercolor[fg]{subtitle}\insertsubtitle}\par
      
      \bigskip
%      \begin{minipage}{0.6\textwidth}
        \usebeamercolor[fg]{date}{\usebeamerfont{date}\insertdate}\par
        \usebeamercolor[fg]{author}{\usebeamerfont{author}\insertauthor}\par
%      \end{minipage}
    };
    \node[anchor=south east,text width=0.45\paperwidth] (extratext) at
    (1.28\paperwidth, 0.15) {
      \usebeamercolor[fg]{title}
      \usebeamerfont{main text}
      \beamer@ukaea@extratitletext
    };
  \end{tikzpicture}
 \addtocounter{framenumber}{-1}
}


\setbeamertemplate{headline}
{%\vspace{-0.07cm}\hspace{0.795\paperwidth}
  \ukaeatriangle{bg}
 \vspace{-0.2\paperwidth}
}


\setbeamertemplate{footline}
{
  \leavevmode
  \hbox{
    \begin{beamercolorbox}[wd=1.0cm,ht=1.5cm,dp=0.3cm,right, rightskip=0.1cm]{pagenumber}%
      \textbf{\large{\usebeamercolor[fg]{pagenumber}\insertframenumber{}}}
    \end{beamercolorbox}
    \usebeamercolor[fg]{normal text}{\textbf{\Large |} \raisebox{0.5ex}{\insertshorttitle}
    }
  }
}

\mode<all>
\endinput
