package somiba.apps;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import somiba.domain.Player;
import somiba.domain.Season;
import somiba.domain.YearTeam;

public class Reader extends AbstractDaoAccessor {

	public static void main(String args[]) {
		int argc = args.length;
		if (argc < 1) {
			System.out.println("java Reader filename");
			System.exit(1);
		}
		Reader ps = new Reader();
		ps.readData("file:" + args[0]);
	}

	void readData(String file_name) {

		String line;
		String team;
		String year;
		int count = 0;
		int teamStart;
		Season[] seasons = new Season[500];

		try {
			URL u = new URL(file_name);
			URLConnection urlc = u.openConnection();
			urlc.setDoInput(true);
			urlc.setAllowUserInteraction(false);

			BufferedReader contents = new BufferedReader(new InputStreamReader(
					urlc.getInputStream()));

			while (true) {
				// white space
				do {
					line = contents.readLine();
				} while (line != null && line.length() == 0);
				if (line == null)
					break;
				year = line.substring(5, 7);
				// headers
				contents.readLine();
				contents.readLine();
				teamStart = count;
				do {
					line = contents.readLine();
					seasons[count] = new Season(year); // sets year
				} while (seasons[count++].parse(line)); // sets name
				count--;
				team = line.substring(0, 9);
				// headers
				for (int i = 0; i < 4; i++)
					contents.readLine();
				for (int i = 0; i < count - teamStart; i++) {
					Season season = seasons[teamStart + i];
					String name = season.getName();
					YearTeam yearTeam = getDaoRepository().getYearTeamDao().get(name, year, team);
					if (yearTeam != null) {
						String mappedName = yearTeam.getCustom();
						if (mappedName != null) {
							name = mappedName;
							seasons[teamStart + i].setName(name);
						}
					}
					Player player = getDaoRepository().getPlayerDao().getByName(name);
					if (player == null) {
						player = new Player(name, 0);
						getDaoRepository().getPlayerDao().save(player);
					}
					player.incrementSeasons();
					line = contents.readLine();
					season.secondParse(team, line); // sets team
					Season totals = getDaoRepository().getSeasonDao().get(name, "", "Totals");
					if (totals == null) {
						totals = new Season(name, "", "Totals");
						getDaoRepository().getSeasonDao().save(totals);
					}
					totals.increment(season);
					List<Season> allSeasons = getDaoRepository().getSeasonDao().getSameYear(name, year);
					Season otherTeam = null;
					if (allSeasons.size() > 0)
						otherTeam = allSeasons.get(0);
					Season composite = null;
					if (otherTeam != null) {
						composite = getDaoRepository().getSeasonDao().get(name, year, "composite");
						if (composite == null) {
							composite = new Season(name, year, "composite");
							composite.increment(otherTeam);
							getDaoRepository().getSeasonDao().save(composite);
						}
						composite.increment(season);
					}
					getDaoRepository().getSeasonDao().save(season);
					getDaoRepository().getSeasonDao().update(totals);
					getDaoRepository().getPlayerDao().update(player);
					if (composite != null)
						getDaoRepository().getSeasonDao().update(composite);
				}
				// headers
				contents.readLine();
				contents.readLine();
			}
		} catch (NullPointerException npe) {
			npe.printStackTrace() ;
		} catch (IOException ioe) {
			System.out.println(ioe) ;
		}
	}
}
