\usepackage[table,xcdraw]{xcolor}
\usepackage{amsmath}
\usepackage{fdsymbol}
\usepackage{tikz}
\usepackage{ifthen}
\usepackage[margin=0.4in,bottom=0.35in,top=0.25in]{geometry}
\usepackage[no-math]{fontspec}
\usepackage{xstring}
\usepackage{multirow}
\usepackage{forloop}
\usepackage{xparse}
\usepackage{multicol}
\usepackage{calc}
\usepackage{titlesec}
\usepackage{ragged2e}
\usepackage{fancyhdr}
\usepackage{enumitem}
\usepackage{tabularx}

\usetikzlibrary {shapes.multipart,shapes.misc,shapes.symbols,calc,positioning,fit,backgrounds}

\input{star-wars-icons.sty}
\input{star-wars-talents.sty}

\setmainfont[Path=./fonts/,
  BoldFont=itcsymbol-bold.otf,
  ItalicFont=itcsymbol-italic.otf,
  BoldItalicFont=itcsymbol-bolditalic.otf]{itcsymbol.otf}

\newfontfamily{\tn}{Elektra Medium Pro}
\DeclareTextFontCommand{\texttn}{\tn}
\newfontfamily{\ltn}{Elektra Light Pro}
\DeclareTextFontCommand{\textltn}{\ltn}

\definecolor{titlecolor}{HTML}{384d3e}
\definecolor{subtitlecolor}{HTML}{4f2829}
\definecolor{tablecolor1}{HTML}{b0beba}
\definecolor{tablecolor2}{HTML}{FFFFFF}
\definecolor{tablecolor3}{HTML}{384d3e}
\definecolor{reset}{HTML}{ffffff}
\def\arraystretch{1.2}

\titleformat{\section}
  {\fontsize{35pt}{42pt}\tn\color{titlecolor}}{}{0em}{}
\titleformat{\subsection}
  {\fontsize{14pt}{16pt}\tn\color{subtitlecolor}}{}{0em}{}
\titlespacing{\subsection}{0pt}{10pt}{0pt}

\titleformat{\subsubsection}
  {\fontsize{10pt}{12pt}\tn\color{subtitlecolor}}{}{0em}{}
\titlespacing{\subsubsection}{0pt}{10pt}{0pt}

\newcounter{undeffed}
\setcounter{undeffed}{0}
\newcounter{x}

\setlength{\parindent}{0pt}

\newlength{\tmpwidth}
\newlength{\namewidth}

\def\specialization{}

\makeatletter
\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}
}

