% some packages required for this code
\RequirePackage{tikz}
% needed for the dashed lines
% \RequirePackage{arydshln} % incompatible with fancy style

%%% The code for the Skilllevel illustration with the little boxes.
% This is idea stolen from the limecv package
\@initializelength{\cvSkillRectangleSize}
\setlength{\cvSkillRectangleSize}{1.2ex}
\newcount\my@repeat@count
\NewDocumentCommand{\cvSkill}{m}{%
  \begingroup
  \my@repeat@count=\z@
  \@whilenum\my@repeat@count<#1\do{\tikz\filldraw[color1] (0, 0) rectangle (\cvSkillRectangleSize, \cvSkillRectangleSize);\advance%
    \my@repeat@count\@ne\,}%
  \my@repeat@count=\numexpr5-\z@\relax
  \@whilenum\my@repeat@count>#1\do{\tikz\filldraw[color2!30] (0, 0) rectangle (\cvSkillRectangleSize, \cvSkillRectangleSize);\advance%
    \my@repeat@count\m@ne\,}%
  \endgroup
}

\@initializelength{\cvskillwidth}
\@initializelength{\cvskilldescriptorwidth}
\@initializelength{\cvskilllegenddescriptorwidth}
\@initializelength{\cvskillexperiencewidth}
\@initializelength{\skillmatrixcolumnwidth}
\@initializelength{\skillmatrixcommentwidth}
\@initializelength{\skillmatrixcommentpadding}
\@initializelength{\skillmatrixhintscolumnwidth}
\@initializelength{\skilllegendhintscolumnwidth}
\NewDocumentCommand{\recomputecvskillmatrixlengths}{}{%
  \setlength{\skillmatrixcommentpadding}{1ex}
%   \setlength{\separatorcolumnwidth}{\skillmatrixcommentpadding}
  \setlength{\skillmatrixhintscolumnwidth}{\widthof{``Languages''}}
  \setlength{\skilllegendhintscolumnwidth}{\skillmatrixhintscolumnwidth} %0.175\textwidth
%   \setlength{\skillmatrixhintscolumnwidth}{0.14\textwidth} %0.175\textwidth
  \setlength{\cvskillwidth}{\widthof{\cvSkill{5}}}
  \setlength{\cvskillexperiencewidth}{\widthof{``Jahre''}}
  \@initializelength{\matrixbodylength}
  \setlength{\matrixbodylength}{\maincolumnwidth-\skillmatrixhintscolumnwidth-\skillmatrixcommentpadding}%\skillmatrixhintscolumnwidth
  \setlength{\skillmatrixcolumnwidth}{0.45\matrixbodylength}%
  \setlength{\cvskilldescriptorwidth}{\skillmatrixcolumnwidth-\cvskillwidth-\cvskillexperiencewidth}
  \setlength{\skillmatrixcommentwidth}{\matrixbodylength-\skillmatrixcolumnwidth-\skillmatrixcommentpadding}%
  \setlength{\cvskilllegenddescriptorwidth}{0.5\maincolumnwidth-\cvskillwidth-\skillmatrixcommentpadding}
  regular lengths
  }
  
