/**
 *
 */
package com.catpeds.crawler.pawpeds;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
import java.util.function.Function;

import javax.inject.Inject;

import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.catpeds.crawler.jsoup.DocumentRepository;
import com.catpeds.model.PedigreeSearchCriteria;
import com.catpeds.model.PedigreeSearchResult;

/**
 * {@link PawpedsRepository} default implementation.
 *
 * @author padriano
 *
 */
class PawpedsRepositoryImpl implements PawpedsRepository {

	private static final Logger LOGGER = LoggerFactory.getLogger(PawpedsRepositoryImpl.class);

	private DocumentRepository documentRepository;
	private PawpedsUrlService pawpedsUrlService;
	private PawpedsDocumentParser pawpedsSearchResultParser;

	@Inject
	PawpedsRepositoryImpl(DocumentRepository documentRepository, PawpedsUrlService pawpedsUrlService,
			PawpedsDocumentParser pawpedsSearchResultParser) {
		this.documentRepository = documentRepository;
		this.pawpedsUrlService = pawpedsUrlService;
		this.pawpedsSearchResultParser = pawpedsSearchResultParser;
	}

	/**
	 * @see com.catpeds.crawler.pawpeds.PawpedsRepository#findAll(com.catpeds.model.PedigreeSearchCriteria)
	 */
	@Override
	public Collection<PedigreeSearchResult> findAll(PedigreeSearchCriteria criteria) {
		String searchUrl = pawpedsUrlService.getAdvancedSearchUrl(criteria);
		return findAndParseawpedsDocument(searchUrl, document -> pawpedsSearchResultParser.parseSearch(document));
	}

	/**
	 * @see com.catpeds.crawler.pawpeds.PawpedsRepository#findAllOffspring(long)
	 */
	@Override
	public Collection<PedigreeSearchResult> findAllOffspring(long id) {
		String offspringSearchUrl = pawpedsUrlService.getOffspringsSearchUrl(id);
		return findAndParseawpedsDocument(offspringSearchUrl, document -> pawpedsSearchResultParser.parseOffsprings(document));
	}

	Collection<PedigreeSearchResult> findAndParseawpedsDocument(String url,
			Function<Document, Collection<PedigreeSearchResult>> parserFunction) {
		try {
			Optional<Document> searchDocument = documentRepository.get(url);
			if (searchDocument.isPresent()) {
				// successful retrieving the document at first try
				return parserFunction.apply(searchDocument.get());
			}
			// this means that there was a timeout. retrying...
			searchDocument = documentRepository.get(url);
			if (searchDocument.isPresent()) {
				return parserFunction.apply(searchDocument.get());
			}

			LOGGER.warn("Timeout retrieving document from {}", url);

		} catch (IOException e) {
			LOGGER.error("Unexpected exception occurred", e);
		}

		return Arrays.asList();
	}
}

@@ -1459,36 +1459,35 @@
 );%0A%09%09return 
-find
+get
 AndParse
 awpedsDocume
@@ -1466,32 +1466,33 @@
 turn getAndParse
+P
 awpedsDocument(s
@@ -1496,36 +1496,24 @@
 t(searchUrl,
- document -%3E
  pawpedsSear
@@ -1522,25 +1522,26 @@
 ResultParser
-.
+::
 parseSearch(
@@ -1539,26 +1539,16 @@
 seSearch
-(document)
 );%0A%09%7D%0A%0A%09
@@ -1799,28 +1799,27 @@
 %09return 
-find
+get
 AndParse
 awpedsDo
@@ -1802,32 +1802,33 @@
 turn getAndParse
+P
 awpedsDocument(o
@@ -1849,20 +1849,8 @@
 Url,
- document -%3E
  paw
@@ -1871,17 +1871,18 @@
 ltParser
-.
+::
 parseOff
@@ -1892,18 +1892,8 @@
 ings
-(document)
 );%0A%09
@@ -1933,20 +1933,19 @@
 lt%3E 
-find
+get
 AndParse
 awpe
@@ -1940,16 +1940,17 @@
 AndParse
+P
 awpedsDo

Use method reference instead of lambda