package test;

import java.util.Comparator;
import java.util.Map;

public class TestDataRow {

	private int id;
	private String raceDate;
	private int raceNumber;
	private int lane;
	private int distance;
	private float time;
	private String timeModifier;
	private int trackId;
	private float horseAvgTime;
	private float horseAvgTimeByDistance;
	private float driverAvgTime;
	private float driverAvgTimeByDistance;

	private float laneWinPercent;
	private static float divisor = 5;

	private int result;

	public TestDataRow(Map<String, String> map) {
		id = Integer.parseInt(map.get("id"));
		raceDate = map.get("RaceDate");
		raceNumber = Integer.valueOf(map.get("RaceNumber"));
		lane = Integer.valueOf(map.get("Lane"));
		distance = Integer.valueOf(map.get("Distance"));
		time = Float.valueOf(map.get("Time"));
		timeModifier = map.get("TimeModifier");
		trackId = Integer.valueOf(map.get("TrackId"));

		if (map.get("HorseAvgTime").equals("NA") || 
				map.get("HorseAvgTime").toLowerCase().equals("null")) {
			horseAvgTime = -1f;
		} else {
			horseAvgTime = Float.valueOf(map.get("HorseAvgTime"));
		}
		if (map.get("horseAvgByDistance").equals("NA") || 
				map.get("horseAvgByDistance").toLowerCase().equals("null")) {
			horseAvgTimeByDistance = -1f;
		} else {
			horseAvgTimeByDistance = Float.valueOf(map.get("horseAvgByDistance"));
		}

		if (map.get("driverAvgTime").equals("NA") || 
				map.get("driverAvgTime").toLowerCase().equals("null")) {
			driverAvgTime = -1f;
		} else {
			driverAvgTime = Float.valueOf(map.get("driverAvgTime"));
		}

		if (map.get("driverAvgByDistance").equals("NA") || 
				map.get("driverAvgByDistance").toLowerCase().equals("null")) {
			driverAvgTimeByDistance = -1f;
		} else {
			driverAvgTimeByDistance = Float.valueOf(map.get("driverAvgByDistance"));
		}
		result = Integer.valueOf(map.get("Result"));
	}

	public int getId() {
		return id;
	}

	public String getRaceDate() {
		return raceDate;
	}

	public int getRaceNumber() {
		return raceNumber;
	}

	public int getLane() {
		return lane;
	}

	public int getDistance() {
		return distance;
	}

	public float getTime() {
		return time;
	}

	public String getTimeModifier() {
		return timeModifier;
	}

	public int getTrackId() {
		return trackId;
	}

	public float getHorseAvgTime() {
		return horseAvgTime;
	}

	public float getHorseAvgTimeByDistance() {
		return horseAvgTimeByDistance;
	}

	public float getDriverAvgTime() {
		return driverAvgTime;
	}

	public float getDriverAvgTimeByDistance() {
		return driverAvgTimeByDistance;
	}

	public int getResult() {
		return result;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setRaceDate(String raceDate) {
		this.raceDate = raceDate;
	}

	public void setRaceNumber(int raceNumber) {
		this.raceNumber = raceNumber;
	}

	public void setLane(int lane) {
		this.lane = lane;
	}

	public void setDistance(int distance) {
		this.distance = distance;
	}

	public void setTime(float time) {
		this.time = time;
	}

	public void setTimeModifier(String timeModifier) {
		this.timeModifier = timeModifier;
	}

	public void setTrackId(int trackId) {
		this.trackId = trackId;
	}

	public void setHorseTimeAvg(float horseTimeAvg) {
		this.horseAvgTime = horseTimeAvg;
	}

	public void setHorseTimeAvgByDistance(float horseTimeAvgByDistance) {
		this.horseAvgTimeByDistance = horseTimeAvgByDistance;
	}

	public void setDriverTimeAvg(float driverTimeAvg) {
		this.driverAvgTime = driverTimeAvg;
	}

	public void setDriverTimeAvgByDistance(float driverTimeAvgByDistance) {
		this.driverAvgTimeByDistance = driverTimeAvgByDistance;
	}

	public void setLaneWinDivisor(float divisor) {
		TestDataRow.divisor = divisor;
	}

	public float getLaneWinPercent() {
		return laneWinPercent / divisor;
	}

	public float getLaneWinPercent(float winPercent) {
		return (laneWinPercent / divisor) * winPercent;
	}


	public void setLaneWinPercent(float laneWinPercent) {
		this.laneWinPercent = laneWinPercent;
	}

	public void setResult(int result) {
		this.result = result;
	}

	public static class RaceDateSorter implements Comparator<TestDataRow> {
		@Override
		public int compare(TestDataRow d1, TestDataRow d2) {
			return d1.getRaceDate().compareTo(d2.getRaceDate());
		}
	}

	public static class TrackIdSorter implements Comparator<TestDataRow> {
		@Override
		public int compare(TestDataRow d1, TestDataRow d2) {
			return Integer.compare(d1.getTrackId(), d2.getTrackId());
		}
	}

	public static class RaceNumberSorter implements Comparator<TestDataRow> {
		@Override
		public int compare(TestDataRow d1, TestDataRow d2) {
			return Integer.compare(d1.getRaceNumber(), d2.getRaceNumber());
		}
	}

	public static class horseAvgTimeSorter implements Comparator<TestDataRow> {
		@Override
		public int compare(TestDataRow d1, TestDataRow d2) {
			return Float.compare(d1.getHorseAvgTime() > 0?d1.getHorseAvgTime() - (d1.getLaneWinPercent()):999,
					d2.getHorseAvgTime() > 0?d2.getHorseAvgTime() - (d2.getLaneWinPercent()):999);
		}
	}

	public static class driverAvgTimeSorter implements Comparator<TestDataRow> {
		@Override
		public int compare(TestDataRow d1, TestDataRow d2) {
			return Float.compare(d1.getDriverAvgTime() > 0 ? d1.getDriverAvgTime() - (d1.getLaneWinPercent()):999,
					d2.getDriverAvgTime() > 0 ? d2.getDriverAvgTime() - (d2.getLaneWinPercent()): 999);
		}
	}

	public static class horseAvgTimeByDistanceSorter implements Comparator<TestDataRow> {
		@Override
		public int compare(TestDataRow d1, TestDataRow d2) {
			return Float.compare(d1.getHorseAvgTimeByDistance() > 0 ? d1.getHorseAvgTimeByDistance() - (d1.getLaneWinPercent()) : 999,
					d2.getHorseAvgTimeByDistance() > 0 ? d2.getHorseAvgTimeByDistance() - (d2.getLaneWinPercent()) : 999);
		}
	}

	public static class driverAvgTimeByDistanceSorter implements Comparator<TestDataRow> {
		@Override
		public int compare(TestDataRow d1, TestDataRow d2) {
			return Float.compare(d1.getDriverAvgTimeByDistance() > 0 ? d1.getDriverAvgTimeByDistance() - (d1.getLaneWinPercent()) : 999,
					d2.getHorseAvgTimeByDistance() > 0 ? d2.getHorseAvgTimeByDistance() - (d2.getLaneWinPercent()) : 999);
		}
	}
}
