package eu.ydp.empiria.player.client.controller.style;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.JavaScriptObject;

import eu.ydp.gwtutil.client.collections.QueueSet;

public class StyleLinkManager {

	public StyleLinkManager(){
		solidStyles = new HashMap<String, JavaScriptObject>();
		removableStyles = new ArrayList<JavaScriptObject>();
	}

	private final Map<String, JavaScriptObject> solidStyles;
	private final List<JavaScriptObject> removableStyles;

	public void registerAssessmentStyles(QueueSet<String> styleLinks){
		doRegisterStyleLinks(styleLinks, false);
	}

	public void registerItemStyles(QueueSet<String> styleLinks){
		doRegisterStyleLinks(styleLinks, true);
	}

	private void doRegisterStyleLinks(QueueSet<String> styleLinks, boolean areRemovable){

		if (areRemovable) {
			removableStyles.clear();
		}

		for (String link : styleLinks){
			if (solidStyles.containsKey(link)) {
				JavaScriptObject styleLink = solidStyles.get(link);
				if (styleLink !=  null) {
					removeStyleLink(styleLink);
				}
				solidStyles.remove(link);
				JavaScriptObject newLink = appendStyleLink(link);
				solidStyles.put(link, newLink);
			} else if (!areRemovable) {
				JavaScriptObject newLink = appendStyleLink(link);
				solidStyles.put(link, newLink);
			} else {
				JavaScriptObject newLink = appendStyleLink(link);
				removableStyles.add(newLink);
			}
		}

	}

	public native String getUserAgent() /*-{
		return navigator.userAgent;
	}-*/;

	private native JavaScriptObject appendStyleLink(String link) /*-{
		var headID = $wnd.document.getElementsByTagName("head")[0];
		var cssNode = $wnd.document.createElement('link');
		cssNode.type = 'text/css';
		cssNode.rel = 'stylesheet';
		cssNode.href = link;
		cssNode.media = 'screen';
		headID.appendChild(cssNode);
		return cssNode;
	}-*/;

	private native void removeStyleLink(JavaScriptObject cssNode) /*-{
		try {
			var headID = $wnd.document.getElementsByTagName("head")[0];
			headID.removeChild(cssNode);
        } catch (e) {
			cssNode.parentNode.removeChild(cssNode);
		}
	}-*/;

}

@@ -72,68 +72,15 @@
 til.
-ArrayList;%0D%0Aimport java.util.HashMap;%0D%0Aimport java.util.List
+HashMap
 ;%0D%0Ai
@@ -249,271 +249,106 @@
  %7B%0D%0A
-%0D%0A%09public StyleLinkManager()%7B%0D%0A%09%09solidStyles = new HashMap%3CString, JavaScriptObject%3E();%0D%0A%09%09removableStyles = new ArrayList%3CJavaScriptObject%3E();%0D%0A%09%7D%0D%0A%0D%0A%09private final Map%3CString, JavaScriptObject%3E solidStyles;%0D%0A%09private final List%3CJavaScriptObject%3E removableStyles
+%09private final Map%3CString, JavaScriptObject%3E solidStyles = new HashMap%3CString, JavaScriptObject%3E()
 ;%0D%0A%0D
@@ -406,32 +406,33 @@
 ing%3E styleLinks)
+ 
 %7B%0D%0A%09%09doRegisterS
@@ -524,24 +524,25 @@
  styleLinks)
+ 
 %7B%0D%0A%09%09doRegis
@@ -670,70 +670,10 @@
 ble)
+ 
 %7B
-%0D%0A%0D%0A%09%09if (areRemovable) %7B%0D%0A%09%09%09removableStyles.clear();%0D%0A%09%09%7D%0D%0A
 %0D%0A%09%09
@@ -706,337 +706,147 @@
 nks)
+ 
 %7B%0D%0A%09%09%09
-if (solidStyles.containsKey
+addStyleIfNotPresent
 (link)
-) %7B
+;
 %0D%0A%09%09
-%09%09JavaScriptObject styleLink = solidStyles.get(link);%0D%0A%09%09%09%09if (styleLink !=  null) %7B%0D%0A%09%09%09%09%09removeStyleLink(styleLink);%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%09solidStyles.remove(link);%0D%0A%09%09%09%09JavaScriptObject newLink = appendStyleLink(link);%0D%0A%09%09%09%09solidStyles.put(link, newLink);%0D%0A%09%09%09%7D else if (!areRemovable
+%7D%0D%0A%09%7D%0D%0A%0D%0A%09private void addStyleIfNotPresent(String link) %7B%0D%0A%09%09if (!solidStyles.containsKey(link)
 ) %7B%0D%0A
-%09
 %09%09%09J
@@ -898,17 +898,16 @@
 k);%0D%0A%09%09%09
-%09
 solidSty
@@ -935,124 +935,13 @@
 );%0D%0A
-%09%09%09%7D else %7B%0D%0A%09%09%09%09JavaScriptObject newLink = appendStyleLink(link);%0D%0A%09%09%09%09removableStyles.add(newLink);%0D%0A%09%09%09%7D%0D%0A
 %09%09%7D%0D%0A
-%0D%0A
 %09%7D%0D%0A
@@ -1565,16 +1565,10 @@
 );%0D%0A
-        
+%09%09
 %7D ca

repair multiple css style loading