% COMMANDS ZUM ZUSAMMENBAUEN DER KARTEN
% ---------------------------------------

% TikZ/PGF Settings für die Karten
\pgfmathsetmacro{\cardwidth}{6}
\pgfmathsetmacro{\cardheight}{9}
\pgfmathsetmacro{\imagewidth}{\cardwidth}
\pgfmathsetmacro{\imageheight}{0.75*\cardheight}
\pgfmathsetmacro{\stripwidth}{0.7}
\pgfmathsetmacro{\strippadding}{0.2}
\pgfmathsetmacro{\textpadding}{0.1}
\pgfmathsetmacro{\titley}{
    \cardheight - \strippadding - 1.5 * \textpadding - 0.5 * \stripwidth
}
\pgfmathsetmacro{\contentTextWidth}{
    (\cardwidth - 2* \strippadding - \stripwidth - 2* \textpadding - 0.3) * 1cm
}
\pgfmathsetmacro{\contentLeft}{2* \strippadding + \stripwidth + \textpadding}
\pgfmathsetmacro{\stripShortHeight}{1}
\pgfmathsetmacro{\contentBottom}{\stripShortHeight + \textpadding}
\pgfmathsetmacro{\leftStripLongHeight}{
    \cardheight - (3 * \strippadding) - \stripShortHeight
}

% Thresholds for the text in the left strip
\pgfmathsetmacro{\leftStripLongHeightNormal}{\leftStripLongHeight * 0.75}
\pgfmathsetmacro{\leftStripLongHeightSmall}{\leftStripLongHeight * 0.85}
\pgfmathsetmacro{\leftStripLongHeightFootnotesize}{\leftStripLongHeight * 0.95}
\pgfmathsetmacro{\leftStripLongHeightScriptsize}{\leftStripLongHeight * 1.05}

\pgfmathsetmacro{\leftStripTextXCoord}{\strippadding + 0.5 * \stripwidth}
\pgfmathsetmacro{\costXCoord}{\cardwidth - 0.5 * \stripwidth - \strippadding}


% Formen der einzelnen Kartenelemente/-bestandteile
\def\shapeCard{(0,0) rectangle (\cardwidth, \cardheight)}
\def\shapeLeftStripLong{
    (\strippadding, -\strippadding)
    rectangle
    (\strippadding + \stripwidth,
     \cardheight - \strippadding - \strippadding - \stripShortHeight)
}
\def\shapeLeftStripShort{
    (\strippadding, \cardheight - \strippadding - \stripShortHeight)
    rectangle
    (\strippadding + \stripwidth, \cardheight + \strippadding)
}
\def\shapeRightStripShortHigh{
    (\cardwidth - \stripwidth - \strippadding,
     \cardheight - \strippadding - \stripShortHeight)
    rectangle
    (\cardwidth - \strippadding, \cardheight + \strippadding)
}
\def\shapeRightStripShortLow{
    (\cardwidth - \stripwidth - \strippadding, -\strippadding)
    rectangle
    (\cardwidth - \strippadding, \stripShortHeight)
}
\def\shapeTitleArea{
    (2 * \strippadding + \stripwidth, \cardheight - \strippadding)
    rectangle
    (\cardwidth - 2 * \strippadding - \stripwidth, \cardheight - 2 * \stripwidth)
}


