% !TEX root = ../../Main.tex

% ==== Base Case ====
% Define a counter to track nesting level
\newcounter{coloredBoxLevel}

% Parameters:
\makeatletter
\define@key{coloredBox}{icon}{\def\coloredBoxIcon{#1}}
\define@key{coloredBox}{icon.prefix}{\def\coloredBoxIconPrefix{#1}}
\define@key{coloredBox}{icon.fontsize}{\def\coloredBoxIconSize{#1}}
\define@key{coloredBox}{icon.offset.x}{\def\coloredBoxIconOffsetX{#1}}
\define@key{coloredBox}{icon.offset.y}{\def\coloredBoxIconOffsetY{#1}}
\define@key{coloredBox}{icon.color}{\def\coloredBoxColor{#1}}
\define@key{coloredBox}{background.color}{\def\coloredBoxBackground{#1}}
\makeatother
% Define the environment
\NewEnviron{ColoredBox}[1][
   icon={\faInfoCircle},
   icon.color={blue},
   icon.prefix={},
   icon.fontsize={28pt},
   icon.offset.x={0pt},
   icon.offset.y={0pt},
   background.color={blue}
]{
   % Start of the environment
   %% Setting the keys
   \setkeys{coloredBox}{#1}

   %% Set background opacity

   % Increment the nesting level counter
   \stepcounter{coloredBoxLevel}

   % Set opacity based on the nesting level using a formula
   \FPeval{\backgroundOpacityFloat}{0.05 + 0.075 * \arabic{coloredBoxLevel}}

   % Convert the opacity to a percentage integer
   \FPeval{\backgroundOpacity}{round(\backgroundOpacityFloat * 100, 0)}

   % Always have the icon opacity 15% higher than the background
   \FPeval{\iconOpacity}{\backgroundOpacity + 15}

   %% === Content ===
   \filbreak
   \vspace*{0.5\baselineskip}

   \noindent
   \begin{minipage}{\linewidth}
   \begin{mdframed}[
      backgroundcolor={\coloredBoxBackground!\backgroundOpacity},
      hidealllines=true,
      skipabove=0.7\baselineskip,
      skipbelow=0.7\baselineskip,
      splitbottomskip=2pt,
      splittopskip=4pt,
      roundcorner=5pt
   ]
   \tikzset{every picture/.style={opacity=1}}
   \begin{tikzpicture}
      % Icon area
      \node[
         % Middle Top
         anchor=north,
         inner sep=0pt,
         outer sep=0pt,
         align=center,
         text width=1cm,
      ] at (0,0 |- current bounding box.north) {
         \coloredBoxIconPrefix
         \fontsize{\coloredBoxIconSize}{\coloredBoxIconSize}
         \selectfont
         \hspace*{\coloredBoxIconOffsetX-1.75cm}
         \raisebox{\coloredBoxIconOffsetY}
            {\color{\coloredBoxColor!\iconOpacity} \coloredBoxIcon}
      };

      % Content area
      \node[
         anchor=north west,
         inner sep=10pt,
         outer sep=0pt,
         text width=\linewidth-0.7cm,
         align=justify
      ] at (-0.5cm,0 |- current bounding box.north) {
         \BODY
      };
   \end{tikzpicture}
   \end{mdframed}
   \end{minipage}
   %% === /Content ===

   % Reduce the nesting level counter
   \addtocounter{coloredBoxLevel}{-1}
}
% ==== /Base Case ====

% ==== Info Box ====
% Derived from `ColoredBox`
\NewEnviron{InfoBox}[1][
   icon={\faInfoCircle},
   icon.color={blue},
   icon.prefix={},
   icon.fontsize={24pt},
   icon.offset.x={0pt},
   icon.offset.y={0pt},
   background.color={blue}
]{
   \let\iBODY\BODY
   \begin{ColoredBox}[#1]
      \iBODY
   \end{ColoredBox}
}
% ==== /Info Box ====

% ==== Warning Box ====
% Derived from `ColoredBox`
\NewEnviron{WarningBox}[1][
   icon={\faExclamationTriangle},
   icon.color={red},
   icon.prefix={},
   icon.fontsize={24pt},
   icon.offset.x={0pt},
   icon.offset.y={0pt},
   background.color={red}
]{
   \let\wBODY\BODY
   \begin{ColoredBox}[#1]
      \wBODY
   \end{ColoredBox}
}
% ==== /Warning Box ====

% ==== Success Box ====
% Derived from `ColoredBox`
\NewEnviron{SuccessBox}[1][
   icon={\faCheckCircle},
   icon.color={green},
   icon.prefix={},
   icon.fontsize={24pt},
   icon.offset.x={0pt},
   icon.offset.y={2pt},
   background.color={green}
]{
   \let\sBODY\BODY
   \begin{ColoredBox}[#1]
      \sBODY
   \end{ColoredBox}
}
% ==== /Success Box ====

% ==== Important Box ====
% Derived from `ColoredBox`
\NewEnviron{ImportantBox}[1][
   icon={\faExclamationCircle},
   icon.color={orange},
   icon.prefix={},
   icon.fontsize={24pt},
   icon.offset.x={0pt},
   icon.offset.y={0pt},
   background.color={orange}
]{
   \let\impBODY\BODY
   \begin{ColoredBox}[#1]
      \impBODY
   \end{ColoredBox}
}

% ==== Simpler Custom Boxes ====
% Environment `CustomBox`
% Usage:
%   \begin{CustomBox}{<icon>}{<color>}
%     ...
%   \end{CustomBox}
% Parameters:
\NewEnviron{CustomBox}[2]{
   \let\cBODY\BODY
   \begin{ColoredBox}[
      icon={#1},
      icon.color={#2},
      icon.prefix={},
      icon.fontsize={24pt},
      icon.offset.x={0pt},
      icon.offset.y={0pt},
      background.color={#2}
   ]
      \cBODY
   \end{ColoredBox}
}
% ==== /Simpler Custom Boxes ====