%
% Copyright 2018 Luca Cappelletti
%
% Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{adigraph}
  [2018/02/16 v1.0 LaTeX package for creating augmenting directed graphs]

\RequirePackage{fp}
\RequirePackage{xparse}
\RequirePackage{xstring}
\RequirePackage{tikz}
\usetikzlibrary{calc}

\def\zero{0}

\makeatletter
\newcommand*{\rom}[1]{\expandafter\@slowromancap\romannumeral #1@}

\NewDocumentCommand{\storeCoordinate}{m m m}{
  \@namedef{adigraphNode#1#2}{#3}%
}
\NewDocumentCommand{\storeParsedEdge}{m m}{
  \@namedef{parsed#1#2}{1}%
}
\makeatother
\newcommand{\markEdgeAsParsed}[2]{%
  %
  % #1 -> First node of edge
  % #2 -> Second node of edge
  %
  \IfInteger{#1}{%
    \def\nodeA{\rom{#1}}%
  }{%
    \def\nodeA{#1}%
  }%
  \IfInteger{#2}{%
    \def\nodeB{\rom{#2}}%
  }{%
    \def\nodeB{#2}%
  }%
  \storeParsedEdge{\nodeA}{\nodeB}
}

\newcommand{\edgeWasParsed}[3]{%
  %
  % #1 -> Variable to store boolean result
  % #2 -> First node of edge
  % #3 -> Second node of edge
  %
  \IfInteger{#2}{%
    \def\nodeA{\rom{#2}}%
  }{%
    \def\nodeA{#2}%
  }%
  \IfInteger{#3}{%
    \def\nodeB{\rom{#3}}%
  }{%
    \def\nodeB{#3}%
  }%
  \ifcsname parsed\nodeA\nodeB\endcsname
    \FPset{#1}{1}
  \else%
    \FPset{#1}{0}
  \fi%
}

\newcommand{\saveNodeCoordinate}[3]{
  %
  % #1 -> Node name
  % #2 -> Coordinate, either X or Y
  % #3 -> Numeric value
  %
  \IfInteger{#1}{%
    \def\nodeName{\rom{#1}}%
  }{%
    \def\nodeName{#1}%
  }%
  \storeCoordinate{#2}{\nodeName}{#3}
}

\newcommand{\retrieveNodeCoordinate}[3]{
  %
  % #1 -> Variable to store coordinate into
  % #2 -> Node name
  % #3 -> Coordinate, either X or Y
  %
  \IfInteger{#2}{%
    \def\nodeName{\rom{#2}}%
  }{%
    \def\nodeName{#2}%
  }%

  \FPset{#1}{\csname adigraphNode#3\nodeName\endcsname}
}

\newcommand{\saveNodeCoordinates}[3]{
  %
  % #1 -> Node name
  % #2 -> X value
  % #3 -> Y value
  %
  \saveNodeCoordinate{#1}{X}{#2}%
  \saveNodeCoordinate{#1}{Y}{#3}%
}

\newcommand{\swp}[3]{
  %
  % #1 -> variable to assign
  % #2 -> First node name
  % #3 -> Second node name
  %
  \retrieveNodeCoordinate{\firstX}{#2}{X}
  \retrieveNodeCoordinate{\firstY}{#2}{Y}
  \retrieveNodeCoordinate{\secondX}{#3}{X}
  \retrieveNodeCoordinate{\secondY}{#3}{Y}
  %
  \FPsub{\xSub}{\secondX}{\firstX}
  \FPsub{\ySub}{\secondY}{\firstY}
  \FPset{#1}{1}
  \FPifgt{\xSub}{0}
    \FPset{#1}{3}
  \else%
    \FPset{#1}{4}
  \fi%
  %
  \FPifeq{\xSub}{0}
    \FPset{\xiszero}{1}
  \else%
    \FPset{\xiszero}{0}
  \fi%
  %
  \FPifgt{\ySub}{0}
    \FPset{\yispos}{1}
  \else%
    \FPset{\yispos}{4}
  \fi%
  %
  \FPadd{\xzeroypos}{\xiszero}{\yispos}
  %
  \FPifeq{\xzeroypos}{2}
    \FPset{#1}{1}
  \fi%
  \FPifeq{\xzeroypos}{5}
    \FPset{#1}{2}
  \fi%
}

\newcommand{\executeNodeBuilder}[5]{
  %
  % #1 is node name
  % #2 is x coordinate
  % #3 is y coordinate
  % #4 is label, that defaults to #1 when absent
  % #5 is color, that defaults to black when absent
  %
  \IfValueT{#2}{
    \IfValueTF{#4}{
      \def\nodeLabel{#4}
    }{
      \def\nodeLabel{#1}
    }
    \IfValueTF{#5}{
      \def\nodeColor{#5}
    }{
      \def\nodeColor{black}
    }
    \saveNodeCoordinates{#1}{#2}{#3}
    \node[vertex,\nodeColor] (#1) at (#2,#3) {$\nodeLabel$};
  }
}

\NewDocumentCommand{\nodeBuilder}{ > { \SplitArgument { 4 } { , } } m }{
  \executeNodeBuilder #1
}

\newcommand{\executeEdgeBuilder}[7]{
  %
  % #1 is first node
  % #2 is second node
  % #3 is weight of first edge, which defaults to 0
  % #4 is weight of second edge, which defaults to 0
  % #5 Should we color edge? Or first optional color
  % #6 Second optional color
  % #7 Label position on axis
  %
  \edgeWasParsed{\isParsed}{#1}{#2}
  \FPifeq{\isParsed}{0}
    \markEdgeAsParsed{#1}{#2}
    \markEdgeAsParsed{#2}{#1}
  \fi%
  \IfValueT{#2}{
    \def\firstNode{#1}
    \def\secondNode{#2}
    \IfValueTF{#3}{
      \def\firstEdgeWeight{#3}
    }{
      \def\firstEdgeWeight{0}
    }
    \IfValueTF{#4}{
      \def\secondEdgeWeight{#4}
    }{
      \def\secondEdgeWeight{0}
    }
    \IfValueTF{#7}{
      \def\labelPosition{#7}
    }{
      \def\labelPosition{midway}
    }
    \IfValueTF{#6}{
      \IfValueTF{#5}{
        \def\firstEdgeColor{#5}
        \def\secondEdgeColor{#6}
      }{
        \def\firstEdgeColor{black}
        \def\secondEdgeColor{black}
      }
    }{
      \IfValueTF{#5}{
        \def\firstEdgeColor{red}
        \def\secondEdgeColor{blue}
      }{
        \def\firstEdgeColor{black}
        \def\secondEdgeColor{black}
      }
    }

    \FPadd{\weightSum}{\firstEdgeWeight}{\secondEdgeWeight}
    \FPmul{\weightMul}{\firstEdgeWeight}{\secondEdgeWeight}
    \FPadd{\firstZero}{\weightMul}{\firstEdgeWeight}
    \FPadd{\secondZero}{\weightMul}{\secondEdgeWeight}

    \swp{\firstWeightPositionFlag}{#1}{#2}
    \swp{\secondWeightPositionFlag}{#2}{#1}

    \FPifeq{\firstWeightPositionFlag}{1}
      \def\firstWeightPosition{left}
    \fi
    \FPifeq{\firstWeightPositionFlag}{2}
      \def\firstWeightPosition{right}
    \fi
    \FPifeq{\firstWeightPositionFlag}{3}
      \def\firstWeightPosition{above}
    \fi
    \FPifeq{\firstWeightPositionFlag}{4}
      \def\firstWeightPosition{below}
    \fi

    \FPifeq{\secondWeightPositionFlag}{1}
      \def\secondWeightPosition{left}
    \fi
    \FPifeq{\secondWeightPositionFlag}{2}
      \def\secondWeightPosition{right}
    \fi
    \FPifeq{\secondWeightPositionFlag}{3}
      \def\secondWeightPosition{above}
    \fi
    \FPifeq{\secondWeightPositionFlag}{4}
      \def\secondWeightPosition{below}
    \fi
    \FPifeq{\isParsed}{1}
    \else
    \FPifeq{\weightSum}{0}
      % No edge has weights, so we draw both with no labels.
      \draw[edge,\firstEdgeColor] (\firstNode) to [bend right=20] (\secondNode);
      \draw[edge,\secondEdgeColor] (\secondNode) to [bend right=20] (\firstNode);
    \else
    \FPifeq{\weightSum}{0}
      \draw[edge,\firstEdgeColor] (\firstNode) to node[\labelPosition, \secondWeightPosition] {$\firstEdgeWeight$}(\secondNode);
    \else
    \FPifeq{\firstZero}{0}
      \draw[edge,\secondEdgeColor] (\secondNode) to node[\labelPosition, \firstWeightPosition] {$\secondEdgeWeight$}(\firstNode);
    \FPifeq{\weightMul}{0}
    \else
      \draw[edge,\firstEdgeColor] (\firstNode) to [bend right=20] node[\labelPosition, \firstWeightPosition] {$\firstEdgeWeight$}(\secondNode);
      \draw[edge,\secondEdgeColor] (\secondNode) to [bend right=20] node[\labelPosition, \secondWeightPosition] {$\secondEdgeWeight$}(\firstNode);
    \fi
  }
}

\NewDocumentCommand{\edgeBuilder}{> { \SplitArgument { 6 } { , } } m}{
  \executeEdgeBuilder #1
}

\NewDocumentCommand{\calculateOrientation}{m m m}{
  \retrieveNodeCoordinate{\firstX}{#2}{X}
  \retrieveNodeCoordinate{\firstY}{#2}{Y}
  \retrieveNodeCoordinate{\secondX}{#3}{X}
  \retrieveNodeCoordinate{\secondY}{#3}{Y}
  \FPsub{\deltaY}{\secondY}{\firstY}
  \FPsub{\deltaX}{\secondX}{\firstX}
  \FPifeq{\deltaX}{0}
    \Fpset{#1}{90}
  \else
    \FPdiv{\deltaDiv}{\deltaY}{\deltaX}
    \FParctan{\arcResult}{\deltaDiv}
    \FPmul{#1}{\arcResult}{57.2958}
  \fi
}

\NewDocumentCommand{\executeCutBuilder}{m m}{
  \IfValueT{#2}{
    \calculateOrientation{\orientation}{#1}{#2}
    \draw[dashed,red,rotate=\orientation] ([yshift=10pt]$(#1)!0.7!(#2)$) -- ([yshift=-10pt]$(#1)!0.7!(#2)$);
    % %\node[vertex] (d) at (-3,-3) {$ARIAN$};
  }
}

\NewDocumentCommand{\cutBuilder}{> { \SplitArgument { 1 } { , } } m}{
  \executeCutBuilder #1
}

\NewDocumentCommand{\smartGraph}{> { \SplitList { ; } } m > { \SplitList { ; } } m > { \SplitList { ; } } m > { \SplitList { ; } } m}{
  %
  % #1 -> Vertices
  % #2 -> Edges
  % #3 -> Modified edges
  % #4 -> Cuts
  %
  \begin{tikzpicture}
    \tikzset{
      vertex/.style={circle,draw,minimum size=2em},
      edge/.style={->,> = latex}
    }

    % vertices
    \ProcessList {#1} {\nodeBuilder}

    %edges
    \ProcessList {#3} {\edgeBuilder}
    \ProcessList {#2} {\edgeBuilder}

    %cuts
    \ProcessList {#4} {\cutBuilder}
  \end{tikzpicture}
}

\NewDocumentCommand{\NewAdigraph}{m m m}{
  %
  % #1 -> Variable to assign to as command
  % #2 -> Nodes
  % #3 -> Edges
  %
  \expandafter\newcommand\csname #1\endcsname[2][]{
    %
    % ##1 -> Modified edges
    %
    \smartGraph{#2}{#3}{##2}{##1}
  }%
}

\endinput
%%
%% End of file `adigraph.sty'.