package de.florianmarsch.fussballmanager.db.entity.lineup;

import java.io.InputStream;
import java.net.URL;
import java.text.Normalizer;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import de.florianmarsch.fussballmanager.db.entity.trainer.Trainer;
import de.florianmarsch.fussballmanager.db.service.AbstractService;
import de.florianmarsch.fussballmanager.db.entity.lineup.QLineUp;
import de.florianmarsch.fussballmanager.db.entity.player.Player;
import de.florianmarsch.fussballmanager.db.entity.player.PlayerService;
import de.florianmarsch.fussballmanager.db.entity.player.QPlayer;

public class LineUpService extends AbstractService<LineUp> {

	PlayerService ps = new PlayerService();
	Map<String, Player> players = ps.getAllOrderedInMap(
			QPlayer.player.abbreviationName);
	
	public LineUpService() {
		super(QLineUp.lineUp);
	}

	@Override
	public LineUp getNewInstance() {
		return new LineUp();
	}
	
	public Set<String> reciveCurrentLineUp(Trainer trainer){
		try {
			String html = null;
			String urlString = trainer.getUrl();
			InputStream is = (InputStream) new URL(urlString).getContent();
			html = IOUtils.toString(is, "UTF-8");

			JSONArray lineUps = new JSONObject(html).getJSONArray("data");
			
			Set<String> teamList = new HashSet<String>();
			for (int i = 0; i < lineUps.length(); i++) {
				String tempName = lineUps.getString(i);
				String name = getMatchedName(tempName);
				if(name != null){
					teamList.add(name);
				}
			}
			System.out.println(teamList);
			return teamList;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("abbruch", e);
		}
	}
	
	public String getMatchedName(String name) {
		System.out.println("check " + name);
		boolean contains = players.containsKey(name);
		if(contains){
			return players.get(name).getName();
		}else{
			return null;
		}
	}
	
}

@@ -940,142 +940,8 @@
  %7B%0A%0A
-%09PlayerService ps = new PlayerService();%0A%09Map%3CString, Player%3E players = ps.getAllOrderedInMap(%0A%09%09%09QPlayer.player.abbreviationName);%0A%09%0A
 %09pub
@@ -962,16 +962,16 @@
 ice() %7B%0A
+
 %09%09super(
@@ -1058,25 +1058,24 @@
 ineUp();%0A%09%7D%0A
-%09
 %0A%09public Set
@@ -1119,16 +1119,17 @@
 trainer)
+ 
 %7B%0A%09%09try 
@@ -1372,12 +1372,9 @@
 %22);%0A
-%09%09%09
 %0A
+
 %09%09%09S
@@ -1474,52 +1474,91 @@
 %09%09%09%09
-String tempName = lineUps.getString(i);
+JSONObject player = lineUps.getJSONObject(i);%0A%0A%09%09%09%09if (player.has(%22match%22)) %7B
 %0A%09%09%09%09
+%09
 Stri
@@ -1571,55 +1571,56 @@
 e = 
-getMatchedName(tempName);%0A%09%09%09%09if(name != null)%7B
+player.getJSONObject(%22match%22).getString(%22name%22);
 %0A%09%09%09
@@ -1647,16 +1647,17 @@
 ;%0A%09%09%09%09%7D%0A
+%0A
 %09%09%09%7D%0A%09%09%09
@@ -1812,229 +1812,7 @@
 %0A%09%7D%0A
-%09%0A%09public String getMatchedName(String name) %7B%0A%09%09System.out.println(%22check %22 + name);%0A%09%09boolean contains = players.containsKey(name);%0A%09%09if(contains)%7B%0A%09%09%09return players.get(name).getName();%0A%09%09%7Delse%7B%0A%09%09%09return null;%0A%09%09%7D%0A%09%7D%0A%09
 %0A%7D%0A

change lineup