\newif\iftb@draft
\tb@draftfalse
\DeclareOption{draft}{\tb@drafttrue}
\newif\iftb@final
\tb@finalfalse
\DeclareOption{final}{\tb@finaltrue}
\ProcessOptions

\RequirePackage{xcolor}
\RequirePackage[leftbars]{changebar}
\RequirePackage{xspace}
\RequirePackage{tikz}%TODO rendre optionnel
\RequirePackage{ifthen}

\newcommand*\@version{version to define}
\newcommand*\version[1]{\renewcommand*{\@version}{#1}}
\newcommand*\showversion{\@version}


\iftb@draft
  \RequirePackage{draftwatermark}%
  \SetWatermarkText{DRAFT v\showversion}%
  \SetWatermarkScale{0.8}%
\else
\fi

\providecommand{\added}{}
\providecommand{\deleted}{}

\newboolean{tb@boolean@withDeleted}
\setboolean{tb@boolean@withDeleted}{true}
\newcommand*{\withoutDeleted}{\setboolean{tb@boolean@withDeleted}{false}}

\newboolean{tb@boolean@withAdded}
\setboolean{tb@boolean@withAdded}{true}
\newcommand*{\withoutAdded}{\setboolean{tb@boolean@withAdded}{false}}

\newboolean{tb@boolean@withTodo}
\setboolean{tb@boolean@withTodo}{true}
\newcommand*{\withoutTodo}{\setboolean{tb@boolean@withTodo}{false}}

\newboolean{tb@boolean@withChangeBar}
\setboolean{tb@boolean@withChangeBar}{true}

\newcommand{\todo}[1]{
  \ifthenelse{\boolean{tb@boolean@withTodo}}{%
    \textcolor{red}{\textbf{[TODO: #1]}}
  }{}
}

\setcounter{changebargrey}{0}

\newcommand*{\addedcolor}{blue}
\newcommand*{\deletedcolor}{gray}
\RequirePackage[normalem]{ulem} % def de sout
%\RequirePackage{soul} % def de st
\newcommand*{\strike}[1]{\sout{#1}}
\newcommand*{\bof}[1]{{\color{midyellow}\uwave{#1}}}

\definecolor{midyellow}{rgb}{0.75,0.75,0}

\newcommand*{\addedstyle}{\color{\addedcolor}}
\newcommand*{\deletedstyle}{\color{\deletedcolor}}

% For outside tikz
\newcommand*{\changetb@m@b}{\cbstart}
\newcommand*{\changetb@m@e}{\cbend\xspace}
% For inside tikz
\newcommand*{\changetb@b}{}
\newcommand*{\changetb@e}{\xspace}

\newcommand*{\changetb@begin}{\changetb@m@b}
\newcommand*{\changetb@end}{\changetb@m@e}

\newcommand*{\withoutChangeBar}{%
  \setboolean{tb@boolean@withChangeBar}{false}%
  \renewcommand*{\changetb@begin}{}%
  \renewcommand*{\changetb@end}{}%
}

\renewcommand{\added}[1]{%
  \ifthenelse{\boolean{tb@boolean@withAdded}}{%
    \changetb@begin{\addedstyle #1}\changetb@end%
  }{#1}}
\newcommand{\addedWithoutCB}[1]{\changetb@b{\addedstyle #1}\changetb@e}
\newenvironment{addedenv}{%
  \ifthenelse{\boolean{tb@boolean@withAdded}}{%
    \captionnamefont{\addedstyle}\captiontitlefont{\addedstyle}% 
    \cbstart\bgroup\addedstyle%
  }{\bgroup}}{%
  \ifthenelse{\boolean{tb@boolean@withAdded}}{%
    \egroup\cbend\captionnamefont{}\captiontitlefont{}%
  }{\egroup}}
\newenvironment{scopeadded}[1][]{%
  \ifthenelse{\boolean{tb@boolean@withAdded}}{%
    \begin{scope}[color=\addedcolor,font=\bfseries\footnotesize,thick,#1]%
  }{\begin{scope}}%
}{%
  \end{scope}%
}

\renewcommand{\deleted}[1]{%
  \ifthenelse{\boolean{tb@boolean@withDeleted}}{%
    \changetb@begin{\deletedstyle\strike{#1}}\changetb@end%
  }{}}
\newcommand{\deletedCom}[2]{\deleted{#1}\footnote{#2}}

\newcommand{\replacedTextNorm}[1]{%
  \ifthenelse{\boolean{tb@boolean@withDeleted}}{%
    {\deletedstyle\strike{#1}}%
  }{}}
\newcommand{\replacedTextTikz}[1]{\tikznote{replaced #1}}
\newcommand{\replacedText}[1]{\replacedTextNorm{#1}}
\newcommand{\replaced}[2]{%
  \ifthenelse{\boolean{tb@boolean@withAdded}}{%
    \changetb@begin{{\addedstyle #2}\replacedText{#1}}\changetb@end%
  }{#2}}
\newcommand{\replacedCom}[3]{\replaced{#1}{#2}\footnote{#3}}

%\newcommand{\replacedF}[2]{\cbstart{\addedstyle #2}\footnote{\color{gray}replaced #1}\cbend\xspace}
%\newcommand{\replacedN}[2]{{\addedstyle #2\tikznote{replaced #1}}}


\newcommand{\maybeadd}[1]{\textcolor{midyellow}{#1}}
\newcommand{\maybedelete}[1]{\textcolor{midyellow}{\strike{#1}}}


\newenvironment{todos}{%
  \bgroup\color{red}\textbf{TODO:}%
  \begin{itemize}%
}{%
  \end{itemize}\egroup%
}

\newcounter{tikznote}

% taken from pgf (modified to expand the list)
\def\foreach#1in#2{%
  \def\pgffor@var{#1}%
  \edef\pgffor@values{#2, \noexpand\pgffor@stop,}%
  \ifx\pgffor@values\pgffor@emptyvalues
    \def\pgffor@values{\pgffor@stop,}%
  \fi%
  \let\pgffor@body\@empty%
  \global\pgffor@continuetrue%
  \pgffor@collectbody}

% lists
\newcommand{\add@to@list}[1]{% 1 -> elem
  \ifx\my@list@note\@empty\relax%
    \global\edef\my@list@note{#1}%
  \else%
    \global\edef\my@list@note{\my@list@note,#1}%
  \fi%
}

\newcommand{\tikznote}[1]{%1 -> note
  \stepcounter{tikznote}%
  \add@to@list{#1}%
  \footnotemark[\value{tikznote}]%
}

\newenvironment{mytikzpicture}[1][]{
  \setcounter{tikznote}{0}
  \def\my@list@note{}
  \begin{tikzpicture}[#1]
}{
  \end{tikzpicture}
  \ifx\my@list@note\@empty\relax%
  \else%
    \medskip%
    \footnoterule%
    {\footnotesize%
      \setcounter{tikznote}{1}%
      \@for\@unevar:=\my@list@note\do{\footnotemark[\value{tikznote}]\@unevar\hfill\stepcounter{tikznote}}%
    }%
  \fi%
}
\newenvironment{tikzpicturecb}[1][]{
  \renewcommand*{\changetb@begin}{\changetb@b}%
  \renewcommand*{\changetb@end}{\changetb@e}%
  \renewcommand{\replacedText}[1]{\replacedTextTikz{##1}}%
  \ifthenelse{\boolean{tb@boolean@withChangeBar}}{%
    \cbstart%
  }{}%
  \begin{mytikzpicture}[#1]
}{
  \end{mytikzpicture}
  \ifthenelse{\boolean{tb@boolean@withChangeBar}}{%
    \cbend%
  }{}%
  \renewcommand*{\changetb@begin}{\changetb@m@b}%
  \renewcommand*{\changetb@end}{\changetb@m@e}%
  \renewcommand{\replacedText}[1]{\replacedTextNorm{##1}}%
}
\iftb@final
  \withoutDeleted%
  \withoutAdded%
  \withoutTodo%
  \withoutChangeBar%
  \def\changes#1{\comment}%
  \def\endchanges{\endcomment}%
  \def\todos{\comment}%
  \def\endtodos{\endcomment}%
\else%
  \newenvironment{changes}[1]{%
    \bgroup\addedstyle Changed in version #1:\begin{itemize}%
    }{%
    \end{itemize}\egroup%
  }%
\fi


