package tests;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;

import data.GuiMysql;
import objects.League;
import objects.SoccerMatch;
import objects.TeamResults;

public class AwayTests extends TestClass {

	private League leagueInfo;

	@Override public void setup(String date, float startingBank, float bettingLevel, Float betMargin, int lookBack, int sportId, Integer countryId,
			Integer leagueId) {
		super.setup(date, startingBank, bettingLevel, betMargin, lookBack, sportId, countryId, leagueId);

		leagueInfo = getLeagueInfoById();
	}

	@Override public void runTest() {
		final ArrayList<SoccerMatch> matches = getMatches();

		final float betLevel = bettingLevel / 100f;

		final LocalDateTime currentDate = matches.get(0).getGameDate();
		LocalDate localDate = currentDate.toLocalDate();
		float betAmount = startingBank * betLevel;

		int betOnGameCount = 0;
		int wins = 0;

		float bestBankResult = startingBank;
		int bestBetMargin = 0;
		int bestLookBack = 0;

		for (int lookBack = 4; lookBack < 25; lookBack++) {
			for (int betMargin = 1; betMargin < 35; betMargin++) {
				final float betMarginDecimal = 1 + (betMargin / (float) 100);
				float bank = startingBank;
				betOnGameCount = 0;
				wins = 0;
				for (final SoccerMatch match : matches) {
					if (match.getHomeScore() < 0 || match.getAwayScore() < 0) {
						continue;
					}

					final TeamResults homeTeamResults = GuiMysql.getInstance().getTeamResultsTest(match.getHomeTeam().getTeamId(), lookBack, true,
							match.getGameDate().format(DateTimeFormatter.ISO_DATE));
					final TeamResults awayTeamResults = GuiMysql.getInstance().getTeamResultsTest(match.getAwayTeam().getTeamId(), lookBack, false,
							match.getGameDate().format(DateTimeFormatter.ISO_DATE));

					final float awayWinPercent = (homeTeamResults.getLosses() + awayTeamResults.getWins())
							/ Float.valueOf(homeTeamResults.getCount() + awayTeamResults.getCount()) * 100;

					final float awayOdds = 100 / awayWinPercent;

					if (localDate.isBefore(match.getGameDate().toLocalDate())) {
						betAmount = bank * betLevel;
						localDate = match.getGameDate().toLocalDate();
					}

					if (awayOdds * betMarginDecimal <= match.getOdds2()) {
						betOnGameCount++;
						bank = bank - betAmount;
						if (match.getAwayScore() > match.getHomeScore()) {
							wins++;
							bank = bank + betAmount * match.getOdds2();
						}
					}

				}
				if (bestBankResult < bank) {
					System.out.println("New best bank " + bank + " with lookback " + lookBack + " and betMargin " + betMargin + " Bet on "
							+ betOnGameCount + " of " + matches.size() + "(" + betOnGameCount / (float) matches.size() + ") wins " + wins + "("
							+ wins / Float.valueOf(betOnGameCount) + ")" + " Win / game " + (bank - startingBank) / Float.valueOf(wins) + " kr");
					bestBetMargin = betMargin;
					bestLookBack = lookBack;
					bestBankResult = bank;
				} else {
					System.out.println("NOT BEST bank " + bank + " with lookback " + lookBack + " and betMargin " + betMargin + " Bet on "
							+ betOnGameCount + " of " + matches.size() + "(" + betOnGameCount / (float) matches.size() + ") wins " + wins + "("
							+ wins / Float.valueOf(betOnGameCount) + ")" + " Win / game " + (bank - startingBank) / Float.valueOf(wins) + " kr");
				}
			}
		}
	}

	public ArrayList<SoccerMatch> getMatches() {
		final ArrayList<SoccerMatch> result = new ArrayList<>();

		final String sql = "SELECT res.*, " + "hTeam.name as homeTeamName, aTeam.name as awayTeamName " + "FROM SoccerResults as res "
				+ "Join Team as hTeam ON res.homeTeam = hTeam.id " + "Join Team as aTeam ON res.awayTeam = aTeam.id " + "WHERE " + "res.leagueId = ? "
				+ "ORDER BY gameDate ASC";

		try {
			final PreparedStatement stat = getConnection().prepareStatement(sql);
			stat.setInt(1, leagueId);

			result.addAll(getMatches(stat));
		} catch (final SQLException e) {
			e.printStackTrace();
		}

		return result;
	}

}
