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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;

public class Database implements Service {

	private int recordNum;
	private String flightID[];
	private String dDate[];
	private String dCity[];
	private String aCity[];
	private String dTime[];
	private String aTime[];
	private String duration[];
	private String price[];
	
	public Database() {
		initialize("./flightdb.txt");
	}

	//Initialize the database by using array
	public void initialize(String filename) {
		int i = 0;
		String line;

		recordNum = 0;

		try {
			BufferedReader db = new BufferedReader(new FileReader(filename));
			
			while(true) {
				line = db.readLine();				
				if(line == null) {
					recordNum = i;
					break;
				} else {
					i++;
				}
			}

			db.close();

			System.out.println("Number of record" + recordNum);

			flightID = new String[recordNum];
			dDate = new String[recordNum];
			dCity = new String[recordNum];
			aCity = new String[recordNum];
			dTime = new String[recordNum];
			aTime = new String[recordNum];
			duration = new String[recordNum];
			price = new String[recordNum];

			db = new BufferedReader(new FileReader(filename));

			i=0;

			while(true) {
		
				line = db.readLine();
				if(line == null) {
					break;
				} else {
                                        //System.out.println(line);
        
                                        flightID[i] = line.substring(0,5);
                                        dDate[i] = line.substring(5,13);
                                        dCity[i] = line.substring(13,23);
                                        aCity[i] = line.substring(23,33);
                                        dTime[i] = line.substring(33,37);
                                        aTime[i] = line.substring(37,41);
                                        duration[i] = line.substring(41,45);
                                        price[i] = line.substring(45,50);
                        
                                        i++;
				}
			}

			db.close();
		}catch (Exception e) {
			System.out.println(e);
			System.exit(-1);
		}
	}

	public String[] queryAll() {
		int i;
		String[] result;
		
		result = new String[recordNum];

		for(i=0; i<recordNum; i++) {
			result[i] = flightID[i]+dDate[i]+dCity[i]+aCity[i]+dTime[i]+aTime[i]+duration[i]+price[i];
		}

		return result;
	}

	public String[] queryBydate(String date) {
		int i, num;
		String[] result;

		num = 0;

		for(i=0; i<recordNum; i++) {
			if(date.compareTo(dDate[i]) == 0)
				num++;
		}

		if(num != 0) {
			result = new String[num];

			num=0;
			for(i=0; i<recordNum; i++) {
				if(date.compareTo(dDate[i]) == 0) {
					result[num] = flightID[i]+dDate[i]+dCity[i]+aCity[i]+dTime[i]+aTime[i]+duration[i]+price[i];
					num++;
				}
			}
		} else
			result = null;

		return result;
	}

	public String[] queryByID(String fID) {
		int i, num;
		String[] result;

		num = 0;

		for(i=0; i<recordNum; i++) {
			if(fID.compareTo(flightID[i]) == 0)
				num++;
		}

		if(num != 0) {
			result = new String[num];

			num=0;
			for(i=0; i<recordNum; i++) {
				if(fID.compareTo(flightID[i]) == 0) {
					result[num] = flightID[i]+dDate[i]+dCity[i]+aCity[i]+dTime[i]+aTime[i]+duration[i]+price[i];
					num++;
				}
			}
		} else
			result = null;

		return result;
	}

	public String[] queryBydateID(String date, String fID) {
		int i, num;
		String[] result;

		num = -1;

		for(i=0; i<recordNum; i++) {
			if(date.compareTo(dDate[i]) == 0 && fID.compareTo(flightID[i]) == 0) {
				num = i;
				break;
			}
		}

		if(num != -1) {
			result = new String[1];		
			result[0] = flightID[num]+dDate[num]+dCity[num]+aCity[num]+dTime[num]+aTime[i]+duration[num]+price[num];
		} else
			result = null;		

		return result;
	}

	public void execute(HttpServletRequest request, HttpServletResponse response, ServletContext context) throws Exception {
		String fID = request.getParameter("flightID");
		String date = request.getParameter("dDate");
		String[] result;

		fID = fID.trim();

		date = date.trim();

		if(fID.length() != 0 && date.length() != 0) {
			result = queryBydateID(date, fID);
		} else if(fID.length() != 0 && date.length() == 0) {
			result = queryByID(fID);
		} else if(fID.length() == 0 && date.length() != 0) {
			result = queryBydate(date);
		} else {
			result = queryAll();
		}
		request.setAttribute("result", result);		
	}
}
