\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{moncv}[2021/01/24 CV a la mode]

\LoadClass[a4paper]{article}

\RequirePackage[utf8]{inputenc}
\RequirePackage[francais]{babel}

\RequirePackage{geometry}

\RequirePackage{ifthen}

\RequirePackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{intersections}
\usetikzlibrary{decorations.shapes,shapes.geometric}

\RequirePackage{sansmathfonts}

\RequirePackage{hyperref}

\pagestyle{empty}

\setlength{\parindent}{0pt}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% declarations
%%%%%%%%%%%%%%%%%%%%%%%%% categories
\newlength{\catenomlength}
\newlength{\catewidth}
\newlength{\cateskip}
%%%%%%%%%%%%%%%%%%%%%%%%% subcategories
\newsavebox{\subcatebox}
\newlength{\subcatewidth}
\newlength{\subcateheight}
%%%%%%%%%%%%%%%%%%%%%%%%% competences
\newlength{\complength}
\newlength{\compheight}
%%%%%%%%%%%%%%%%%%%%%%%%% info panneau
\newsavebox{\panneaubox}
\newlength{\infopanneauwidth}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% modifiables
%%%%%%%%%%%%%%%%%%%%%%%%% couleurs
\colorlet{fond1}{blue!20}
\colorlet{fond2}{black!20}

\colorlet{texte1}{blue!90!green!50}

\colorlet{bon}{blue!70!green!70}
\colorlet{mauvais}{blue!30!red!70}

%%%%%%%%%%%%%%%%%%%%%%%%% dimensions def (en cm)
\def\hbandeau{6}
\def\lpanneau{6}
\def\catemargin{1}

\def\subcatemargin{0.8}
\def\subcateskip{0.2}

\def\infopanneauskip{1}

\def\picway{}

