% (The MIT License)
%
% Copyright (c) 2022 Yegor Bugayenko
%
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the 'Software'), to deal
% in the Software without restriction, including without limitation the rights
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
% copies of the Software, and to permit persons to whom the Software is
% furnished to do so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in all
% copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
% SOFTWARE.

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{ppt-slides}[2022/09/12 0.1.2 Slide Decks à la Power Point]

\RequirePackage{pgfopts}
\makeatletter
\pgfkeys{
  /ppt-slides/.is family,
  /ppt-slides,
  template/.estore in = \ppt@template,
  template/.default = 9x6,
  scheme/.estore in = \ppt@scheme,
  directory/.estore in = \ppt@directory,
  directory/.default = .,
  directory
}
\ProcessPgfOptions*
\makeatother

\makeatletter\ifdefined\ppt@template
  \input{\ppt@directory/ppt-templates/ppt-\ppt@template.tex}
\else
  \message{^^Jppt: No template is loaded, because 'template' option is not specified}
\fi\makeatother

\makeatletter\ifdefined\ppt@scheme
  \RequirePackage{xcolor}
  \input{\ppt@directory/ppt-schemes/ppt-\ppt@scheme.tex}
\else
	\message{^^Jppt: No color scheme is loaded, because 'scheme' option is not specified}
\fi\makeatother

\RequirePackage{ifthen} % for if/then/else in LaTeX
\RequirePackage{href-ul} % for nice hyperlinks
\RequirePackage{pagecolor} % to change page color
\RequirePackage{varwidth} % for pptWide
\RequirePackage{qrcode} % to print QR codes
\RequirePackage{tikz} % for drawings in TOC
\RequirePackage{tikzpagenodes} % for pptPin
\RequirePackage[inline]{enumitem} % for lists in crumbs
\RequirePackage{crumbs} % for crumbs

\newcommand\pptMiddle[1]{%
  \vspace*{\fill}#1\vspace*{\fill}%
}

\newcommand\pptBanner[2][green]{%
  \par
  {\setlength{\fboxsep}{6pt}\colorbox{#1}{\color{\thepagecolor}\large #2}}
  \par
}

\newcommand\pptChapter[2][]{%
  \crumbection[#1]{#2}%
  \pptMiddle{%
    {\large\ttfamily Chapter \#\the\value{section}:}%
    \newline%
    \rotatebox{1}{\pptBanner[blue]{\LARGE#2}}%
  }%
}

\newcommand\pptSection[2][]{%
  \subcrumbection[#1]{#2}%
  \pptHeader{#2}%
}

\newcommand\pptHeader[1]{%
  \vspace*{-0.75in}\hspace*{-0.5in}%
  \rotatebox{1}{\pptBanner[orange]{\Large #1}}\vspace*{0.15in}%
  \par%
}

\newcommand\pptTitle[2]{%
  {\pptBanner{\Huge #1}}%
  {\pptBanner{\large #2}}%
}

\newcommand*\pptTOC{}
\newcommand\pptToc[1][]{%
  \renewcommand*\pptTOC{}%
  \print{%
    \def\param{#1}%
    \ifx\param\empty\else%
      \pptBanner{#1}%
    \fi%
    \tikz{%
      \node (z) {%
        \begin{varwidth}{\textwidth}%
          \ifx\pptTOC\empty\else%
            \begin{itemize}[label={}]
              \pptTOC
            \end{itemize}
          \fi%
        \end{varwidth}%
      };%
      \path [draw=blue,line width=4pt] (z.north west) -- (z.south west);%
    }%
  }%
  \begin{@empty}%
    \renewcommand\crumb[2]{%
      \ifx\pptTOC\empty\else\ifdefined\click\click[0]\fi\fi
      \gappto\pptTOC{\item ##2}
    }
    \crumbs
  \end{@empty}%
  \ifdefined\flush\flush[1]\fi%
}

\RequirePackage{tabularx}
\newcommand\pptQuote[3]{%
  \begin{tabularx}{\columnwidth}{c>{\raggedright\arraybackslash}X}%
    \raisebox{\dimexpr-\height+\ht\strutbox}{\pptPic{0.25}{#1}}%
    &%
    ``#2''%
    \def\param{#3}%
    \ifx\param\empty\else%
      \newline\newline%
      \small%
      --- #3
    \fi%
    \\%
  \end{tabularx}%
}

\newcommand\pptPic[2]{%
  {%
    \setlength{\fboxsep}{0pt}%
    \setlength{\fboxrule}{1pt}%
    \fcolorbox{gray}{white}{%
      \includegraphics[width=#1\columnwidth]{#2}%
    }%
  }%
}

\newcommand\pptPin[2][right]{%
  \begin{tikzpicture}[remember picture,overlay]%
    \ifthenelse{\equal{#1}{left}}
    {
      \node[anchor=north east] at ($(current page text area.north west) - (24pt, 18pt)$) {%
        \begin{minipage}{0.25\textwidth}%
          \raggedleft #2
        \end{minipage}%
      };
    }
    {
      \node[anchor=north east] at (current page text area.north east) {%
        \begin{minipage}{0.25\textwidth}%
          \raggedright #2
        \end{minipage}%
      };%
    }
  \end{tikzpicture}%
}

\newcommand\pptThought[1]{%
  \pptMiddle{%
    \tikz{%
      \node [inner sep=18pt] (z) {%
        \begin{varwidth}{0.8\textwidth}
          \Large
          #1
        \end{varwidth}
      };
      \path [draw=green,line width=8pt] (z.north west) -- (z.south west);
    }
  }
}

\newcommand\pptSnippet[2][\small]{%
  \begin{samepage}%
    #1\verbatiminput{#2}%
  \end{samepage}%
}

\newcommand\pptQR[2][2in]{%
  \tikz{%
    \node[draw=white]{%
      \href{#2}{%
        \qrcode[height=#1]{#2}%
      }%
    }%
  }
}

\RequirePackage{seqsplit}
\newcommand\pptPinQR[2][right]{%
  \pptPin[#1]{%
    \begin{minipage}{1.4in}%
      \raggedleft%
      \pptQR[1in]{#2}%
      \\[3pt]
      \ttfamily\fontsize{8pt}{10pt}\selectfont\seqsplit{#2} \href{#2}{$\rightarrow$}
      \\
    \end{minipage}%
  }%
}

\newenvironment{pptWideOne}
  {\begin{adjustwidth}{-2in}{-1in}}
  {\end{adjustwidth}}
\newenvironment{pptWide}[1]
  {\begin{pptWideOne}\begin{multicols}{#1}}
  {\end{multicols}\end{pptWideOne}}

\endinput
