package pl.minidmnv.apple.source.fixture.service.parser;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

import org.jsoup.nodes.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import pl.minidmnv.apple.source.fixture.data.Fixture;
import pl.minidmnv.apple.source.fixture.data.FixtureResult;
import pl.minidmnv.apple.source.team.data.Team;

/**
 * @author mnicinski.
 */
@Component
public class FSFixtureParser {

	private static final Logger log = LoggerFactory.getLogger(FSFixtureParser.class);

	private static final String HOME_TEAM_PARAM = "CX÷";
	private static final String AWAY_TEAM_PARAM = "AF÷";
	private static final String DATE_PARAM = "AD÷";
	private static final String COMPETITION_TITLE = "title=\"";
	private static final String DATE_END_PARAM = "</span";
	private static final String DATE_BEGIN_PARAM = "date\">";
	private static final String TEAM_PARAM = "name\"><span>";

	private int findIndexOfParameter(String parameter, String elem, int elemIndex) {
		return elem.indexOf(parameter, elemIndex) + parameter.length();
	}

	private int findIndexOfParameter(String parameter, String elem) {
		return findIndexOfParameter(parameter, elem, 0);
	}

	private int getIndexOfParameterEnding(String elem, int elemIndex) {
		return findIndexOfParameter("¬", elem, elemIndex) - 1;
	}

	public Fixture transformStageScheduledElementToFixture(String elem) {
		log.info(elem);

		String detailsId = elem.substring(1, getIndexOfParameterEnding(elem, 0));

		int elemIndex = findIndexOfParameter(DATE_PARAM, elem);
		LocalDateTime date =
				Instant.ofEpochSecond(Long.valueOf(
						elem.substring(elemIndex, getIndexOfParameterEnding(elem, elemIndex))))
						.atZone(ZoneId.systemDefault())
						.toLocalDateTime();
		elemIndex = findIndexOfParameter(HOME_TEAM_PARAM, elem);
		Team homeTeam = new Team(elem.substring(elemIndex,
				getIndexOfParameterEnding(elem, elemIndex)));

		elemIndex = findIndexOfParameter(AWAY_TEAM_PARAM, elem);
		Team awayTeam = new Team(elem.substring(elemIndex,
				getIndexOfParameterEnding(elem, elemIndex)));

		return new Fixture(homeTeam, awayTeam, date, detailsId);
	}

	public FixtureResult transformDomResult(Element elem) {
		String resultsHtml = elem.html();
		log.debug("domResult: " + resultsHtml);

		LocalDateTime date = Instant.ofEpochSecond(
				Long.valueOf(resultsHtml.substring(findIndexOfParameter(DATE_BEGIN_PARAM, resultsHtml),
						findIndexOfParameter(DATE_END_PARAM, resultsHtml) - DATE_END_PARAM.length()))).atZone(ZoneId.systemDefault())
				.toLocalDateTime();
		int elemIndex = findIndexOfParameter(COMPETITION_TITLE, resultsHtml);
		String competition =  resultsHtml.substring(elemIndex, findIndexOfParameter("\"", resultsHtml,
				elemIndex));

		//TODO: dokonczyc implementacje wyciagania pozostalych elementow - druzyn, oraz wynikow
		Team homeTeam = new Team("homeTeam");
		Team awayTeam = new Team("awayTeam");
		Integer homeScore = 1;
		Integer awayScore = 0;

		return new FixtureResult(competition, homeTeam, awayTeam, homeScore, awayScore, date);
	}

}

@@ -970,24 +970,153 @@
 ARAM
- = %22name%5C%22%3E%3Cspan
+_END = %22span%3E%22;%0A%09private static final String TEAM_PARAM = %22name%5C%22%3E%3C%22 + TEAM_PARAM_END;%0A%09private static final String STRONG_LAB_REGEX = %22%3C/?strong
 %3E%22;%0A
@@ -2697,16 +2697,21 @@
 gth())))
+%0A%09%09%09%09
 .atZone(
@@ -2733,21 +2733,16 @@
 fault())
-%0A%09%09%09%09
 .toLocal
@@ -3036,83 +3036,490 @@
 w%0A%09%09
-Team homeTeam = new Team(%22homeTeam%22);%0A%09%09Team awayTeam = new Team(%22awayTeam%22
+elemIndex = findIndexOfParameter(TEAM_PARAM, resultsHtml);%0A%09%09Team homeTeam = new Team(cutOutStrongLab(resultsHtml.substring(elemIndex,%0A%09%09%09%09findIndexOfParameter(TEAM_PARAM_END, resultsHtml, elemIndex) - (TEAM_PARAM_END.length() + 2))));%0A%09%09elemIndex = findIndexOfParameter(TEAM_PARAM, resultsHtml, elemIndex);%0A%09%09Team awayTeam = new Team(cutOutStrongLab(resultsHtml.substring(elemIndex,%0A%09%09%09%09findIndexOfParameter(TEAM_PARAM_END, resultsHtml, elemIndex) - (TEAM_PARAM_END.length() + 2)))
 );%0A%09
@@ -3661,10 +3661,109 @@
 e);%0A%09%7D%0A%0A
+%09private String cutOutStrongLab(String text) %7B%0A%09%09return text.replaceAll(STRONG_LAB_REGEX, %22%22);%0A%09%7D%0A%0A
 %7D%0A

Modify fixture parser method to parse homeTeam name as well and almost awayTeam name.