% macro to get the height of an node
% https://tex.stackexchange.com/questions/554237/how-can-i-get-the-height-of-a-tikz-node-and-store-it-in-a-length
\makeatletter % allow use of @
\NewDocumentCommand {\getnodedimen} {O{\nodewidth} O{\nodeheight} m} {
    \begin{pgfinterruptboundingbox}
        \begin{scope}[local bounding box=bb@temp]
            \node[inner sep=0pt, fit=(#3)] {};
        \end{scope}
        \path ($(bb@temp.north east)-(bb@temp.south west)$);
    \end{pgfinterruptboundingbox}
    \pgfgetlastxy{#1}{#2}
}
\makeatother % end use of @

% Stylings für die Elemente definieren
\tikzset{
    %   runde Ecken für die Karten
    cardcorners/.style={rounded corners=0.2cm},%
    %   runde Ecken für die "Fähnchen"
    elementcorners/.style={rounded corners=0.1cm},%
    strip/.style={elementcorners},%
    %   Bild für das Kartenmotiv
    cardimage/.style={%
        path picture={%
            \node[below=-1.5mm] at (0.5*\cardwidth,\cardheight) {%
                \includegraphics[width=\imagewidth cm]{#1}%
            };
        }%
    },%
}

% TikZ-Raster
\newcommand{\carddebug}{
    \draw [step=1,help lines] (0,0) grid (\cardwidth,\cardheight);
}

% Rahmen der Karte
\newcommand{\cardborder}{
    \draw[lightgray,cardcorners] \shapeCard;
}

% Hintergrund der Karte
\newcommand{\cardbackground}[1]{
    \draw[cardcorners, cardimage=#1] \shapeCard;
}

% Kategorie der Karte
% arg1: Background
% arg2: Type
% arg3: TypeIcon
% arg4: Text for left stripe
% arg5: Title
\newcommand{\cardtype}[5]{
    % First we fill the intersecting area
    % The \clip command does not allow options, therefore
    % we have to use a scope to set the even odd rule.
    \begin{scope}[even odd rule]
        % Define a clipping path. All paths outside shapeCard will
        % be cut because the even odd rule is set.
        \clip[cardcorners] \shapeCard;

        % Fill shapeLeftStripLong and shapeLeftStripShort.
        % Since the even odd rule is set, only the card will be filled.
        \fill[strip, #1] \shapeLeftStripLong;
        \fill[strip, #1] \shapeLeftStripShort;
    \end{scope}

    \node(leftStripTextInvisible) at (\leftStripTextXCoord, \leftStripLongHeight - \textpadding)
    [rotate=90, anchor=east, font=\normalsize, opacity=0] {
        \uppercase{#4 - #2}
    };

    \getnodedimen{leftStripTextInvisible}

    \node(leftStripText) at (\leftStripTextXCoord, \leftStripLongHeight - \textpadding)
    [rotate=90, anchor=east, font=\normalsize] {
        \color{white}

        \ifdimgreater{\leftStripLongHeightNormal cm}{\nodeheight}
        {\uppercase{#4 - #2}}
        {\ifdimgreater{\leftStripLongHeightSmall cm}{\nodeheight}
        {\small \uppercase{#4 - #2}}
        {\ifdimgreater{\leftStripLongHeightFootnotesize cm}{\nodeheight}
        {\footnotesize \uppercase{#4 - #2}}
        {\ifdimgreater{\leftStripLongHeightScriptsize cm}{\nodeheight}
        {\scriptsize \uppercase{#4 - #2}}
        {\tiny \uppercase{#4 - #2}}}}}
    };

    \node at (\leftStripTextXCoord, \cardheight - \strippadding - \strippadding - 0.37) {
        \color{white}#3
    };

    \cardtitle{#5}
}

% Known card types
\newcommand{\cardtypeUnitToken}[1]{
    \cardtype{tokenbg}{Unit}{\LARGE\sympawn}{TOKEN}{{\Large TOKEN} \\ #1}}

\newcommand{\cardtypeUnit}[1]{\cardtype{unitbg}{Unit}{\LARGE\sympawn}{#1}{#1}}

\newcommand{\cardtypeBoss}[1]{\cardtype{bossbg}{Boss}{\LARGE\symking}{#1}{#1}}

\newcommand{\cardtypeSpellToken}[1]{
    \cardtype{tokenbg}{Spell}{\Large\faBolt}{TOKEN}{{\Large TOKEN} \\ #1}
}
\newcommand{\cardtypeSpell}[1]{\cardtype{spellbg}{Spell}{\Large\faBolt}{#1}{#1}}

\newcommand{\cardtypeIntention}[1]{
    \cardtype{intentionbg}{Intention}{\Large\faCrosshairs}{#1}{#1}
}

\newcommand{\cardtypeArtifactToken}[1]{
    \cardtype{tokenbg}{Artifact}{\LARGE\decoone}{TOKEN}{{\Large TOKEN} \\ #1}
}
\newcommand{\cardtypeArtifact}[1]{
    \cardtype{artifactbg}{Artifact}{\LARGE\decoone}{#1}{#1}
}

\newcommand{\cardtypeEquipmentToken}[1]{
    \cardtype{tokenbg}{Equipment}{\huge\dag}{TOKEN}{{\Large TOKEN} \\ #1}
}
\newcommand{\cardtypeEquipment}[1]{
    \cardtype{equipmentbg}{Equipment}{\huge\dag}{#1}{#1}
}

\newcommand{
    \cardtypePotion}[1]{\cardtype{equipmentbg}{Potion}{\Large\faFlask}{#1}{#1}
}
\newcommand{\cardtypePotionToken}[1]{
    \cardtype{equipmentbg}{Potion}{\Large\faFlask}{TOKEN}{\Large TOKEN}
}

% Titel der Karte
\newcommand{\cardtitle}[1]{
    \fill[elementcorners, titlebg, opacity=.85] \shapeTitleArea;
    \node[inner ysep=0, text width=3.75cm] at (0.5 * \cardwidth, \titley) {
        \begin{center}
            \color{white}\uppercase{\normalsize #1}
        \end{center}
    };
}

% Inhalt der Karte
\newcommand{\cardsplitcontent}[2]{
    % \begin{scope}[even odd rule]
        % \clip[cardcorners] \shapeCard;
    % \end{scope}

    % draw content node just to get its dimensions
    \node(content)[
        above right,
        minimum width=\cardwidth,
        text width=\contentTextWidth
    ] at (\contentLeft, \contentBottom) {
        \textit{\normalsize #1}
        \vrule width \textwidth height 2pt \\[-2pt]
        \vspace{0.2cm}
        {\normalsize #2}
    };

    % draw content background using dimensions of the content node
    \getnodedimen{content}
    \node[
        fill=contentbg,
        above right,
        elementcorners,
        minimum height=\nodeheight,
        minimum width=\nodewidth + 1
    ] at (\contentLeft - \textpadding, \contentBottom) {};

    % redraw content so it is on top of the background
    \node[
        above right,
        minimum width=\cardwidth,
        text width=\contentTextWidth
    ] at (\contentLeft, \contentBottom) {
        \textit{\normalsize #1}
        \vrule width \textwidth height 2pt \\[-2pt]
        \vspace{0.2cm}
        {\normalsize #2}
    };
}

% Inhalt der Karte
\newcommand{\cardcontent}[1]{
    % \begin{scope}[even odd rule]
        % \clip[cardcorners] \shapeCard;
    % \end{scope}
    \node(content)[
        above right,
        text width=\contentTextWidth
    ] at (\contentLeft, \contentBottom) {
        {\normalsize #1}
    };

    % draw content background using dimensions of the content node
    \getnodedimen{content}
    \node[
        fill=contentbg,
        above right,
        elementcorners,
        minimum height=\nodeheight,
        minimum width=\nodewidth + 1
    ] at (\contentLeft - \textpadding, \contentBottom) {};

    % redraw content so it is on top of the background
    \node[above right, text width=\contentTextWidth] at (\contentLeft, \contentBottom) {
        {\normalsize #1}
    };
}

% Buying cost
\newcommand{\cardbuycost}[1]{
    \begin{scope}[even odd rule]
        \clip[cardcorners] \shapeCard;
        \fill[strip, pricebg] \shapeRightStripShortHigh;
    \end{scope}
    \node at (\costXCoord, \titley - \textpadding) {\color{black}#1};
}

% Playing cost
\newcommand{\cardplaycost}[1]{
    \begin{scope}[even odd rule]
        \clip[cardcorners] \shapeCard;
        \fill[strip, playbg] \shapeRightStripShortLow;
    \end{scope}
    \node at (\costXCoord, \strippadding + \strippadding) {\color{black}#1};
}

% Module containing the card
\newcommand{\cardmodule}[1]{
    \node[rotate=90, right] at
    (\strippadding + 0.5 * \stripwidth, \textpadding)
    {\color{black}#1};
}
