/* Taylor Leese 
 * CSPP 53015
 * Homework #3
 * 10/17/07
 */

package homework3;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class OPIncidents {

	private static final String fileName = "op.csv";

	private static final String split_re = "\\|";
	private static final String address_re = "[0-9]+( )+[NSEW]?( )*[0-9a-zA-Z]+";
	
	private static final int ucrColNum = 3;
	private static final int addrColNum = 6;
	
	private BufferedReader br;
	
	public static void main(String[] args) throws IOException {
		OPIncidents incidents = new OPIncidents();
		incidents.search();
	}
	
	public OPIncidents() throws IOException {
		br = new BufferedReader(new FileReader(fileName));	
		br.readLine(); // skip first line
	}

	public void search() throws IOException {
		Pattern p = Pattern.compile(address_re);  
		Map searchMap = new HashMap();
		String line = null;
		
		do {
			String ucr = null;
			String address = null;
			
			line = br.readLine();
			if (line == null) {
				continue;
			}
			
			String[] tokens = line.split(split_re);
			if (tokens.length < addrColNum+1) {
				continue;
			}
			
			ucr = tokens[ucrColNum];
			address = tokens[addrColNum];
			
			Matcher matcher = p.matcher(address);
			boolean found = matcher.find();
			
			if (found) {
				address = matcher.group().toUpperCase().trim();
			}
			
			if (searchMap.containsKey(ucr)) {
				IncidentEntry ie = (IncidentEntry) searchMap.get(ucr);
				ie.increment();
				ie.addAddress(address);
			} else {
				IncidentEntry ie = new IncidentEntry();
				ie.addAddress(address);
				searchMap.put(ucr, ie);	
			}
		} while (line != null);
		
		List ucrList = new ArrayList(searchMap.entrySet());
		Collections.sort(ucrList, new IncidentComparator());
		
		//BufferedWriter bwriter = new BufferedWriter(new FileWriter("op-out.csv"));
		
		for (int i = 0; i < 3 && i < ucrList.size(); i++) {
			Map.Entry m = (Entry) ucrList.get(i);
			IncidentEntry ie = (IncidentEntry) m.getValue();
			System.out.println("UCR: " + m.getKey() + " Count: " + ie.getUcrCount());
			//bwriter.write("UCR: " + m.getKey() + "|Count: " + ie.getUcrCount() + "\n");
			List addressList = new ArrayList(ie.getAddressCounts().entrySet());
			Collections.sort(addressList, new IncidentComparator());
			for (int j = 0; j < addressList.size(); j++) {
				Map.Entry e = (Entry) addressList.get(j);
				Integer count = (Integer) e.getValue();
				if (count.intValue() > 1) {
					System.out.println("\tAddress: " + e.getKey() + " Count: " + e.getValue());
					//bwriter.write(e.getKey() + "|" + e.getValue() + "\n");
				}
			}
			//bwriter.write("\n\n");
		}
		//bwriter.flush();
	}
}

