package com.github.spirylics.xgwt.polymer;

import com.google.gwt.query.client.Function;
import com.google.gwt.query.client.GQuery;
import com.google.gwt.query.client.js.JsUtils;
import com.google.gwt.user.client.ui.HTMLPanel;

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

public class Dialog extends HTMLPanel {

    static Dialog main;

    public Dialog() {
        super("paper-dialog", "<h2 class=\"dialog-header\"></h2>" +
                "<paper-dialog-scrollable class=\"dialog-content\"></paper-dialog-scrollable>" +
                "<div class=\"buttons\">" +
                "<paper-button dialog-dismiss>OK</paper-button>" +
                "<paper-button dialog-confirm autofocus>OK</paper-button>" +
                "</div>");
        $(this)
                .attr("entry-animation", "scale-up-animation")
                .attr("exit-animation", "fade-out-animation")
                .attr("with-backdrop", "");
    }

    public static Dialog main() {
        if (main == null) {
            main = new Dialog();
            $(body).append(main.getElement());
        }
        $(main).off();
        return main.close();
    }

    public Dialog setHeader(String html) {
        GQuery header = $(this).find(".dialog-header").empty();
        if (html == null) {
            header.hide();
        } else {
            header.html(html).show();
        }
        return this;
    }

    public Dialog setContent(String html) {
        GQuery content = $(this).find(".dialog-content").empty();
        if (content == null) {
            content.hide();
        } else {
            content.html(html).show();
        }
        return this;
    }

    public Dialog open() {
        JsUtils.jsni(getElement(), "open");
        return this;
    }

    public Dialog close() {
        JsUtils.jsni(getElement(), "close");
        return this;
    }

    public Dialog onceOpen(final Function function) {
        return onOpen(new Function() {
            @Override
            public void f() {
                function.f(getEvent(), getArguments());
                offOpen(this);
            }
        });
    }

    public Dialog onceClose(final Function function) {
        return onClose(new Function() {
            @Override
            public void f() {
                function.f(getEvent(), getArguments());
                offClose(this);
            }
        });
    }

    public Dialog onOpen(Function function) {
        return on("iron-overlay-opened", function);
    }

    public Dialog onClose(Function function) {
        return on("iron-overlay-closed", function);
    }

    public Dialog offOpen(Function function) {
        return off("iron-overlay-opened", function);
    }

    public Dialog offClose(Function function) {
        return off("iron-overlay-closed", function);
    }

    public Dialog off(String event, Function function) {
        $(this).off(event, function);
        return this;
    }

    public Dialog on(String event, Function function) {
        $(this).on(event, function);
        return this;
    }

}

@@ -37,16 +37,140 @@
 lymer;%0A%0A
+import com.google.gwt.event.logical.shared.ValueChangeEvent;%0Aimport com.google.gwt.event.logical.shared.ValueChangeHandler;%0A
 import c
@@ -206,16 +206,16 @@
 nction;%0A
-
 import c
@@ -296,16 +296,59 @@
 sUtils;%0A
+import com.google.gwt.user.client.History;%0A
 import c
@@ -1148,32 +1148,1031 @@
 p%22, %22%22);%0A    %7D%0A%0A
+    public Dialog(final String historyToken) %7B%0A        this();%0A        final String dialogParameter = %22;dialog=%22 + historyToken;%0A        History.addValueChangeHandler(new ValueChangeHandler%3CString%3E() %7B%0A            @Override%0A            public void onValueChange(ValueChangeEvent%3CString%3E event) %7B%0A                if (!event.getValue().contains(dialogParameter)) %7B%0A                    close();%0A                %7D else %7B%0A                    open();%0A                %7D%0A            %7D%0A        %7D);%0A        onOpen(new Function() %7B%0A            @Override%0A            public void f() %7B%0A                if (!History.getToken().contains(dialogParameter)) %7B%0A                    History.newItem(History.getToken() + dialogParameter, false);%0A                %7D%0A            %7D%0A        %7D).onClose(new Function() %7B%0A            @Override%0A            public void f() %7B%0A                if (History.getToken().contains(dialogParameter)) %7B%0A                    History.back();%0A                %7D%0A            %7D%0A        %7D);%0A    %7D%0A%0A
     public stati
@@ -2247,16 +2247,29 @@
  Dialog(
+%22dialog_main%22
 );%0A     
@@ -2324,31 +2324,8 @@
   %7D%0A
-        $(main).off();%0A
     
@@ -2343,16 +2343,8 @@
 main
-.close()
 ;%0A  

add collection utils