import java.util.List;
import java.util.ArrayList;
import java.io.*;

public class ParseUtil {

	private Boolean IsStringNullOrEmpty(String s) {
		if (s.equals("") || s.equals(null)) {
			return true;
		}
		return false;
	}

	private String parseOneQuote(String[] args) {
		String prl = "";
		if (args[2].equals("") || args[2].equals(null)) {
			System.out
					.println("Third parameter in file (Market) can't be empty");
			return "";
		}

		String instrumentType = args[1].toUpperCase();

		if (instrumentType.equals("STOCK")) {
			if (args.length >= 4 && !IsStringNullOrEmpty(args[3])) {
				prl = "/rt/stock/all?Mkt=\"" + args[2] + "\"&~fields=" + args[3];
			} else {
				prl = "/rt/stock/all?Mkt=\"" + args[2] + "\"";
			}
		} else if (instrumentType.equals("DERIVATIVE")) {
			if (args.length >= 4 && !IsStringNullOrEmpty(args[3])) {
				prl = "/rt/derivative/all?Mkt=\"" + args[2] + "\"&~fields="
						+ args[3];
			} else if (args[2].equals("ALL")){
				prl = "/rt/derivative/all";
			} else {
				prl = "/rt/derivative/all?Mkt=\"" + args[2] + "\"";	
			}
		} else {
			System.out.println("Invalid type :" + args[1]);
			return "";
		}

		return prl;
	}
	
	private String parseOneInfo(String[] args) {
		String prl = "";
		if (args[2].equals("") || args[2].equals(null)) {
			System.out
					.println("Third parameter in file (Market) can't be empty");
			return "";
		}

		String instrumentType = args[1].toUpperCase();

		if (instrumentType.equals("STOCK")) {
			if (args.length >= 4 && !IsStringNullOrEmpty(args[3])) {
				prl = "/rt/stock/all?Mkt=\"" + args[2] + "\"&Id=\"" + args[3]
						+ "\"&~fields=" + args[4];
			} else {
				prl = "/rt/stock/all?Mkt=\"" + args[2] + "\"";
			}
		} else if (instrumentType.equals("DERIVATIVE")) {
			if (args.length >= 4 && !IsStringNullOrEmpty(args[3])) {
				prl = "/rt/derivative/all?Mkt=\"" + args[2]  + "\"&Id=\"" + args[3]
						+ "\"&~fields=" + args[4];
			} else if (args[2].equals("ALL")){
				prl = "/rt/derivative/all";
			} else {
				prl = "/rt/derivative/all?Mkt=\"" + args[2] + "\"";	
			}
		} else {
			System.out.println("Invalid type :" + args[1]);
			return "";
		}

		return prl;
	}

	private String parseOneOrder(String[] args) {
		String prl = "";

		if (IsStringNullOrEmpty(args[2])) {
			System.out
					.println("Third parameter in file (Market) can't be empty");
			return "";
		}

		if (IsStringNullOrEmpty(args[3])) {
			System.out.println("Fourth parameter in file (Id) can't be empty");
			return "";
		}

		String instrumentType = args[1].toUpperCase();

		if (instrumentType.equals("STOCK")) {
			if (args.length >= 5 && !IsStringNullOrEmpty(args[4])) {
				prl = "/rt/stock/order?Mkt=\"" + args[2] + "\"&Id=\"" + args[3]
						+ "\"&~fields=" + args[4];
			} else {
				prl = "/rt/stock/order?Mkt=\"" + args[2] + "\"&Id=\"" + args[3]
						+ "\"";
			}
		} else if (instrumentType.equals("DERIVATIVE")) {
			if (args.length >= 5 && !IsStringNullOrEmpty(args[4])) {
				prl = "/rt/derivative/order?Mkt=\"" + args[2] + "\"&Id=\""
						+ args[3] + "\"&~fields=" + args[4];
			} else if (args[2].equals("ALL")){
				prl = "/rt/derivative/order";
			} else {
				prl = "/rt/derivative/order?Mkt=\"" + args[2] + "\"&Id=\""
						+ args[3] + "\"";
			}
		} else {
			System.out.println("Invalid type :" + args[1]);
			return "";
		}
		return prl;
	}

	public List<String> parseFile(String filename) {
		List<String> parseList = new ArrayList<String>();
		try {
			
			FileInputStream fstream = new FileInputStream(filename);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			while ((strLine = br.readLine()) != null) {
				if (!strLine.startsWith(";")) {
					String[] p = strLine.split(";");
					if (p.length >= 3) {
						String instrumentType = p[0].toUpperCase();

						if (instrumentType.equals("QUOTE")) {
							String prl = parseOneQuote(p);

							if (!prl.equals(""))
								parseList.add(prl);

						} else if (instrumentType.equals("ORDER")) {
							String prl = parseOneOrder(p);

							if (!prl.equals(""))
								parseList.add(prl);
						} else if (instrumentType.equals("INFO")) {
							String prl = parseOneInfo(p);

							if (!prl.equals(""))
								parseList.add(prl);
						} else if (instrumentType.equals("NEWS")) {
							parseList.add("/rt/news");
						}
						else {
							System.out.println("Invalid type :" + p[0]
									+ " in file " + filename);
						}
					}
				}
			}
			in.close();
		} catch (Exception e) {
			System.err.println("Error: " + e.getMessage());
		}

		return parseList;
	}
}
