package nloc;

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

public class Nloc {
  public static void main(String[] args) {
    Channel chan0 = new Channel(6,6);
    Channel chan1 = new Channel(8,8);
    Channel chan2 = new Channel(8,8);
    Channel chan3 = new Channel(11,12);
    Channel chan4 = new Channel(19,21);
    Channel chan5 = new Channel(5,5);
    Channel chan6 = new Channel(8,8);
    Channel chan7 = new Channel(9,10);
    Channel chan8 = new Channel(25,28);

    Module m0 = new Module("m0", 22,10);
    Module m1 = new Module("m1", 10,10);

    Pump p0 = new Pump();
    Sink s0 = new Sink();


    p0.addChild(chan0);
    chan0.addChild(chan1);
    chan1.addChild(chan3);
    chan1.addChild(chan2);
    chan2.addChild(chan4);
    chan3.addChild(chan5);
    chan4.addChild(chan7);
    chan7.addChild(m1);
    m1.addChild(chan8);
    chan8.addChild(s0);

    chan5.addChild(m0);
    m0.addChild(chan6);
    chan6.addChild(chan7);

    getAllPaths(p0);
    //List<Path> pl = new ArrayList<Path>();
    //printAllPaths(p0,new Path(), pl);
    //System.out.println(pl);
    //for (Path p : pl) {
    //  for (Channel ch : p.getChanlist()) {
    //    System.out.println("ID: " + ch.getID());
    //  }
    //}


   // for(Channel ch : chan0.getChildren()) {
   //   System.out.println("ID: " + ch.getID() + " pSteps: " + ch.getPSteps());
   // }
   // for(Channel ch : chan1.getChildren()) {
   //   System.out.println("ID: " + ch.getID() + " pSteps: " + ch.getPSteps());
   // }
   // for(Channel ch : chan2.getChildren()) {
   //   System.out.println("ID: " + ch.getID() + " pSteps: " + ch.getPSteps());
 // }
  }
  // list nodes_to_visit = {root};
  // while( nodes_to_visit isn't empty ) {
  //     currentnode = nodes_to_visit.take_first();
  //       nodes_to_visit.prepend( currentnode.children );
  //         //do something
  //         }
  //
//stack.push(root)
//while !stack.isEmpty() do
//    node = stack.pop()
//    for each node.childNodes do
//        stack.push(stack)
//    endfor
//    // …
//endwhile

  public static void getAllPaths(Channel chan) {
    Path p = new Path();
    List<Path> pl = new ArrayList<Path>();
    Stack<Channel> chanStack = new Stack<Channel>();

    chanStack.push(chan);
    while(!chanStack.empty()) {
      Channel temp = chanStack.pop();
      System.out.println("path add: " + temp.getID());
      p.add(temp);
      if (temp.getChildren().size() == 0) {
        System.out.println(chanStack);
        for (Channel c : p.getChanlist()) {
          System.out.println( c + ": " + c.getID());
        }
      }
      for (Channel chl : temp.getChildren()) {
        System.out.println("stack push " + chl.getID());
        System.out.println(chanStack);
        chanStack.push(chl);
      }
    }
  }
  
  public static void printAllPaths(Channel chan, Path path, List<Path> pathlist) {
    path.add(chan);
    if (chan.getChildren().size() == 0) { 
      System.out.println("Here" );
      for (Channel ch : path.getChanlist()) {
        System.out.print(ch.getID() + " ; ");
      }
      System.out.println( "");
      pathlist.add(path);
    } else {
      for (Channel ch : chan.getChildren()) {
        printAllPaths(ch, new Path(path), pathlist);
      }
    }
  }

  public static void printPath(Channel chan) {
    System.out.println("ID: " + chan.getID() + " pSteps: " + chan.getPSteps());
    if (chan.getChildren().size() != 0) {
      for (Channel ch : chan.getChildren()) {
        printPath(ch);
      }
    }
  }

}