/*******************************************************************************
 * 
 * Lab9 program
 * 
 * Purpose:  This program asks the user for two zip codes, looks up each 
 * 			zip code from an input file to get their state-city name,
 * 			latitude and longitude, then calculates the distances between
 * 			the two zipcodes and prints results.
 * 
 * @author Student's Name
 *
 * @version 1.0 Date    
 * 
 ******************************************************************************/
import java.io.*;
import java.util.*;

public class Lab9 {

	/**=========================================================================
	 * CLASS CONSTANTS
	 * 
	 * Constants used throughout this program and their comments.
	 *========================================================================*/
	public static final double RADIUS = 3956.6;
	

	/**=========================================================================
	 * CLASS METHODS
	 * 
	 * Program methods are listed below in alphabetical order or the order in
	 * which they are used in main below.
	 *========================================================================*/
	
	/**
	 * Asks the user to enter and then returns a 5 digit zipCode
	 * @param console - A console/user input Scanner object
	 * @param whichEntry - The word "first" or "second" used in the prompt.
	 * @return  Returns a string representing the 5 digit zipCode entered
	 * 			by the user.
	 */
	public static String getZipCode(Scanner console, String whichEntry) {
		String retZip = "";  // Assign a value as input by the user below

		// Student will complete
		
		return retZip;
	}
	
	/**
	 * Returns the number of lines in a specified file.
	 * @param fname - the name of the file
	 * @return
	 * @throws FileNotFoundException
	 */
	public static int numLines(String fname) throws FileNotFoundException {
		Scanner file = new Scanner(new File(fname));
		int cnt = 0;
		while (file.hasNextLine()) {
			file.nextLine();
			cnt++;
		}
		file.close();
		return cnt;
	}
	
	/**
	 * Given the name of a file and arrays already sized to the number of
	 * lines in the file this methods fills the arrays with the contents
	 * read from the file.
	 * @param fname - the name of the input file
	 * @param zips - zipcodes array
	 * @param lats - lattitudes array
	 * @param longs - longitutes array
	 * @param cities - city states array
	 * @throws FileNotFoundException 
	 */
	public static void readFile(String fname, String[] zips, double[] lats, double[] longs, String[] cities) throws FileNotFoundException {
		// Declare and initialize vars
		Scanner file = new Scanner(new File(fname));

		// Student will complete
		
		file.close();
	}
	
	
	/**
	 * Looks for zip code specified in array and if found returns its else returns
	 * index else returns -1.
	 * @param zips - zipcodes array
	 * @param findZip - zipcode to look for
	 * @return
	 * @throws FileNotFoundException
	 */
	public static int findZipCode(String[] zips, String findZip) throws FileNotFoundException {
		int idx = -1;

		// Student will complete
		
		return idx;
	}
	
	/**
	 * Given a city and state string formatted as NY-ALBANY returns
	 * a string formated as ALBANY, NY.
	 * @param cityState - the city and state string to format
	 * @return
	 */
	public static String formatCityState(String cityState) {
		String formattedCityState = "";
		
		// Student will complete
	    
		return formattedCityState;
	}
	
	/**
	 * Returns spherical distance in miles between two points
	 * @param lat1  - Latitude of first point
	 * @param long1 - Longitude of first point
	 * @param lat2 - Latitude of second point
	 * @param long2 - longitude of second point
	 * @return
	 */
	public static double calculateDistance(double lat1, double long1,
										   double lat2, double long2) {
		lat1 = Math.toRadians(lat1);
		long1 = Math.toRadians(long1);
		lat2 = Math.toRadians(lat2);
		long2 = Math.toRadians(long2);
		double theCos = Math.sin(lat1) * Math.sin(lat2) +
				        Math.cos(lat1) * Math.cos(lat2) *
				        Math.cos(long1 - long2);
		double arcLength = Math.acos(theCos);
		return arcLength * RADIUS;
		
	}
	
	/**=========================================================================
	 * CLASS main
	 * 
	 * @param args - The command-line arguments passed to the running program.
	 * @throws FileNotFoundException 
	 *========================================================================*/
	public static void main(String[] args) throws FileNotFoundException {
		
		// Declare variables, get num lines, declare array
		Scanner console = new Scanner(System.in);
		String fname = "zipcodes.txt";
		int numLines = numLines(fname);
		String zips[] = new String[numLines];
		double lats[] = new double[numLines];
		double longs[] = new double[numLines];
		String cities[] = new String[numLines];
		
		// Determine number of lines in file then read the file
		readFile(fname, zips, lats, longs, cities);
		
		// Get zip codes from the user
		String zipCode1 = getZipCode(console, "first");  // Get first zip code
		String zipCode2 = getZipCode(console, "second"); // Get second zip code
	
		// Find zip codes indexes
		int idx1 = findZipCode(zips, zipCode1);   // Find first zip code's idx
		int idx2 = findZipCode(zips, zipCode2);   // Find second zip code's idx
		
		// If both are found then calculate distance and print out formatted data
		if ( idx1!=-1 && idx2!=-1 ) {
			double miles = calculateDistance( lats[idx1], longs[idx1],
					                          lats[idx2], longs[idx2] );
			System.out.printf("%s %s is %.2f miles from %s %s\n", 
					                          formatCityState(cities[idx1]),
					                          zipCode1,
					                          miles,
					                          formatCityState(cities[idx2]),
					                          zipCode2 );
		} else {  
			System.out.printf("One or both of the zip codes could not be found.\n");
		}	
	}
}