\RequirePackage{multirow}
% \RequirePackage{ifmtarg}
\NewDocumentCommand\legendFontSize{}{\scriptsize}
\DeclareDocumentCommand\starIndependentTabular{}{}%
\NewDocumentCommand\cvSkillMatrixLegend{s +O{.25em} +O{basic knowledge} +O{intermediate knowledge with experience in projects} +O{extensive experience in projects} +O{deep expert knowledge} +O{expert/guru} +m}{%
    % check whether Argument #8 is given and if so provide it as cvitem
    \def\legendString{#8}%
    \ifdefempty{\legendString}{}{\cvitem{#8}{}}%
    \arrayrulecolor{color1}%
    \setlength\arrayrulewidth{\separatorrulewidth}%
    \if@aftersection\else%
    \vspace*{-\separatorrulewidth}\fi% HACK; I don't understand where the space is coming from, nor what it's exact value is :(
    \noindent%
    \RenewDocumentCommand{\starIndependentTabular}{}{%
        \begin{tabular}[t]{@{}p{\hintscolumnwidth}%\skilllegendhintscolumnwidth
                        @{\hspace{\separatorcolumnwidth}}|@{\hspace{\separatorcolumnwidth}}
                        p{\cvskillwidth}@{} 
                        p{\cvskilllegenddescriptorwidth}  @{\hspace{2\skillmatrixcommentpadding}}
                        p{\cvskillwidth}@{}p{\cvskilllegenddescriptorwidth}@{}}%
            \@moderncvstrut{4pt}{16pt}  & \cvSkill{1}\, & \,{\legendFontSize #3} & \cvSkill{3}\, &\,{\legendFontSize #5 } \\
                        %
                    & \cvSkill{2}\, & \,\multirow{2}{\cvskilllegenddescriptorwidth}{{\legendFontSize #4}} & \cvSkill{4}\,  &\,{\legendFontSize #6 } \\
                %         
                    &  &   & \cvSkill{5}\, &\,{\legendFontSize #7 } \\[#2]% the spacing needs to be inside the cell for the vertical rule to extend correctly
        \end{tabular}%
        \par\@aftersectionfalse\ignorespaces%    
    }%
    % because of this weird style and the position of the parameter [#2], the dashed lines of the other version look bad. So no lines. 
    \IfBooleanTF#1%
        {%
            \starIndependentTabular%
        }
        {% 
            \starIndependentTabular%
        }
  }%
\NewDocumentCommand\cvSkillMatrixHeadFont{}{\normalfont}%
\NewDocumentCommand\cvSkillMatrixHead{+O{.25em} +O{Level} +O{Skill} +O{Years} +O{Comment}}{%  
    \arrayrulecolor{color1}%
    \setlength\arrayrulewidth{\separatorrulewidth}%
    \if@aftersection\else%
    \vspace*{-\separatorrulewidth}\fi% HACK; I don't understand where the space is coming from, nor what it's exact value is :(
    \noindent%
    \begingroup%
%     \renewcommand{\arraystretch}{1.25}%
    \begin{tabular}[t]{@{}p{\hintscolumnwidth}%
                    @{\hspace{\separatorcolumnwidth}}|@{\hspace{\separatorcolumnwidth}}%
                    p{\skillmatrixhintscolumnwidth}%
                    @{\hspace{\skillmatrixcommentpadding}}p{\cvskillwidth}@{}%
                    p{\cvskilldescriptorwidth}@{}%
                    p{\cvskillexperiencewidth}%
                    @{\hspace{\skillmatrixcommentpadding}}p{\skillmatrixcommentwidth}@{}}%
        & & \centering{\cvSkillMatrixHeadFont#2} & \centering{\cvSkillMatrixHeadFont#3} & \centering{\cvSkillMatrixHeadFont#4} & {\cvSkillMatrixHeadFont#5} \\[#1]% the spacing needs to be inside the cell for the vertical rule to extend correctly
    \end{tabular}%
    \endgroup%
    \par\@aftersectionfalse\ignorespaces%    
  }%


\NewDocumentCommand\cvSkillMatrixEntry{s O{.25em} +m +m +m +m +m}{%
    \arrayrulecolor{color1}%
    \setlength\arrayrulewidth{\separatorrulewidth}%
    \if@aftersection\else%
    \vspace*{-\separatorrulewidth}\fi% HACK; I don't understand where the space is coming from, nor what it's exact value is :(
    \noindent%
    %test for the star * in the command
    \IfBooleanTF#1{% If a star is seen a dotted line is drawn above the entry
        \begingroup%
        \renewcommand{\arraystretch}{1.25}%
        \begin{tabular}[t]{@{}p{\hintscolumnwidth}%
                        @{\hspace{\separatorcolumnwidth}}|@{\hspace{\separatorcolumnwidth}}p{\skillmatrixhintscolumnwidth}
                        @{\hspace{\skillmatrixcommentpadding}}
                        p{\cvskillwidth}@{}
                        p{\cvskilldescriptorwidth}@{}
                        p{\cvskillexperiencewidth} @{\hspace{\skillmatrixcommentpadding}}p{\skillmatrixcommentwidth}@{}}%
%             \cline{3-6}%
            & \raggedleft\hintstyle{#3} &\centering \cvSkill{#4} &\centering {#5} & \centering {#6} &{\itshape#7}\\[#2]%
        \end{tabular}%
        \endgroup%
%         \par\@aftersectionfalse\ignorespaces%
    }{% If no star is seen no line is drawn
        \begingroup%
        \renewcommand{\arraystretch}{1.25}%
        \begin{tabular}[t]{@{}p{\hintscolumnwidth}%
                        @{\hspace{\separatorcolumnwidth}}|@{\hspace{\separatorcolumnwidth}}p{\skillmatrixhintscolumnwidth}
                        @{\hspace{\skillmatrixcommentpadding}}
                        p{\cvskillwidth}@{}
                        p{\cvskilldescriptorwidth}@{}
                        p{\cvskillexperiencewidth} @{\hspace{\skillmatrixcommentpadding}}p{\skillmatrixcommentwidth}@{}}%
            & \raggedleft\hintstyle{#3} &\centering \cvSkill{#4} &\centering {#5} & \centering {#6} &{\itshape#7}\\[#2]%
        \end{tabular}%
        \endgroup%
%         \par\@aftersectionfalse\ignorespaces%
    }%
    \par\@aftersectionfalse\ignorespaces%
}