\let\@var\@empty
\newcommand{\append}[1]{\addtocounter{undeffed}{1}\xdef\@var{\@var #1} }
% Another variant with kernel \g@addto@macro
\newcommand{\appendother}[1]{\g@addto@macro{\@var}{#1}}

\newcommand{\undeffed}{\@var}


\newcommand{\drawforce}{%
  \@ifundefined{force@\specialization}{}{%
    \definecolor{talentcolor}{HTML}{000000}
    \node[forcepower] at (0.2,-2.48) {};
    \node[talentname] at (0,-2.1) {\footnotesize{}\texttn{FORCE TALENT}};
  }
}

\newcommand{\setforce}{%
  \protected@write\@auxout{}{%
    \global\string\@namedef{force@\specialization}{}%
  }%
}

\makeatother

\newlength{\textlength}

\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}
\fancyhead{}
\fancyfoot{}
\fancyfoot[R]{\raisebox{18pt}{\thepage}}

\def\career{}
\def\careerskills{}

\newcommand{\setcareer}[2]{%
  \def\career{#1}%
  \def\careerskills{#2}%
}

\tikzset{
  every node/.style={anchor=north west},
  talent/.style={
    chamfered rectangle,
    chamfered rectangle corners={north east, south west, south east},
    draw,
    thick,
    double,
    inner xsep=-2pt,
    inner ysep=0pt,
    fill=white,
    align=left
  },
  talentname/.style={
    chamfered rectangle,
    chamfered rectangle corners={north east, south east},
    inner ysep=0cm,
    inner xsep=0.11cm,
    text width = 2.81cm,
    align=left,
    text=white,
    fill=talentcolor
  },
  cost/.style={
    chamfered rectangle,
    text=white,
    chamfered rectangle xsep=2cm,
    text width= 30pt,
    align=center,
    inner ysep=-1pt,
    fill=talentcolor,
    right=of \alph{talentx}\alph{talenty}.south,
    left=-40pt
  },
  upgradecost/.style={
    chamfered rectangle,
    text=white,
    chamfered rectangle xsep=2cm,
    text width= 30pt,
    align=center,
    inner ysep=-1pt,
    fill=talentcolor,
    below right=1pt and -40pt of big\alph{bupgradex}\alph{bupgradey}.south east
  },
  forcepower/.style={
    starburst,
    starburst points=7,
    random starburst=6,
    starburst point height=10pt,
    fill=talentcolor,
    draw=white,
    line width=1.5pt,
    minimum width=30pt,
    minimum height=30pt,
    rotate=155
  },
  ranked/.style={
    talentname,
    fill=white,
    draw = talentcolor,
    thick,
    inner ysep=0.05cm,
    minimum width = 3.36cm,
    inner xsep = 0.05cm,
    chamfered rectangle xsep = 0.15cm
  }
}

\newcounter{talentx}
\newcounter{talenty}

\newcounter{upgradex}
\newcounter{upgradey}
\newcounter{bupgradex}
\newcounter{bupgradey}

\newcounter{tmpchar}
\newcounter{tmpx}
\newcounter{tmpxp}
\newcounter{tmpy}
\newcounter{tmpyp}

\newcommand{\talentline}[2]{
  \StrChar{#1}{2}[\tmpone]
  \StrChar{#2}{2}[\tmptwo]
  \ifthenelse{\equal{\tmpone}{\tmptwo}}{
    \draw[line width=1.5mm] ([xshift=3.57cm,yshift=-0.7cm]#1.north west) -- ([yshift=-0.7cm]#2.north west);
  }{
    \draw[line width=1.5mm] (#1) -- (#2);
  }
}

\newcommand{\upgradeline}[2]{
  \StrChar{#1}{2}[\tmpone]
  \StrChar{#2}{2}[\tmptwo]
  \ifthenelse{\equal{\tmpone}{\tmptwo}}{
    \draw[line width=1.5mm, color=talentcolor] ([xshift=3.57cm,yshift=-0.7cm]#1.north west) -- ([yshift=-0.7cm]#2.north west);
  }{
    \draw[line width=1.5mm, color=talentcolor] (#1) -- (#2);
  }
}

\newcommand{\swtalent}[5]{%
  \addtocounter{talentx}{1}%
  \ifthenelse{\equal{\arabic{talentx}}{5}}{%
    \addtocounter{talenty}{1}%
    \setcounter{talentx}{1}%
  }{}

  \ifthenelse{\equal{#3}{true}}{%
    \definecolor{talentcolor}{HTML}{5f1111}
  }{%
    \definecolor{talentcolor}{HTML}{1e115f}
  }


  \node[text=white,text width = 2.6cm] at ($(\arabic{talentx}*4.5,-\arabic{talenty}*4.9) + (0.5,-0.15)$) (tmp\alph{talentx}\alph{talenty}) {\footnotesize \texttn{\uppercase{#1}}};

  \getnodedimen{tmp\alph{talentx}\alph{talenty}}
  \node[talent] at (\arabic{talentx}*4.5,-\arabic{talenty}*4.9)
    (\alph{talentx}\alph{talenty})
  {
    \settowidth{\textlength}{\footnotesize \texttn{{\uppercase{#1}}}}%
    \tabular{{>{\raggedright}p{3cm}}}
      \vspace*{7pt}
      \ifdim\nodeheight > 15pt
        \rule{0pt}{7pt} \\
      \fi
      \ifdim\nodeheight > 28pt
        \rule{0pt}{7pt} \\
      \fi
      \scriptsize #2 \\
    \endtabular
  };

  \ifthenelse{\equal{#5}{true}}{%
    \node[ranked] at ($(\arabic{talentx}*4.5,-\arabic{talenty}*4.9) + (0.08,-0.08)$) {%
      \small \hspace*{-6pt} \checkbox %
      \settowidth{\textlength}{\footnotesize \texttn{{\uppercase{#1}}}}%
      \ifdim\nodeheight > 15pt
        \\ \phantom{t}
      \fi
      \ifdim\nodeheight > 30pt
        \\ \phantom{t}
      \fi
    };
  }{}

  \ifthenelse{\equal{#4}{true}}{%
    \setforce{}
    \node[forcepower] at ($(\arabic{talentx}*4.5,-\arabic{talenty}*4.9) + (0.3,-0.5)$) {};
  }{}

  \node[talentname] at ($(\arabic{talentx}*4.5,-\arabic{talenty}*4.9) + (0.13,-0.14)$) {%
    \small \hspace*{-6pt} \checkbox %
    \settowidth{\textlength}{\footnotesize \texttn{{\uppercase{#1}}}}%
    \ifdim\nodeheight > 15pt
      \\ \phantom{t}
    \fi
    \ifdim\nodeheight > 30pt
      \\ \phantom{t}
    \fi
  };
  \node[cost] {\tiny\texttn{COST \fpeval{5*\arabic{talenty}}}};
  \node[text=white,text width = 2.6cm] at ($(\arabic{talentx}*4.5,-\arabic{talenty}*4.9) + (0.5,-0.19)$) (t\alph{talentx}\alph{talenty}) {\footnotesize \texttn{\uppercase{#1}}};
}

\newenvironment{talenttree}[5]{%
  \setcounter{talentx}{0}
  \setcounter{talenty}{1}
  \def\specialization{#1}
  \center
    \def\bookandpage{#3}
    \tabular{p{0.75\textwidth} p{0.15\textwidth}}
      \texttn{\Huge{}\career{}:} \textltn{\Huge{}\specialization} & \multirow{4}{*}[0.7cm]{%
        \begin{tikzpicture}
          \drawforce
          \definecolor{talentcolor}{HTML}{000000}
          \node[ranked,minimum width = 3.33cm] at (-0.05, -1.34) {\footnotesize{}\texttn{RANKED TALENT}};
          \node[talentname] at (0,-1.4) {\footnotesize{}\texttn{RANKED TALENT}};
          \definecolor{talentcolor}{HTML}{5f1111}
          \node[talentname] at (0,-0.7) {\footnotesize{}\texttn{ACTIVE}};
          \definecolor{talentcolor}{HTML}{1e115f}
          \node[talentname] at (0,0) {\footnotesize{}\texttn{PASSIVE}};
        \end{tikzpicture}
      }\\
      \ifthenelse{\equal{\careerskills}{}}{
        \ifthenelse{\equal{#4}{}}{}{%
          \texttn{\footnotesize{}Gain Force Rating #4} \\
        }
      }{
        \texttn{\footnotesize{}Career Skills:} \textltn{\footnotesize{}\careerskills}\\
      }
      \ifthenelse{\equal{#2}{}}{}{
        \texttn{\footnotesize{}Additional Carreer Skills:} \textltn{\footnotesize{}#2}\\
      }
      \ifthenelse{\equal{#5}{}}{}{%
        \texttn{\footnotesize{}Prerequisite: Force Rating #5} \\
      }

    \endtabular\\
    \vspace*{20pt}
    \ifthenelse{\equal{\careerskills}{}}{
      \vspace{5pt}
      \ifthenelse{\equal{#4}{}}{
        \vspace{10pt}
      }{}
    }{}
    \tikzpicture
}{%
    \endtikzpicture
    \fancyfoot[C]{\raisebox{18pt}{\bookandpage}}
  \endcenter
  \newpage
}

\newcommand{\talentlines}[2]{
  \StrDel{#1}{ }[\tmpvert]
  \StrDel{\tmpvert}{/}[\tmpvert]
  \setcounter{tmpx}{0}
  \setcounter{tmpy}{1}
  \setcounter{tmpyp}{2}

  \forloop{tmpchar}{1}{\value{tmpchar} < 17}{
    \addtocounter{tmpx}{1}%
    \ifthenelse{\equal{\arabic{tmpx}}{5}}{%
      \addtocounter{tmpyp}{1}%
      \addtocounter{tmpy}{1}%
      \setcounter{tmpx}{1}%
    }{}
    \StrChar{\tmpvert}{\value{tmpchar}}[\tmpchar]
    \ifthenelse{\equal{\tmpchar}{|}}{
      \talentline{\alph{tmpx}\alph{tmpy}}{\alph{tmpx}\alph{tmpyp}}
    }{}
  }

  \StrDel{#2}{ }[\tmphor]
  \StrDel{\tmphor}{/}[\tmphor]
  \setcounter{tmpx}{0}
  \setcounter{tmpxp}{1}
  \setcounter{tmpy}{2}

  \forloop{tmpchar}{1}{\value{tmpchar} < 13}{
    \addtocounter{tmpx}{1}%
    \addtocounter{tmpxp}{1}%
    \ifthenelse{\equal{\arabic{tmpx}}{4}}{%
      \addtocounter{tmpy}{1}%
      \setcounter{tmpx}{1}%
      \setcounter{tmpxp}{2}%
    }{}
    \StrChar{\tmphor}{\value{tmpchar}}[\tmpchar]
    \ifthenelse{\equal{\tmpchar}{-}}{
      \talentline{\alph{tmpx}\alph{tmpy}}{\alph{tmpxp}\alph{tmpy}}
    }{}
  }
}

\newcommand{\upgradeplaceholder}{%
  \addtocounter{upgradex}{1}%
  \ifthenelse{\equal{\arabic{upgradex}}{5}}{%
    \addtocounter{upgradey}{1}%
    \setcounter{upgradex}{1}%
  }{}

  \node[text=white,text width = 2.6cm] at ($(\arabic{upgradex}*4.5,-\arabic{upgradey}*3.8) + (0.5,-0.15)$) (tmp\alph{upgradex}\alph{upgradey}) {\footnotesize \texttn{\uppercase{ }}};

  \getnodedimen{tmp\alph{upgradex}\alph{upgradey}}
  \node[talent,color=white] at (\arabic{upgradex}*4.5,-\arabic{upgradey}*3.8)
    (\alph{upgradex}\alph{upgradey})
  {
    \settowidth{\textlength}{\footnotesize \texttn{{\uppercase{ }}}}%
    \tabular{p{3cm}}
      \rule{0pt}{13pt}\\
    \endtabular
  };
}

\newcommand{\swupgrade}[4]{%
  \forloop{x}{0}{\value{x} < #3}{%
    \upgradeplaceholder{}%
  }%

  \setlength{\tmpwidth}{(4.5cm * #3)-1.05cm}
  \node[text=white,text width = \tmpwidth] at ($(\arabic{bupgradex}*4.5,-\arabic{bupgradey}*3.8) + (0.5,-0.15)$) (tmpup\alph{bupgradex}\alph{bupgradey}) {\footnotesize \texttn{\uppercase{#1}}};

  \getnodedimen{tmpup\alph{bupgradex}\alph{bupgradey}}
  \node[talent, text width = \tmpwidth, draw=talentcolor] at (\arabic{bupgradex}*4.5,-\arabic{bupgradey}*3.8)
    (big\alph{bupgradex}\alph{bupgradey})
  {
    \settowidth{\textlength}{\footnotesize \texttn{{\uppercase{#1}}}}%
    \tabular{p{\tmpwidth{} - 0.5cm}}
      \rule{0pt}{13pt}\\
      \ifdim\nodeheight > 15pt
        \rule{0pt}{8pt} \\
      \fi
      \ifdim\nodeheight > 30pt
        \rule{0pt}{8pt} \\
      \fi
      \scriptsize{} #2 \\
    \endtabular
  };

  \setlength{\namewidth}{\tmpwidth - 0.6cm}
  \node[talentname, text width = \namewidth] at ($(\arabic{bupgradex}*4.5,-\arabic{bupgradey}*3.8) + (0.13,-0.14)$) {%
    \small \hspace*{-6pt} \checkbox %
    \settowidth{\textlength}{\footnotesize \texttn{{\uppercase{#1}}}}%
    \ifdim\nodeheight > 15pt
      \\ \phantom{t}
    \fi
    \ifdim\nodeheight > 30pt
      \\ \phantom{t}
    \fi
  };
  \node[text=white,text width = \namewidth] at ($(\arabic{bupgradex}*4.5,-\arabic{bupgradey}*3.8) + (0.5,-0.19)$) (t\alph{bupgradex}\alph{bupgradey}) {\footnotesize \texttn{\uppercase{#1}}};

  \node[upgradecost] (c\alph{bupgradex}\alph{bupgradey}) {\tiny\texttn{COST #4}};

  \addtocounter{bupgradex}{#3}%
  \ifthenelse{\equal{\arabic{bupgradex}}{5}}{%
    \addtocounter{bupgradey}{1}%
    \setcounter{bupgradex}{1}%
  }{}
}

\newcommand{\upgradelines}[2]{
  \begin{scope}[on background layer]
    \StrDel{#1}{ }[\tmpvert]
    \StrDel{\tmpvert}{/}[\tmpvert]
    \setcounter{tmpx}{0}
    \setcounter{tmpy}{1}
    \setcounter{tmpyp}{2}

    \forloop{tmpchar}{1}{\value{tmpchar} < 17}{
      \addtocounter{tmpx}{1}%
      \ifthenelse{\equal{\arabic{tmpx}}{5}}{%
        \addtocounter{tmpyp}{1}%
        \addtocounter{tmpy}{1}%
        \setcounter{tmpx}{1}%
      }{}
      \StrChar{\tmpvert}{\value{tmpchar}}[\tmpchar]
      \ifthenelse{\equal{\tmpchar}{|}}{
        \upgradeline{\alph{tmpx}\alph{tmpy}}{\alph{tmpx}\alph{tmpyp}}
      }{}
    }

    \StrDel{#2}{ }[\tmphor]
    \StrDel{\tmphor}{/}[\tmphor]
    \setcounter{tmpx}{0}
    \setcounter{tmpxp}{1}
    \setcounter{tmpy}{2}

    \forloop{tmpchar}{1}{\value{tmpchar} < 13}{
      \addtocounter{tmpx}{1}%
      \addtocounter{tmpxp}{1}%
      \ifthenelse{\equal{\arabic{tmpx}}{4}}{%
        \addtocounter{tmpy}{1}%
        \setcounter{tmpx}{1}%
        \setcounter{tmpxp}{2}%
      }{}
      \StrChar{\tmphor}{\value{tmpchar}}[\tmpchar]
      \ifthenelse{\equal{\tmpchar}{-}}{
        \upgradeline{\alph{tmpx}\alph{tmpy}}{\alph{tmpxp}\alph{tmpy}}
      }{}
    }
  \end{scope}
}

\newenvironment{forcepower}[3]{%
  \setcounter{upgradex}{0}
  \setcounter{upgradey}{1}
  \setcounter{bupgradex}{1}
  \setcounter{bupgradey}{1}
  \definecolor{talentcolor}{HTML}{215e26}
  \def\powername{#1}
  \center
    \def\bookandpage{#3}
    \tabular{p{0.75\textwidth} p{0.15\textwidth}}
      \texttn{\Huge{}Force Power:} \textltn{\Huge{}\powername} & \multirow{4}{*}[0.7cm]{%
        \begin{tikzpicture}
          \node[talentname] at (0,0) {\footnotesize{}\texttn{FORCE ABILITY}};
        \end{tikzpicture}
      }\\
      \texttn{\footnotesize{}Prerequisite: Force Rating #2+} \\

    \endtabular\\
    \vspace*{20pt}
    \vspace{5pt}
    \vspace{10pt}
    \tikzpicture
}{%
    \endtikzpicture
    \fancyfoot[C]{\raisebox{18pt}{\bookandpage}}
  \endcenter
  \newpage
}


\newcommand\charPart[2]{\noindent
  \begin{tikzpicture}
    \node[shape=chamfered rectangle, draw, fill=tablecolor3, text depth=-0.25em, text height=1.25em, inner sep=0pt,outer sep=0pt, minimum height=8mm, minimum width=20mm, thick, chamfered rectangle corners={north west, north east}, font=\tn\color{white}](label){\footnotesize\uppercase{#1}};
    \node[shape=circle, thick, draw, above=-1em of label, fill=white]{\large\bfseries #2};
  \end{tikzpicture}
}


\newcommand\Characteristics[6]{
\noindent
\begin{center}
  \charPart{BRAWN}{#1}\charPart{AGILITY}{#2}\charPart{INTELLECT}{#3}\\\charPart{CUNNING}{#4}\charPart{WILLPOWER}{#5}\charPart{PRESENCE}{#6}%
\end{center}
\hfill\null}
