package com.highgateguild.utils.fileprocessing.examples;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

import com.highgateguild.utils.fileprocessing.building.AlternativeSequenceBuilder;
import com.highgateguild.utils.fileprocessing.building.DriverBuilder;
import com.highgateguild.utils.fileprocessing.helpers.Counter;
import com.highgateguild.utils.fileprocessing.helpers.DelimitedRecordParser;
import com.highgateguild.utils.fileprocessing.helpers.Factory;
import com.highgateguild.utils.fileprocessing.helpers.SimpleCounter;

public class ExtendedTest extends TestCase {

	private Factory<Flight> factory = new Factory<Flight>() {

		public Flight create(String record, String[] fields) throws IOException {
			if (fields.length != 3) {
				throw new IOException("Invalid record: " + record);
		}
			return new Flight(fields);
		}
		
	};

	private List<Flight> flights = new ArrayList<Flight>();

	public void testIncludingFiltersOutRecordsThatDoNotMatchACondition()
			throws Exception {
		Counter allRecords = new SimpleCounter();
		Counter checkBRecords = new SimpleCounter();
		Counter checkNonBRecords = new SimpleCounter();
		Counter checkAcceptedNonBRecords = new SimpleCounter();
		Counter nonBlankRecords = new SimpleCounter();
		new DriverBuilder().
			fromFile("docs/sample-data/flights2.txt").
			counting(allRecords).
			withoutBlankLines().
			counting(nonBlankRecords).
			allowing(
				new AlternativeSequenceBuilder().
					accepting(3).
					matching("^B").
					counting(checkBRecords).
					populatingList(flights, factory,  new DelimitedRecordParser(",")).
					getProcessing(),
				new AlternativeSequenceBuilder().
					counting(checkNonBRecords).
					accepting(1).
					counting(checkAcceptedNonBRecords).
					matching("^[^B]").
					populatingList(flights, factory,  new DelimitedRecordParser(",")).
					getProcessing())
			.read();
		System.out.println(flights);
		assertEquals(3, flights.size());
		assertEquals(6, allRecords.getCount());
		assertEquals(4, nonBlankRecords.getCount());
		assertEquals(2, checkBRecords.getCount());
		assertEquals(2, checkNonBRecords.getCount());
		assertEquals(1, checkAcceptedNonBRecords.getCount());
		checkEntry(0, "BA123", "Glasgow", "09:45");
		checkEntry(2, "BM435", "Bradford", "10:40");
	}
	
	public void testPopulatesAList() throws Exception {
		new DriverBuilder().
		fromFile("docs/sample-data/flights.txt").
		populatingList(flights, factory,  new DelimitedRecordParser(","))
				.read();
		assertEquals(4, flights.size());
		checkEntry(0, "BA123", "Glasgow", "09:45");
		checkEntry(2, "BM435", "Bradford", "10:40");
	}

	private void checkEntry(int index, String flightCode, String destination,
			String departing) {
		Flight flight = flights.get(index);
		assertEquals(flightCode, flight.getFlightCode());
		assertEquals(destination, flight.getDestination());
		assertEquals(departing, flight.getDeparting());
	}
}
