package com.vaadin.prototype.wc.gwt.client.widgets;

import static com.google.gwt.query.client.GQuery.$;
import static com.google.gwt.query.client.GQuery.console;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.js.JsExport;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.query.client.GQuery;
import com.google.gwt.user.client.EventListener;
import com.vaadin.prototype.wc.gwt.client.html.HTMLElement;

@JsExport
public class WCUtils {
    @JsExport
    public static String formatCurrency(double val) {
        NumberFormat fmt = NumberFormat.getCurrencyFormat();
        return fmt.format(val);
    }

    public static int getAttrIntValue(HTMLElement el, String attr, int def) {
        return (int)getAttrFloatValue(el, attr, def);
    }

    public static double getAttrDoubleValue(HTMLElement el, String attr, double def) {
        return (double)getAttrFloatValue(el, attr, (float)def);
    }

    public static String getAttrValue(HTMLElement el, String attr, String def) {
        String val = el.getAttribute(attr);
        return val == null || val.isEmpty() ? def : val;
    }

    public static float getAttrFloatValue(HTMLElement el, String attr, float def) {
        try {
            return Float.valueOf(getAttrValue(el, attr, String.valueOf(def)));
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    public static void loadVaadinTheme(HTMLElement container, HTMLElement el, HTMLElement style, String def) {
        String theme = getAttrValue(el, "theme", def);
        if ($(theme).text().contains(theme)) {
            return;
        }
        GQuery l = $("link[href*='vaadin-x.html']");
        String base = GWT.getModuleBaseURL();
        if (!l.isEmpty()) {
            base = l.attr("href").replace("vaadin-x.html", "");
        } else if (base.contains("VAADIN/widgetsets")) {
            base += "../../../";
        }
        base += "VAADIN/themes/";
        $(style).text("@import url('" + base + theme + "/styles.css')");
        container.setAttribute("class", theme);
    }

    public static native void observe(JavaScriptObject jso, EventListener ev) /*-{
        var fnc = function(changes) {
          ev.@com.google.gwt.user.client.EventListener::onBrowserEvent(*)(null);
        };
        jso.__fnc = fnc;
        Object.observe(jso, fnc);
    }-*/;

    public static native void unobserve(JavaScriptObject jso) /*-{
        if (jso && jso.__fnc) {
          Object.unobserve(jso, jso.__fnc);
          jso.__fnc = undefined;
        }
    }-*/;
}

@@ -295,16 +295,58 @@
 Export;%0A
+import com.google.gwt.dom.client.Element;%0A
 import c
@@ -428,16 +428,62 @@
 GQuery;%0A
+import com.google.gwt.query.client.Predicate;%0A
 import c
@@ -1809,30 +1809,266 @@
 href
-*='vaadin-x
+%5D%22).filter(new Predicate()%7B%0A            public boolean f(Element e, int index) %7B%0A                String h = $(e).attr(%22href%22);%0A                console.log(h);%0A                return h.matches(%22.*(x-vaadin%7Cvaadin-x%7Cv-%5C%5Cw+)%5C%5C
 .html
-'%5D
 %22);%0A
+            %7D%0A        %7D);%0A
     
@@ -2182,18 +2182,26 @@
 lace
-(%22vaadin-x
+First(%22%5B%5C%5Cw%5C%5C-%5D+%5C%5C
 .htm
@@ -2341,16 +2341,40 @@
 themes/%22
+ + theme + %22/styles.css%22
 ;%0A      
@@ -2414,36 +2414,17 @@
  base + 
-theme + %22/styles.css
+%22
 ')%22);%0A  

fix FF & explorer issue on loading themes (bad GWT code when computing baseurl)