%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% This is the TikZ-shields package
% A package to draw badges in the style of shields.io with tikz
% Maintained by thomas-saigre
%
% Project repository and bug tracker:
% https://github.com/thomas-saigre/tikz-shields
%
% Released under the GNU GENERAL PUBLIC LICENSE v3
% See https://github.com/thomas-saigre/tikz-shields/blob/main/LICENSE
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ProvidesPackage{tikz-shields}[2025/05/12 version v0.1 Drawing badges with TikZ]

\RequirePackage{tikz}
\RequirePackage{fontawesome}
\RequirePackage{hyperref}
\RequirePackage[most]{tcolorbox}
\RequirePackage{xkeyval}
\RequirePackage{ifthen}


\DeclareOptionX{font}[\sf]{\def\shield@default@font{#1}}
\DeclareOptionX{labelColor}[default-left]{\def\shield@default@labelColor{#1}}
\DeclareOptionX{color}[default-blue]{\def\shield@default@color{#1}}
\DeclareOptionX{colorLeft}[white]{\def\shield@default@colorLeft{#1}}
\DeclareOptionX{colorRight}[white]{\def\shield@default@colorRight{#1}}
\DeclareOptionX{logoColor}[white]{\def\shield@default@logoColor{#1}}
\DeclareOptionX{style}[flat]{%
    \def\shield@tmp@style{#1}%
    \def\shield@default@style{wrong}
    \def\shield@default@error{wrong}
    \@for\shield@opt:=flat,flat-square,plastic,for-the-badge,social\do{%
        \ifx\shield@tmp@style\shield@opt
            \def\shield@default@style{#1}%
        \fi
    }%
    \ifx\shield@default@style\shield@default@error
        \PackageError{tikz-shields}{Invalid style option '#1'}{Valid options are: flat, flatsquare, plastic, for-the-badge, social.}
    \fi
}

\definecolor{default-blue}{HTML}{1283c4}
\definecolor{default-left}{HTML}{5e5e5e}
\definecolor{default-grey}{HTML}{565656}
\definecolor{default-green}{HTML}{77d810}
\definecolor{default-social-frame}{HTML}{d5d5d5}
\definecolor{default-social-top}{HTML}{fcfcfc}
\definecolor{default-social-bottom}{HTML}{e5e5e5}

\ExecuteOptionsX{font}
\ExecuteOptionsX{labelColor}
\ExecuteOptionsX{color}
\ExecuteOptionsX{colorLeft}
\ExecuteOptionsX{colorRight}
\ExecuteOptionsX{logoColor}
\ExecuteOptionsX{style}


\define@key{badge}{labelColor}{\def\badge@labelColor{#1}}
\define@key{badge}{color}{\def\badge@color{#1}}
\define@key{badge}{colorLeft}{\def\badge@colorLeft{#1}}
\define@key{badge}{colorRight}{\def\badge@colorRight{#1}}
\define@key{badge}{logo}{\def\badge@logo{#1}}
\define@key{badge}{logoColor}{\def\badge@logoColor{#1}}
\define@key{badge}{link}{\def\badge@link{#1}}
\define@boolkey{badge}{showlink}{\def\badge@showlink{#1}}
\define@choicekey{badge}{style}{flat,flat-square,plastic,for-the-badge,social}{\def\badge@style{#1}}
\define@key{badge}{baseline}{\def\badge@baseline{#1}}

\ProcessOptionsX



\newtcbox{\socialleft}{
  enhanced,
  nobeforeafter,
  tcbox raise base,
  boxrule=1pt,
  top=0pt, left=0pt, right=0pt, bottom=0pt,
  colframe=default-social-frame,
  coltext=black,
  interior style={top color=default-social-top, bottom color=default-social-bottom},
  overlay={}
}

\newtcbox{\socialright}{
  enhanced,
  nobeforeafter,
  tcbox raise base,
  boxrule=0pt,
  top=0pt, left=0pt, right=0pt, bottom=0pt,
  colframe=default-social-frame,
  coltext=black,
  interior style={top color=default-social-top, bottom color=default-social-bottom},
  overlay unbroken and first={
    \shade[top color=default-social-top, bottom color=default-social-bottom]
      ([xshift=0pt, yshift=3pt]frame.west) --
      ([xshift=-3pt,yshift=0pt]frame.west) --
      ([xshift=0pt, yshift=-3pt]frame.west) -- cycle;
    \draw[color=default-social-frame, line width=1pt]
        ([xshift=0pt, yshift=3pt]frame.west) --
        ([xshift=-3pt,yshift=0pt]frame.west) --
        ([xshift=0pt, yshift=-3pt]frame.west) [rounded corners] --
        (frame.south west) --
        (frame.south east) --
        (frame.north east) --
        (frame.north west) [sharp corners] --
        cycle;
    },
}


% Creation of a text with dropped shadow
% #1 Color of background
% #2 Color of the text
% #3 Text
\newcommand{\droptextshadow}[3]{%
    \tikz[baseline,outer sep=0pt, inner sep=0pt]{
    % Dropped Shadow
    \node[#1!40!black] at (0,-0.1ex) {#3};
    % Text
    \node[#2] at (0,0) {#3};
    }%
}

% Creation of a text without dropped shadow
% #1 Color of the text
% #2 Text
\newcommand{\droptextnoshadow}[2]{%
    \tikz[baseline,outer sep=0pt, inner sep=0pt]{
    % Text
    \node[#1] at (0,0) {#2};
    }%
}



% Draw a badge
% #1 Default options
% #2 Left text
% #3 Right text
\newcommand{\drawBadgeAux}[3][]{%
    \def\badge@labelColor{\shield@default@labelColor}%
    \def\badge@color{\shield@default@color}%
    \def\badge@colorLeft{\shield@default@colorLeft}%
    \def\badge@colorRight{\shield@default@colorRight}%
    \def\badge@logo{}%
    \def\badge@logoColor{\shield@default@logoColor}%
    \def\badge@style{\shield@default@style}%
    \def\badge@baseline{0mm}%
    %
    \setkeys{badge}{#1}%
    %
    \ifthenelse{\equal{\badge@style}{social}}{%
        % social badge : special handling
        \shield@default@font%
        \mbox{\socialleft{#2\vphantom{#3}}\hspace{1ex}\socialright{#3\vphantom{#2}}}%
    }{%
        \def\opt@rounded{rounded corners}%
        \def\opt@margin{0pt}%
        \def\opt@gap{5pt}%
        \def\opt@bottomColor{!60!black}%
        \def\opt@topColor{}%
        % \def\opt@baseline{2mm}%
        \ifthenelse{\equal{\badge@style}{flat}}{%
        }{%
        }%
        \ifthenelse{\equal{\badge@style}{flat-square}}{%
            \def\opt@rounded{sharp corners}%
            \def\opt@bottomColor{}%
        }{%
        }%
        %
        \ifthenelse{\equal{\badge@style}{plastic}}{%
            \def\opt@topColor{!70!white}%
        }{%
        }%
        %
        \ifthenelse{\equal{\badge@style}{for-the-badge}}{%
            \def\opt@rounded{sharp corners}%
            \def\opt@margin{3pt}%
            \def\opt@gap{10pt}%
            \def\opt@bottomColor{}%
            % \def\opt@baseline{3mm}%
        }{%
        }%
        %%
        \mbox{%
        \tcbsidebyside[
            bicolor, sidebyside,
            sidebyside adapt=both,
            nobeforeafter, nobeforeafter,
            sidebyside gap=\opt@gap,
            top=\opt@margin, left=\opt@margin, right=\opt@margin, bottom=\opt@margin,
            boxrule=0pt, \opt@rounded,
            baseline=\badge@baseline,
            interior style={top color=\badge@labelColor\opt@topColor, bottom color=\badge@labelColor\opt@bottomColor},
            segmentation style={top color=\badge@color\opt@topColor, bottom color=\badge@color\opt@bottomColor},
        ]{  % Left bloc
            \shield@default@font%
            \ifx\badge@logo\@empty%
            \else%
                \droptextnoshadow{\badge@logoColor}{\badge@logo}~%
            \fi%
            \ifx#2\@empty%
            \else%
                \droptextshadow{\badge@labelColor}{\badge@colorLeft}{#2\vphantom{#3}}%
                %
            \fi%
        }{  % Right bloc
            \shield@default@font
            \droptextshadow{\badge@color}{\badge@colorRight}{#3\vphantom{#2}}%
            %
        }%
        }
        \hspace{-1.5ex}%
    }
}


\newcommand{\drawBadge}[3][]{%
    %
    \def\badge@link{}%
    \def\badge@showlink{false}%
    \setkeys{badge}{#1}%
    \ifx\badge@link\@empty%
        \drawBadgeAux[#1]{#2}{#3}%
    \else%
    {%
        \ifKV@badge@showlink%
        \else%
            \hypersetup{hidelinks}%
        \fi%
        \href{\badge@link}{\mbox{\drawBadgeAux[#1]{#2}{#3}}}%
        }%
    \fi%
}


\newcommand{\githubBadge}[2][]
{
    \drawBadge[link=https://github.com/#2, logo=\faGithub, #1]{}{github.com/#2}
}

\newcommand{\gitlabBadge}[2][]
{
    \drawBadge[link=https://gitlab.com/#2, logo=\faGitlab, #1]{}{gitlab.com/#2}
}

\newcommand{\forgeBadge}[2][]
{
    \drawBadge[link=https://#2, #1]{}{#2}
}

\NewDocumentCommand{\licenseBadge}{O{} O{license} m}
{
    \drawBadge[link=https://creativecommons.org/#3, logo=\faBalanceScale, #1]{#2}{#3}
}