%%%%%%%%%%%%%%%%%%%%%%%%% dimensions set
\setlength{\complength}{3cm}
\setlength{\compheight}{0.5cm}
\setlength{\cateskip}{1cm}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newenvironment{categorie}[2][0pt]{% l'argument #1 optionnel est la correction de l'écart entre les catégories
  \setlength{\linewidth}{\textwidth} %%% bizarre non ? La linewidth semble avoir été définie avant le rappel de \geometry dans \AtBeginDocument
  \def\catenom{{\LARGE \textsc{#2}}}
  \settowidth{\catenomlength}{\catenom}
  \begin{tikzpicture}[texte1,remember picture,overlay]
    \draw (0,0) coordinate (rond) -- ++(\catemargin,0) coordinate (noeud) -- (\textwidth-\catenomlength,0) node [right,yshift=0.2ex] {\catenom};
    \coordinate (box) at (noeud);
    % \draw [fill,thick] (noeud) circle (0.02);
  \end{tikzpicture}%
  \begin{tikzpicture}[remember picture, overlay,texte1]
    \draw[fill=white,ultra thick] (rond) circle (0.1);
  \end{tikzpicture}%
  \def\argument{#1}%
}{\vspace{\cateskip}\vspace{\argument}}

\newenvironment{catetexte}{\setlength{\catewidth}{\linewidth}\addtolength{\catewidth}{-\catemargin cm}
  \hspace*{\catemargin cm}\begin{minipage}{\catewidth}}{\end{minipage}}

\newenvironment{subcategorie}[2][]{%
  \begin{lrbox}{\subcatebox}
    \setlength{\subcatewidth}{\linewidth}%
    \addtolength{\subcatewidth}{-\catemargin cm}%
    \addtolength{\subcatewidth}{-\subcatemargin cm}%
    \addtolength{\subcatewidth}{-4pt}%
    \def\illustration{#1}%
    \ifthenelse{\equal{\illustration}{}}{%
      \begin{minipage}{\subcatewidth}
      }{%
        \addtolength{\subcatewidth}{-2cm}%
        \begin{minipage}{\subcatewidth}
      }
    }{%
    \end{minipage}
    \ifthenelse{\equal{\illustration}{}}{}{
      \begin{minipage}{2cm}
        \centering
        \includegraphics[width=2cm,height=2cm,keepaspectratio]{\picway\illustration}
    \end{minipage}
    }
  \end{lrbox}%
  \begin{tikzpicture}[remember picture,overlay,texte1]
    \draw [fill,thick] (noeud) circle (0.02);
    \path (noeud) |- coordinate (coin) ($(box.south)+(0,-\subcateskip)$);
    \path (coin) ++(\subcatemargin,0) node (box) [below right] {\usebox{\subcatebox}};
    \draw (noeud) |- coordinate (noeud) (box.west);

    \draw let \p1=($(box.north west)-(box.south west)$),\n1={veclen(\x1,\y1)} in ($(box.south west) +(0.1,0)$) -- (box.south west) --  ++(\p1) -- ++(0.1,0) node {\global\setlength{\subcateheight}{\n1}};

    \draw [fill,thick] (noeud) circle (0.02);
  \end{tikzpicture}%
  \vspace{\subcateheight}
}

\newcommand{\etoiles}[2]{% ce qui est con avec cette étoile c'est qu'elle est moche.
  \begin{tikzpicture}
    \pgfmathsetmacro{\a}{#1/4}
    \begin{scope}
      \clip [decorate, decoration={shape backgrounds, shape=star, shape sep=\a cm, shape size=\a cm}, shape border rotate=10] (0,0) -- (#1,0);
      \pgfmathparse{(1+2*\a)*#2-\a}
      \pgfmathsetmacro{\b}{\pgfmathresult}
      \fill [yellow] (-\a,-\a) rectangle (\b,+\a);
    \end{scope}
    \draw [decorate, decoration={shape backgrounds, shape=star, shape sep=\a cm, shape size=\a cm},shape border rotate=10] (0,0) -- (#1,0);
  \end{tikzpicture}
}

\newcommand{\comp}[2]{% #1 nom #2 taux
  \begin{tikzpicture}
    \draw [texte1] (0,0) rectangle (\complength,\compheight);
    \node at (0.5\complength,\compheight) [above] {#1};
    \fill [texte1] (0,0) rectangle (#2\complength,\compheight);
  \end{tikzpicture}
}

\renewcommand{\comp}[3][]{% #1 nomFichier #2 nom #3 taux
  \begin{tikzpicture}[baseline]
    \ifthenelse{\equal{#1}{}}{%
      \node [yshift=2.5pt] {\strut #2};
    }{%
      % \node [above=-0.3cm] {\cooltooltip{}{}{#2}{}{\includegraphics[height=1.1cm,width=1.7cm,keepaspectratio]{#1}}};
      \node [above=-0.3cm] {\includegraphics[height=1.1cm,width=1.7cm,keepaspectratio]{\picway#1}};
    }
    \draw [texte1] (-1cm,0) -- ++(-\compheight,0) arc (180:0:1cm+\compheight) -- ++(-\compheight,0) arc (0:180:1cm);

    \pgfmathparse{#3*1.8}\pgfmathsetmacro{\a}{\pgfmathresult}
    \fill [bon!#3!mauvais] (-1cm-\compheight,0) arc (180:180-\a:1cm+\compheight) -- ++(-\a:\compheight) arc (180-\a:180:1cm) -- cycle;
  \end{tikzpicture}
}

\def\epaisseurFleche{0.8}
\def\longueurFleche{4}
\newcommand{\compJauge}[3][]{% #1 nomFichier #2 nom #3 taux
  \begin{tikzpicture}[baseline,line join=round,scale=0.5]
    \fill [fond1, rounded corners]  (90:1.5) arc (90:0:1.5) -- ++(-0.5,0) arc (0:180:1) -- (-1.5,0) arc (180:90:1.5);

    %%%%

    \pgfmathparse{-#3*1.8+180}\pgfmathsetmacro{\a}{\pgfmathresult}
    
    \begin{scope}[scale=0.3, rotate=\a]
      \draw [postaction={draw,line width=2pt, color=texte1}] [line width=5pt, white,fill=white] (-\epaisseurFleche,0) -- (0,\epaisseurFleche) -- (\longueurFleche,0) -- (0,-\epaisseurFleche) -- cycle;

      \ifthenelse{\equal{#1}{}}{%
        \node [below] {\parbox{1.8cm}{\centering #2}};
      }{%
        % \node [above=-0.3cm] {\cooltooltip{}{}{#2}{}{\includegraphics[height=1.1cm,width=1.7cm,keepaspectratio]{#1}}};
        \node [below] {\includegraphics[height=1cm,width=1.8cm,keepaspectratio]{\picway#1}};
      }

    \end{scope}
  \end{tikzpicture}
}

\newenvironment{infopanneau}[1]{\begin{lrbox}{\panneaubox}%
    \begin{minipage}{\infopanneauwidth}
      \begin{center}
        {\color{texte1}\Large #1}
        
      }{
      \end{center}
    \end{minipage}\end{lrbox}%
  \begin{tikzpicture}[remember picture,overlay,texte1]

    \node [fill=fond1] (nouvnp) at ($(noeud-panneau.south)+(0,-\infopanneauskip)$) [below] {\usebox{\panneaubox}};

    \draw [fill,thick] (noeud-panneau.south) circle (0.02);    
    \draw  (noeud-panneau.south) -- (nouvnp.north);
    \draw [fill,thick] (nouvnp.north) circle (0.02);    
    
    \draw ($(nouvnp.north west)+(0,-0.1)$) -- (nouvnp.north west) -- (nouvnp.north east) -- ++(0,-0.1);
    \draw ($(nouvnp.south west)+(0,0.1)$) -- (nouvnp.south west) -- (nouvnp.south east) -- ++(0,0.1);

    \coordinate (noeud-panneau) at (nouvnp.south);
\end{tikzpicture}
}

\newlength{\formawidth}
\newcommand{\formation}[5]{%#1 année #2 intitulé #3 détails #4 mention #5 université
  \vspace{1ex}
  
  \begin{minipage}{1.5em}
    \rotatebox{90}{#5}
  \end{minipage}
  \setlength{\formawidth}{\linewidth}%
  \addtolength{\formawidth}{-2em}%
  \begin{minipage}{\formawidth}
    \begin{center}
      #1

      {\textbf{#2}}

      {\itshape #3}
      
      {\footnotesize #4}
    \end{center}
  \end{minipage}
  \vspace{1ex}
}

%% le paramétrage

\def\photo{}
\def\nomfamille{Doe}
\def\prenom{John}
\def\email{john.doe}\def\messagerie{jd.net}
\def\telephone{}

\newcommand{\setpicway}[1]{\def\picway{#1}}

\newcommand{\setphoto}[1]{\def\photo{#1}}
\newcommand{\setnom}[1]{\def\nomfamille{#1}}
\newcommand{\setprenom}[1]{\def\prenom{#1}}
\newcommand{\setmail}[2]{\def\email{#1}\def\messagerie{#2}}
\newcommand{\settel}[1]{\def\telephone{#1}}


\AtBeginDocument{%
  \geometry{top=\hbandeau cm,left=\lpanneau cm, right=0.5cm, bottom=0.5cm}

  \setlength{\infopanneauwidth}{\lpanneau cm}
  \addtolength{\infopanneauwidth}{-20pt}

  \sffamily
{%
  \fontsize{0.5cm}{0.7cm}\selectfont{}

  \begin{tikzpicture}[remember picture, overlay]
    \fill[fond1] (current page.north west) -- (current page.north east) -- coordinate [pos=0.4] (nom) ++(0,-\hbandeau)  -| coordinate (finBandeauGauche) cycle;
    \node (mail) [below=of nom] {};
    \node (tel) [below=12pt of mail] {};
    
    \node at (nom) [left=0.5cm] {\fontsize{1cm}{1.2cm}\selectfont{\textbf{\prenom{} \textsc{\nomfamille{}}}}};
    \node at (mail) [left=0.5cm] {\href{mailto:\email @\messagerie}{{\email{}@\messagerie{}}}};
    \node at (tel) [left=0.5cm] {\telephone{}};
    

    \fill[fond2] (finBandeauGauche) -- ++ (\lpanneau,0) |- (current page.south west);

    %% il se trouve qu'on pouvait calculer veclen(\lpanneau,\hbandeau)
    \pgfmathparse{sqrt(\lpanneau*\lpanneau+\hbandeau*\hbandeau)}
    \pgfmathsetmacro{\rayon}{\pgfmathresult}

    \pgfmathparse{sqrt((\rayon*\rayon-0.25*\lpanneau*\lpanneau))}
    \pgfmathsetmacro{\a}{\pgfmathresult}
    \path (current page.north west) ++(0.5*\lpanneau,-\a) coordinate (noeud-panneau);

    
    \fill[white] (current page.north west) circle (\rayon);
    \begin{scope}
      \clip (current page.north west) circle (\rayon);

      \path (current page.north west) ++(0.5*\rayon,-0.5*\rayon) node  {\includegraphics[width=\rayon cm]{\picway\photo}};
    \end{scope}
    
  \end{tikzpicture}%
}
}