package bk;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JTree;

import codeline.AbstractForLoop;
import codeline.CodeLine;
import codeline.DeclareVariable;
import codeline.ForEachLoop;
import codeline.FunctionCall;
import codeline.FunctionDefinition;
import codeline.IfLoop;
import codeline.SetVariable;
import graph.LayeredGraphEdge;
import graph.LayeredGraphNode;
import processor.ControlFlow;
import processor.Memory;
import processor.PseudoCodeNode;
import processor.Memory.ReadOnlyMemory;
import processor.Memory.Visibility;

/**
 * The stage of the BK node placement algorithm where the blocks are computed.
 * @author kolja
 *
 */
public class VerticalAligment {

    /**
     * creates the pseudo code for calculating the block graph (stage vertical alignment)
     * @param tree the tree where the code will be displayed
     * @param calcLayout a pointer to a function that calculates an extremal layout (for recursive calls)
     * @return the root of the code
     */
    public static PseudoCodeNode calculateBlockGraph( JTree tree, PseudoCodeNode calcLayout ) {
        String[] vars = { "graph", "L", "r", "neighbors", "layout", "m", "i", "k", "mids", "n" };

        PseudoCodeNode root = new PseudoCodeNode( "function calculateBlockGraph( layout, graph )", vars, tree, new FunctionDefinition( new String[]{ "layout", "graph" } ) ) {
            private static final long serialVersionUID = -839881272535733474L;

            @Override
            public String getDebugOutput( Memory m )
            {
                if( m.isSomewhereDefined( "n", Visibility.LOCAL ) )
                    m.<LayeredGraphNode>read( "n", Visibility.LOCAL).setSelected( LayoutType.fromString( m.read( "layout", Visibility.LOCAL ) ) );
                return super.getDebugOutput( m );
            }
        };
        root.add( new PseudoCodeNode( "L = graph.getContainedLayers();", vars, tree, new DeclareVariable<ArrayList<ArrayList<LayeredGraphNode>>>( "L" ) {
            @Override
            protected ArrayList<ArrayList<LayeredGraphNode>> value(ReadOnlyMemory m) {
                return m.<LayeredGraphNode>read( "graph", Visibility.LOCAL ).getContainedLayers();
            }
        } ) );
        PseudoCodeNode layerLoop = new PseudoCodeNode( "for i=layout.contains('LOWER') ? 0 : |L|-1 to layout.contains('LOWER') ? |L|-1 : 0 do", vars, tree, new AbstractForLoop<Integer>( "i" ) {
            @Override
            protected Integer intialize(ReadOnlyMemory m) {
                if( m.<String>read( "layout", Visibility.LOCAL ).contains( "LOWER" ) )
                    return 0;
                return m.<ArrayList<ArrayList<LayeredGraphNode>>>read( "L", Visibility.LOCAL ).size() - 1;
            }
            @Override
            protected Integer step(ReadOnlyMemory m) {
                if( m.<String>read( "layout", Visibility.LOCAL ).contains( "LOWER" ) )
                    return m.<Integer>read( "i", Visibility.LOCAL ) + 1;
                return m.<Integer>read( "i", Visibility.LOCAL ) - 1;
            }
            @Override
            protected boolean condition(ReadOnlyMemory m) {
                if( m.<String>read( "layout", Visibility.LOCAL ).contains( "LOWER" ) )
                    return m.<Integer>read( "i", Visibility.LOCAL ) <= m.<ArrayList<ArrayList<LayeredGraphNode>>>read( "L", Visibility.LOCAL ).size() - 1;
                return m.<Integer>read( "i", Visibility.LOCAL ) >= 0;
            }
        });
        root.add( layerLoop );
        layerLoop.add( new PseudoCodeNode( "r = layout.contains('RIGHT') ? -1 : INFINITY;", vars, tree, new DeclareVariable<Double>( "r" ) {
            @Override
            protected Double value(ReadOnlyMemory m) {
                if( m.<String>read( "layout", Visibility.LOCAL ).contains( "RIGHT" ) )
                    return -1.0;
                return Double.POSITIVE_INFINITY;
            }
        } ) );
        PseudoCodeNode nodeLoop = new PseudoCodeNode( "for k=layout.contains('RIGHT') ? 0 : |L[i]|-1 to layout.contains('RIGHT') ? |L[i]|-1 : 0 do", vars, tree, new AbstractForLoop<Integer>( "k" ) {
            @Override
            protected Integer intialize( ReadOnlyMemory m) {
                if( m.<String>read( "layout", Visibility.LOCAL ).contains( "RIGHT" ) )
                    return 0;
                return m.<ArrayList<ArrayList<LayeredGraphNode>>>read( "L", Visibility.LOCAL ).get( m.read( "i", Visibility.LOCAL ) ).size() - 1;
            }
            @Override
            protected Integer step( ReadOnlyMemory m) {
                if( m.<String>read( "layout", Visibility.LOCAL ).contains( "RIGHT" ) )
                    return m.<Integer>read( "k", Visibility.LOCAL ) + 1;
                return m.<Integer>read( "k", Visibility.LOCAL ) - 1;
            }
            @Override
            protected boolean condition( ReadOnlyMemory m) {
                if( m.<String>read( "layout", Visibility.LOCAL ).contains( "RIGHT" ) )
                    return m.<Integer>read( "k", Visibility.LOCAL ) <= m.<ArrayList<ArrayList<LayeredGraphNode>>>read( "L", Visibility.LOCAL ).get( m.read( "i", Visibility.LOCAL ) ).size() - 1;
                return m.<Integer>read( "k", Visibility.LOCAL ) >= 0;
            }
        });
        nodeLoop.add( new PseudoCodeNode( "n = L[i][k];", vars, tree, new DeclareVariable<LayeredGraphNode>( "n" ) {
            @Override
            protected LayeredGraphNode value(ReadOnlyMemory m) {
                return m.<ArrayList<ArrayList<LayeredGraphNode>>>read( "L", Visibility.LOCAL ).get( m.read( "i", Visibility.LOCAL ) ).get( m.read( "k", Visibility.LOCAL ) );
            }
        }));
        PseudoCodeNode ifNode = new PseudoCodeNode( "if n has subgraph then", vars, tree, new IfLoop() {
            @Override
            protected boolean condition(ReadOnlyMemory m) {
                return m.<LayeredGraphNode>read( "n", Visibility.LOCAL ).getContainedNodes().size() > 0;
            }
        } );
        nodeLoop.add( ifNode );
        ifNode.add( new PseudoCodeNode( "call calcLayout( layout, n );", vars, tree, new FunctionCall( calcLayout, new String[]{ "layout", "n" } ) ) );
        nodeLoop.add( new PseudoCodeNode( "neighbors = layout.contains('LOWER') ? predecessors(n) : successors(n)", vars, tree, new DeclareVariable<ArrayList<LayeredGraphNode>>( "neighbors" ) {
            @Override
            protected ArrayList<LayeredGraphNode> value(ReadOnlyMemory m) {
                ArrayList<LayeredGraphEdge> list = m.<LayeredGraphNode>read( "n", Visibility.LOCAL ).getSortedOutgoingEdges();
                if( m.<String>read( "layout", Visibility.LOCAL ).contains( "LOWER" ) )
                    list = m.<LayeredGraphNode>read( "n", Visibility.LOCAL ).getSortedIncomingEdges();
                ArrayList<LayeredGraphNode> result = new ArrayList<LayeredGraphNode>();
                for( LayeredGraphEdge e : list )
                {
                    if( m.<String>read( "layout", Visibility.LOCAL ).contains( "LOWER" ) )
                        result.add( e.getSources().get( 0 ) );
                    else
                        result.add( e.getTargets().get( 0 ) );
                }
                return result;
            }
        } ) );
        layerLoop.add( nodeLoop );
        nodeLoop.add( new PseudoCodeNode( "neighbors = layout.contains('RIGHT') ? neighbors : reverse( neighbors )", vars, tree, new SetVariable<ArrayList<LayeredGraphNode>>( "neighbors" ) {
            @Override
            protected ArrayList<LayeredGraphNode> value(ReadOnlyMemory m) {
                ArrayList<LayeredGraphNode> list = m.read( "neighbors", Visibility.LOCAL );
                if( m.<String>read( "layout", Visibility.LOCAL ).contains( "RIGHT" ) )
                    return list;
                else
                {
                    ArrayList<LayeredGraphNode> result = new ArrayList<LayeredGraphNode>();
                    for( int i = list.size() - 1; i >= 0; i-- )
                        result.add( list.get( i ) );
                    return result;
                }
            }
        } ) );
        PseudoCodeNode ifNeighbors = new PseudoCodeNode( "if |neighbors| > 0 then", vars, tree, new IfLoop() {
            @Override
            protected boolean condition( ReadOnlyMemory m) {
                return m.<ArrayList<LayeredGraphNode>>read( "neighbors", Visibility.LOCAL ).size() > 0;
            }
        });
        nodeLoop.add( ifNeighbors );
        ifNeighbors.add( new PseudoCodeNode( "mids = [roundDown((|neighbors|-1)/2),roundUp((|neighbors|-1)/2)]", vars, tree, new DeclareVariable<ArrayList<Integer>>( "mids" ) {
            @Override
            protected ArrayList<Integer> value(ReadOnlyMemory m) {
                int size = m.<ArrayList<LayeredGraphNode>>read( "neighbors", Visibility.LOCAL ).size() - 1;
                int m1 = size / 2, m2 = (int)(size / 2.0 + 0.5);
                ArrayList<Integer> list = new ArrayList<Integer>();
                list.add( m1 );
                if( m1 != m2 )
                    list.add( m2 );
                return list;
            }
        } ) );
        PseudoCodeNode midLoop = new PseudoCodeNode( "foreach m in mids do", vars, tree, new ForEachLoop<Integer>( "m" ) {
            @Override
            protected List<Integer> list(ReadOnlyMemory m) {
                return m.read( "mids", Visibility.LOCAL );
            }
        } );
        ifNeighbors.add( midLoop );
        PseudoCodeNode ifAlign = new PseudoCodeNode( "if align[n] == n then", vars, tree, new IfLoop() {
            @Override
            protected boolean condition(ReadOnlyMemory m) {
                LayeredGraphNode n = m.read( "n", Visibility.LOCAL );
                return n.getAlign( LayoutType.fromString( m.read( "layout", Visibility.LOCAL ) ) ) == n;
            }
        });
        midLoop.add( ifAlign );
        PseudoCodeNode ifMarked = new PseudoCodeNode( "if (neighbors[m],n) not conflicted and ((r < pos(neighbors[m]) and layout.contains('RIGHT')) or (r > pos(neighbors[m]) and layout.contains('LEFT'))) then", vars, tree, new IfLoop() {
            @Override
            protected boolean condition(ReadOnlyMemory m) {
                LayeredGraphEdge e = m.<LayeredGraphNode>read( "graph", Visibility.LOCAL ).findEdgeBetween(
                        m.<ArrayList<LayeredGraphNode>>read( "neighbors", Visibility.LOCAL ).get( m.read( "m", Visibility.LOCAL ) ),
                        m.read( "n", Visibility.LOCAL ) );
                if( e == null )
                    e = m.<LayeredGraphNode>read( "graph", Visibility.LOCAL ).findEdgeBetween( m.read( "n", Visibility.LOCAL ),
                            m.<ArrayList<LayeredGraphNode>>read( "neighbors", Visibility.LOCAL ).get( m.read( "m", Visibility.LOCAL ) ) );
                ArrayList<LayeredGraphNode> layerBefore;
                if( m.<String>read( "layout", Visibility.LOCAL ).contains( "LOWER" ) )
                    layerBefore = m.<ArrayList<ArrayList<LayeredGraphNode>>>read( "L", Visibility.LOCAL ).get( m.<Integer>read( "i", Visibility.LOCAL ) - 1 );
                else
                    layerBefore = m.<ArrayList<ArrayList<LayeredGraphNode>>>read( "L", Visibility.LOCAL ).get( m.<Integer>read( "i", Visibility.LOCAL ) + 1 );
                int posU = layerBefore.indexOf( m.<ArrayList<LayeredGraphNode>>read( "neighbors", Visibility.LOCAL ).get( m.read( "m", Visibility.LOCAL ) ) );
                return !e.isConflicted( LayoutType.fromString( m.read( "layout", Visibility.LOCAL ) ) ) &&
                        ( ( m.<Double>read( "r", Visibility.LOCAL ) < posU && m.<String>read( "layout", Visibility.LOCAL ).contains( "RIGHT" ) ) ||
                                ( m.<Double>read( "r", Visibility.LOCAL ) > posU && m.<String>read( "layout", Visibility.LOCAL ).contains( "LEFT" ) ) );
            }
        });
        ifAlign.add( ifMarked );
        ifMarked.add( new PseudoCodeNode( "align[neighbors[m]] = n;", vars, tree, new CodeLine() {
            @Override
            public ControlFlow runForward(Memory m) {
                LayeredGraphNode u = m.<ArrayList<LayeredGraphNode>>read( "neighbors", Visibility.LOCAL ).get( m.read( "m", Visibility.LOCAL ) );
                LayeredGraphNode v = m.read( "n", Visibility.LOCAL );
                LayeredGraphNode old = u.getAlign( LayoutType.fromString( m.read( "layout", Visibility.LOCAL ) ) );
                u.setAlign( v, LayoutType.fromString( m.read( "layout", Visibility.LOCAL ) ) );
                actions.push( (Memory mem) -> {
                    u.setAlign( old, LayoutType.fromString( mem.read( "layout", Visibility.LOCAL ) ) );
                });
                return new ControlFlow( ControlFlow.STEP_OVER );
            }
        }) );
        ifMarked.add( new PseudoCodeNode( "root[n] = root[neighbors[m]];", vars, tree, new CodeLine() {
            @Override
            public ControlFlow runForward(Memory m) {
                LayeredGraphNode u = m.<ArrayList<LayeredGraphNode>>read( "neighbors", Visibility.LOCAL ).get( m.read( "m", Visibility.LOCAL ) );
                LayeredGraphNode v = m.read( "n", Visibility.LOCAL );
                LayeredGraphNode old = v.getRoot( LayoutType.fromString( m.read( "layout", Visibility.LOCAL ) ) );
                v.setRoot( u.getRoot( LayoutType.fromString( m.read( "layout", Visibility.LOCAL ) ) ), LayoutType.fromString( m.read( "layout", Visibility.LOCAL ) ) );
                actions.push( (Memory mem) -> {
                    v.setRoot( old, LayoutType.fromString( m.read( "layout", Visibility.LOCAL ) ) );
                });
                return new ControlFlow( ControlFlow.STEP_OVER );
            }
        }) );
        ifMarked.add( new PseudoCodeNode( "align[n] = root[n];", vars, tree, new CodeLine() {
            @Override
            public ControlFlow runForward(Memory m) {
                LayeredGraphNode v = m.read( "n", Visibility.LOCAL );
                LayeredGraphNode old = v.getAlign( LayoutType.fromString( m.read( "layout", Visibility.LOCAL ) ) );
                v.setAlign( v.getRoot( LayoutType.fromString( m.read( "layout", Visibility.LOCAL ) ) ), LayoutType.fromString( m.read( "layout", Visibility.LOCAL ) ) );
                actions.push( (Memory mem) -> {
                    v.setAlign( old, LayoutType.fromString( m.read( "layout", Visibility.LOCAL ) ) );
                });
                return new ControlFlow( ControlFlow.STEP_OVER );
            }
        }) );
        ifMarked.add( new PseudoCodeNode( "r = pos(neighbors[m]);", vars, tree, new SetVariable<Double>( "r" ) {
            @Override
            protected Double value(ReadOnlyMemory m) {
                ArrayList<LayeredGraphNode> layerBefore;
                if( m.<String>read( "layout", Visibility.LOCAL ).contains( "LOWER" ) )
                    layerBefore = m.<ArrayList<ArrayList<LayeredGraphNode>>>read( "L", Visibility.LOCAL ).get( m.<Integer>read( "i", Visibility.LOCAL ) - 1 );
                else
                    layerBefore = m.<ArrayList<ArrayList<LayeredGraphNode>>>read( "L", Visibility.LOCAL ).get( m.<Integer>read( "i", Visibility.LOCAL ) + 1 );
                LayeredGraphNode u = m.<ArrayList<LayeredGraphNode>>read( "neighbors", Visibility.LOCAL ).get( m.read( "m", Visibility.LOCAL ) );
                return (double)layerBefore.indexOf( u );
            }
        }) );
        return root;
    }
}
