/*
 *  Team 1
 *  CS575: Software Design
 *  Project Phase I
 *  Imlicit Invocation style
 *  IIMaster.java
 */

import java.util.Vector;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PipedReader;
import java.io.PipedWriter;

/**
 *  <p>Master coordinator class for the Imlicit Invocation KWIC system.
 *  
 *  <p>Orchestrates the different worker threads of the Implicit Invocation KWIC system.</p>
 *  @author Diana Tetelman
 */

public class IIMaster
{
    static public PipedWriter shifterWriteMessagePipe;
    static public PipedReader shifterReadMessagePipe;

    static public PipedWriter sorterWriteMessagePipe;
    static public PipedReader sorterReadMessagePipe;
  
    static public PipedWriter outputWriteMessagePipe;
    static public PipedReader outputReadMessagePipe;
    
    static public SentenceCollection inputCollection;
    static public SentenceCollection shiftedCollection;
    
    static public void main(String[] args) throws KWICException
    {
        Vector files = new Vector(); //args;
        // command line parsing + usage clause
	int optind;   // option index
	if (args.length < 1) {
	    System.out.println("Usage: java PNFMain [infile1] [infile2] ... [infilen]");
	    System.exit(1);
	} else {
	    for (optind=0; optind < args.length; optind++) {
		files.add (args[optind]);
	    }
	}
        
        try{
            shifterWriteMessagePipe = new PipedWriter();
            shifterReadMessagePipe = new PipedReader(shifterWriteMessagePipe);

            sorterWriteMessagePipe = new PipedWriter();
            sorterReadMessagePipe = new PipedReader(sorterWriteMessagePipe);
            
            outputWriteMessagePipe = new PipedWriter();
            outputReadMessagePipe = new PipedReader(outputWriteMessagePipe);

            inputCollection = new SentenceCollection();
            shiftedCollection = new SentenceCollection();

            IIInput in = new IIInput(files, ' ', '\n', false);
            IIShifter shift = new IIShifter(false);
            IISorter sort = new IISorter(false);
            IIOutput out = new IIOutput();
        }
        catch( Exception e )
        {
            e.printStackTrace();
            throw new KWICException(e.toString());
        }
    }    

    protected void printCollection( SentenceCollection collection)
    {
        try{
            collection.startKWICRowIterator();
            while( collection.hasNextKWICRow() == true )
            {
                Object[] temp = collection.getNextKWICRow();
                int count = temp.length;
                for( int i=0; i<count; i++ )
                {
                    System.out.print(temp[i].toString() + " ");
                }
                System.out.println();
            }
        }
        catch(KWICException e) {}	
    }
}
