import java.io.*;
import java.util.Vector;

/** A user interface to drive the Space and Point classes. This implementation
 * does not include the full user interface as specified in the assignment 
 * handout.  It includes the methods that were required to be implemented.  
 * A1Driver is used by A1Test to perform some tests on the program.
 */

public class A1Driver {
    private Space space;
    
    public static void main(String[] args) throws IOException {
        A1Driver a1 = new A1Driver();
        a1.space = new Space();
        
        a1.load("..\\pointdata");
        a1.readViewPoint();
        a1.printSpace();
    }
    
    /**
     * Load the points defined from the file filename
     * into the collection
     * @param filename Name of the file containing the
     *        list of points.
     * File format: One point per line in the order of the 
     * dimensions (x, y, z).
     * Assumption: The file is correctly formatted. 
     */

    public void load(String filename) throws IOException  {
        BufferedReader br = new BufferedReader(new FileReader(filename));
        String line;
        while((line = br.readLine()) != null) {
            space.addPoint(new Point(line));    
        }
    }
       
    /**
     * Write the collection of points into a file in the same
     * format as the input file.
     * @param filename Name of file to be written.  The file
     *                 will be overwritten if it already exists.
     */
    public void write(String filename) throws IOException {
        PrintStream ps = new PrintStream(new FileOutputStream(filename));
        space.printPoints(ps);      
    }
    
    /**
     * Prompt for and read a point from the keyboard, and add 
     * it to the space.
     */
    public void readPoint() throws IOException {
        System.out.println("Please enter the coordinates for a point");
	BufferedReader br = new BufferedReader(new InputStreamReader
					       (System.in));
	String line = br.readLine();
	if(line == null) {
	    System.err.println("Did not get input from user\n");
	}
	space.addPoint(new Point(line));
    }
    
    /**
     * Prompt for and read a point from the keyboard. This point will
     * become the new view point.
     */
    public void readViewPoint() throws IOException {
        System.out.println("Please enter the coordinates for a point");
	BufferedReader br = new BufferedReader(new InputStreamReader
					       (System.in));
	String line = br.readLine();
	if(line == null) {
	    System.err.println("Did not get input from user\n");
	}
	space.setView(new Point(line));
        
    }

    /**
     * Print the point at position i in the collection ordered by
     * the current view point.
     * @param i The position of the point to retrieve.
     */
    public void get(int i) {
        System.out.println(space.get(i));
    }
    
    /**
     * Print the collection of points from the space in the order
     * of a point's distance from the view point.
     */
    public void printSpace() {
        space.order();
        System.out.println(space);
    }
    
    /**
     * Print the list of points that are in the same kth plane as
     * the view point. We assume that x is dimension 0, y is dimension 1,
     * and z is dimension 2.
     *
     * @param k Index of the plane to report on
     */
    public void samePlane(int k) {
        Vector v = space.samePlane(k);
	for(int i = 0; i < v.size(); i++) {
	    System.out.println(v.get(i));
	}
    }

    /**
     * Create and initialize the space object
     */
    public void initSpace(){
    	space = new Space();
    }
}
