package client.search;


import java.util.ArrayList;
import java.util.Collections;
import client.reservation.*;

/**
 * This class performs sort based on reservation options 
 * We didn't implement Compareable, so we have to do this all manually
 * 
 * @author Forkey
 * @version 1
 * @since 06/28/2016
 */
public class FlightSearchSorter {
	
	public ArrayList<ReservationOption> sortPrice(boolean ascending, ArrayList<ReservationOption> resOptions) {
		ArrayList<ReservationOption> sortedReservations = new ArrayList<ReservationOption>();
		
		for (ReservationOption resOption : resOptions) {
			ReservationOption cheapestReservation = cheapestReservation(resOptions);
			sortedReservations.add(cheapestReservation);
			resOptions.remove(resOption);
		}
		if (!ascending) {
			Collections.reverse(sortedReservations);
		}
		return sortedReservations;
	}
	
	public ArrayList<ReservationOption> sortTime(boolean ascending, ArrayList<ReservationOption> resOptions) {
		ArrayList<ReservationOption> sortedReservations = new ArrayList<ReservationOption>();
		
		for (ReservationOption resOption : resOptions) {
			ReservationOption fastestReservation = fastestReservation(resOptions);
			sortedReservations.add(fastestReservation);
			resOptions.remove(resOption);
		}
		if (!ascending) {	
			Collections.reverse(sortedReservations);
		}
		return sortedReservations;
	}
	
	public ReservationOption cheapestReservation(ArrayList<ReservationOption> resOptions) {
		ReservationOption cheapestRes = resOptions.get(0);
		for (ReservationOption resOption: resOptions) {
			//TODO: need to consider coach seating
			if (resOption.getPrice(true) < cheapestRes.getPrice(true)) {
				cheapestRes = resOption;
			}
		}
		return cheapestRes;
	}
	
	public ReservationOption fastestReservation(ArrayList<ReservationOption> resOptions) {
		ReservationOption fastestRes = resOptions.get(0);
		//We could use int or double here, IDC.  Used LONG because thatis technically what timeUnit uses.
		long hours = 0;
		long minutes = 0;
		long seconds = 0;
		String totalTime = resOptions.get(0).getTotalTime();
		getTimeValues(totalTime, hours, minutes, seconds);

		for (ReservationOption resOption: resOptions) {
			String resTotalTime = resOption.getTotalTime();
			long resHours = 0;
			long resMinutes = 0;
			long resSeconds = 0;
			getTimeValues(resTotalTime, resHours, resMinutes, resSeconds);

			if (resHours < hours) {
				fastestRes = resOption;
			} else if (resHours == hours) {
				if (resMinutes < minutes){
					fastestRes = resOption;
				} else if (resMinutes == minutes) {
					if (resSeconds < seconds) {
						fastestRes = resOption;
					}
				}
			}
		}
		return fastestRes;
	}
	
	private void getTimeValues(String totalTime, long hours, long minutes, long seconds) {
		String[] timeValues = totalTime.split(":");
		hours = Long.parseLong(timeValues[0]);
		minutes = Long.parseLong(timeValues[1]);
		seconds = Long.parseLong(timeValues[2]);
	}
	


}
@@ -377,16 +377,24 @@
 on%3E sort
+AllClass
 Price(bo
@@ -401,32 +401,53 @@
 olean ascending,
+ boolean classOption,
  ArrayList%3CReser
@@ -557,32 +557,54 @@
 onOption%3E();%0A%09%09%0A
+%09%09if (classOption) %7B%0A%09
 %09%09for (Reservati
@@ -630,32 +630,33 @@
 : resOptions) %7B%0A
+%09
 %09%09%09ReservationOp
@@ -682,32 +682,34 @@
 ation = cheapest
+FC
 Reservation(resO
@@ -709,32 +709,33 @@
 on(resOptions);%0A
+%09
 %09%09%09sortedReserva
@@ -758,32 +758,33 @@
 stReservation);%0A
+%09
 %09%09%09resOptions.re
@@ -792,32 +792,272 @@
 ove(resOption);%0A
+%09%09%09%7D%0A%09%09%09%7D else %7B%0A%09%09%09%09for (ReservationOption resOption : resOptions) %7B%0A%09%09%09%09%09ReservationOption cheapestReservation = cheapestCCReservation(resOptions);%0A%09%09%09%09%09sortedReservations.add(cheapestReservation);%0A%09%09%09%09%09resOptions.remove(resOption);%0A%09%09%09%7D%0A
 %09%09%7D%0A%09%09if (!ascen
@@ -1100,32 +1100,36 @@
 dReservations);%0A
+%09%09%09%0A
 %09%09%7D%0A%09%09return sor
@@ -1687,32 +1687,34 @@
 nOption cheapest
+FC
 Reservation(Arra
@@ -2018,32 +2018,399 @@
 eapestRes;%0A%09%7D%0A%09%0A
+%09public ReservationOption cheapestCCReservation(ArrayList%3CReservationOption%3E resOptions) %7B%0A%09%09ReservationOption cheapestRes = resOptions.get(0);%0A%09%09for (ReservationOption resOption: resOptions) %7B%0A%09%09%09//TODO: need to consider coach seating%0A%09%09%09if (resOption.getPrice(false) %3C cheapestRes.getPrice(false)) %7B%0A%09%09%09%09cheapestRes = resOption;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return cheapestRes;%0A%09%7D%0A%09%0A
 %09public Reservat
@@ -3605,11 +3605,9 @@
 );%0A%09%7D%0A%09%0A
-%0A%0A
 %7D

add the actual file intended for last check-in -.-