package cl.fatman.capital.fund;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import org.apache.log4j.Logger;

import java.io.IOException;
import java.time.LocalDate;
import java.util.Map;
import java.util.HashMap;

public class ExtractFundData {
	
	private String link;
	private String userAgent;
	static final Logger logger = Logger.getLogger(ExtractFundData.class);
	
	
	public ExtractFundData(String link, String userAgent) {
		super();
		this.link = link;
		this.userAgent = userAgent;
	}
	
	
	public String getLink() {
		return link;
	}
	
	
	public void setLink(String link) {
		this.link = link;
	}
	
	
	public String getUserAgent() {
		return userAgent;
	}
	
	
	public void setUserAgent(String userAgent) {
		this.userAgent = userAgent;
	}
	
	/*
	 * Method to retrieve the fund information and fund rate from the AAFM web page using a date and fund type.
	 * We will store the fund data in Hash Map.
	 */
	public Map<Fund, Double> getFundData(LocalDate queryDate, String typeName, int typeNumber) {
		logger.debug("getFundData(LocalDate queryDate, String typeName, int typeNumber)");
		String url = link + "?clase=informe&metodo=rentabilidad_html&inversion=%&administradora=%&tipo=" + typeNumber 
				+ "&dia=" + queryDate.getDayOfMonth() + "&mes=" + queryDate.getMonthValue() 
				+ "&anio=" + queryDate.getYear();
		logger.debug("User Agent: " + userAgent);
		logger.debug("URL: " + url);
		Map<Fund, Double> fundMap = new HashMap<Fund, Double>();
		try {
			logger.info("Start processing table data from AAFM web page.");
			Document document = Jsoup.connect(url).userAgent(userAgent).get();
			Elements rows = document.select("table").get(3).select("tr");
			for (Element row : rows) {
				Elements cols = row.select("td");
				String institution = cols.get(0).text().replace("\u00a0","");
				//Discard header row, summary row and empty rows.
				if (institution.isEmpty() || institution.contains("Administradora")) continue;
				if (institution.contains("TOTALES")) break;
				String run = cols.get(1).text().replace("\u00a0","");
				String name = cols.get(2).text().replace("\u00a0","");
				String series = cols.get(3).text().replace("\u00a0","");
				String tmpRate = cols.get(4).text().replace(".", "");
				//Discard rows without a fund rate.
				if (tmpRate.contains("\u00a0")) continue;
				Double rate = Double.parseDouble(tmpRate.replace(",", "."));
				logger.debug(institution + " " + name + " " + run + " " + series + " " + typeName + " " + rate);
				Fund tmpFund = new Fund(name, run, series, institution, typeName);
				fundMap.put(tmpFund, rate);
				logger.debug("Fund data created and added to the HashMap.");
			}
			logger.info("Finish processing table data from AAFM web page. Fund HashMap successfully created.");
		} catch (IOException e) {
			fundMap = null;
			logger.error("Problem retrieving AAFM web page.", e);
		} catch (IllegalArgumentException e) {
			fundMap = null;
			logger.error("The URL is no longer valid.", e);
		}
		return fundMap;
	}
}
@@ -211,16 +211,48 @@
 eption;%0A
+import java.text.MessageFormat;%0A
 import j
@@ -271,16 +271,16 @@
 alDate;%0A
-
 import j
@@ -402,16 +402,43 @@
 rAgent;%0A
+%09private String baseQuery;%0A
 %09static 
@@ -502,18 +502,16 @@
 ass);%0A%09%0A
-%09%0A
 %09public 
@@ -551,24 +551,42 @@
 ng userAgent
+, String baseQuery
 ) %7B%0A%09%09super(
@@ -630,39 +630,67 @@
 nt = userAgent;%0A
+%09%09this.baseQuery = baseQuery;%0A
 %09%7D%0A%09%0A
-%09%0A
 %09public String g
@@ -712,34 +712,32 @@
 turn link;%0A%09%7D%0A%09%0A
-%09%0A
 %09public void set
@@ -777,26 +777,24 @@
  link;%0A%09%7D%0A%09%0A
-%09%0A
 %09public Stri
@@ -838,18 +838,16 @@
 t;%0A%09%7D%0A%09%0A
-%09%0A
 %09public 
@@ -919,16 +919,154 @@
 t;%0A%09%7D%0A%09%0A
+%09public String getBaseQuery() %7B%0A%09%09return baseQuery;%0A%09%7D%0A%09%0A%09public void setBaseQuery(String baseQuery) %7B%0A%09%09this.baseQuery = baseQuery;%0A%09%7D%0A%09%0A
 %09/*%0A%09 * 
@@ -1418,222 +1418,41 @@
 l = 
-link + %22?clase=informe&metodo=rentabilidad_html&inversion=%25&administradora=%25&tipo=%22 + typeNumber %0A%09%09%09%09+ %22&dia=%22 + queryDate.getDayOfMonth() + %22&mes=%22 + queryDate.getMonthValue() %0A%09%09%09%09+ %22&anio=%22 + queryDate.getYear(
+this.getURL(queryDate, typeNumber
 );%0A%09
@@ -3117,9 +3117,361 @@
 Map;%0A%09%7D%0A
+%09%0A%09private String getURL(LocalDate queryDate, int typeNumber) %7B%0A%09%09String url = %22%22;%0A%09%09MessageFormat query = new MessageFormat(baseQuery);%0A%09%09Object%5B%5D queryObjects = %7BtypeNumber, queryDate.getDayOfMonth(), queryDate.getMonthValue(), %0A%09%09%09%09                 Integer.toString(queryDate.getYear())%7D;%0A%09%09url = link + query.format(queryObjects);%0A%09%09return url;%0A%09%7D%0A
 %7D

Change message format to avoid include a point in the URL.