package uk.ac.cam.cstibhotel.otcanalyser.networklayer;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.zip.ZipInputStream;
import java.net.URL;
import java.text.ParseException;

import uk.ac.cam.cstibhotel.otcanalyser.trade.*;


/*
 * 
 * @author: ts579
 * 
 */

public class ParseZIP {
	
	private static Boolean convertToBool(String tv, String fv, String input) throws BooleanFieldFormatException{
		if(input == tv){
			return true;
		}
		else if(input == fv){
			return false;
		}
		else if(input ==""){
			return null;
		}
		else throw new BooleanFieldFormatException();
	}
	
	//TODO: implement this bit
	private static Trade stringVectorToTrade(String[] tradeIn){
		Trade tradeOut = new Trade();
		//Setting fields in tradeOut appropriately
		try{
			
	
			tradeOut.setDisseminationID(Long.parseLong(tradeIn[0]));
			tradeOut.setOriginalDisseminationID(Long.parseLong(tradeIn[1]));
			tradeOut.setAction(Action.parseAct(tradeIn[2]));
			
			//Parsing the execution timestamp date
			DateFormat etf = new SimpleDateFormat("yyy-MM-dd'T'kk:mm:ss");
			tradeOut.setExecutionTimestamp(etf.parse(tradeIn[3]));
			
			//Cleared
			tradeOut.setCleared(convertToBool("C", "U", tradeIn[4]));
			
			//Collateralization
			tradeOut.setCollateralization(Collateralization.parseColl(tradeIn[5]));
			
			//End user exception
			tradeOut.setEndUserException(convertToBool("Y", "N", tradeIn[6]));
			
			//Other price affecting term TODO: Is this the INDICATION_OF_OTHER_PRICE_AFFECTING_TERM?
			tradeOut.setBespoke(convertToBool("Y", "N", tradeIn[7]));
			
			//BLOCK
			tradeOut.setBlockTrades(convertToBool("Y", "N", tradeIn[8]));
			
			//EXECUTION_VENUE
			tradeOut.setExecutionVenue(convertToBool("ON","OFF", tradeIn[9]));
			
			
			

		}
		catch(ActionFormatException e){
			e.printStackTrace();
		}
		catch(CollateralizationFormatException e){
			e.printStackTrace();
		}
		catch (ParseException e) {
			e.printStackTrace();
		}
		catch(BooleanFieldFormatException e){
			e.printStackTrace();
		}
		return tradeOut;
	}
	
	public static LinkedList<Trade> downloadData(String zipFile, String splitBy) throws IOException {
		String line;
		int i = 0;
		LinkedList<Trade> dataOut = new LinkedList<Trade>();
		
		URL url = new URL(zipFile);
		ZipInputStream zis = new ZipInputStream(url.openStream());
		zis.getNextEntry();
		InputStreamReader isr = new InputStreamReader(zis);
		BufferedReader br = new BufferedReader(isr);
		
		//reading line by line
		while((line = br.readLine()) != null){
 			//do not read the first line
			if(i!=0){
 				dataOut.add(stringVectorToTrade(line.split(splitBy)));
 			}
 			i++;
 		}
		
 		try {
 			br.close();
 		} catch (IOException IOEx) {
 			//safe to ignore
 		}
 		
 		return dataOut;
	}
	
	public static void main(String[] args) {
		try {
			ParseZIP.downloadData("https://kgc0418-tdw-data-0.s3.amazonaws.com/slices/CUMULATIVE_COMMODITIES_2015_02_04.zip",",");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

@@ -628,15 +628,20 @@
 nput
- == 
+.equals(
 tv)
+)
 %7B%0A%09%09
@@ -677,15 +677,20 @@
 nput
- == 
+.equals(
 fv)
+)
 %7B%0A%09%09
@@ -727,14 +727,20 @@
 nput
- ==
+.equals(
 %22%22)
+)
 %7B%0A%09%09
@@ -810,16 +810,155 @@
 );%0A%09%7D%0A%09%0A
+%09private static long parseLong(String input) %7B%0A%09%09if (input.equals(%22%22)) %7B%0A%09%09%09return 0;%0A%09%09%7D else %7B%0A%09%09%09return Long.parseLong(input);%0A%09%09%7D%0A%09%7D%0A%09%0A
 %09//TODO:
@@ -1151,37 +1151,32 @@
 DisseminationID(
-Long.
 parseLong(tradeI
@@ -1222,21 +1222,16 @@
 ationID(
-Long.
 parseLon
@@ -2893,16 +2893,264 @@
 (i!=0)%7B%0A
+%09%09%09%09String%5B%5D tradeIn = line.split(splitBy);%0A%09%09%09%09for (int j = 0; j %3C tradeIn.length; j++) %7B%0A%09%09%09%09%09if (tradeIn%5Bj%5D.startsWith(%22%5C%22%22) && tradeIn%5Bj%5D.endsWith(%22%5C%22%22)) %7B%0A%09%09%09%09%09%09tradeIn%5Bj%5D = tradeIn%5Bj%5D.substring(1, tradeIn%5Bj%5D.length() - 1);%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%09%09%0A
  %09%09%09%09dat
@@ -3178,35 +3178,23 @@
 ToTrade(
-line.split(splitBy)
+tradeIn
 ));%0A %09%09%09

fix some parsing bugs