package org.nuxeo.opensocial.container.client.view;

import org.nuxeo.opensocial.container.client.ContainerConstants;
import org.nuxeo.opensocial.container.client.ContainerEntryPoint;
import org.nuxeo.opensocial.container.client.ContainerMessages;
import org.nuxeo.opensocial.container.client.bean.GadgetBean;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.Function;
import com.gwtext.client.widgets.Tool;
import com.gwtext.client.widgets.portal.PortalColumn;

/**
* @author Guillaume Cusnieux
*/
public class GadgetTools {

  private final static ContainerConstants CST = GWT.create(ContainerConstants.class);
  private final static ContainerMessages MSG = GWT.create(ContainerMessages.class);
  private GadgetForm form;
  private GadgetPortlet portlet;
  private String title;

  public GadgetTools(GadgetPortlet portlet) {
    this.portlet = portlet;
  }

  public Tool[] getButtons() {
    if (portlet.getView()
        .equals(GadgetPortlet.CANVAS_VIEW)) {
      return getCanvasButtons();
    } else {
      return getDefaultButtons();
    }
  }

  private Tool[] getCanvasButtons() {
    final Tool min = new Tool(Tool.MINIMIZE, new Function() {
      public void execute() {
        ContainerPortal portal = ContainerEntryPoint.getContainerPortal();
        PortalColumn maximizedCol = portal.getMaximizedCol();
        maximizedCol.remove(portlet.getId(), true);
        showManager();
        for (PortalColumn col : portal.getPortalColumns()) {
          col.show();
        }
        ;
        maximizedCol.hide();
        updateLayoutSizeForMin(ContainerEntryPoint.PANEL_WIDTH + "px");

      }

      private native void updateLayoutSizeForMin(String width)
      /*-{
      $wnd.$("#containerPortal").width(width);
      $wnd.$(".containerPortal").width(width);
      $wnd.$(".x-column-inner").width(width);
      $wnd.$("#containerPanel").width(width);
      }-*/;

    });
    return new Tool[] { min };

  }

  private Tool[] getDefaultButtons() {
    final GadgetBean gadget = portlet.getGadgetBean();
    if (gadget.getPermission()) {
      Tool gear = new Tool(Tool.GEAR, new Function() {
        public void execute() {
          launchGear();
        }
      });

      Tool close = new Tool(Tool.CLOSE, new Function() {
        public void execute() {
          if (Window.confirm(MSG.askedDeleteGadget(title))) {
            ContainerEntryPoint.getService()
                .removeGadget(gadget, ContainerEntryPoint.getGwtParams(),
                    new AsyncCallback<GadgetBean>() {
                      public void onFailure(Throwable arg0) {
                        ContainerPortal.showErrorMessage(CST.error(),
                            CST.deleteError());
                      }

                      public void onSuccess(GadgetBean gadget) {
                        ContainerEntryPoint.getContainerPortal()
                            .removeGadgetPortlet(portlet.getId());
                      }
                    });
          }
        }

      });
      Tool max = new Tool(Tool.MAXIMIZE, new Function() {

        public void execute() {
          ContainerPortal portal = ContainerEntryPoint.getContainerPortal();
          final GadgetBean gadget = portlet.getGadgetBean();
          PortalColumn maximizedCol = portal.getMaximizedCol();
          GadgetPortlet canvas = new GadgetPortlet(gadget,
              GadgetPortlet.CANVAS_VIEW);
          ContainerPortal.setMaximizedPortlet(canvas);
          maximizedCol.add(canvas);
          hideManager();
          for (PortalColumn col : portal.getPortalColumns()) {
            col.hide();
          }
          ;
          maximizedCol.show();
          canvas.updateGadgetPortlet();
          canvas.doLayout();
          maximizedCol.doLayout();
          updateLayoutSizeForMax(canvas.getId());
          if (!portal.isCollapsed())
            canvas.unCollapseGadget();
        }

        private native void updateLayoutSizeForMax(String id)
        /*-{
        $wnd.$("#containerPortal").width("100%");
        $wnd.$(".containerPortal").width("100%");
        $wnd.$("#containerPanel").width("100%");
        $wnd.$(".x-column-inner").width("100%");
        $wnd.$("#maximizedCol").attr("style","width:100%;paddinf:0;margin:0;");
        $wnd.$("#"+id).attr("style","width:100%;paddinf:0;");
        }-*/;

      });

      return new Tool[] { max, gear, close };
    }
    return new Tool[] {};

  }

  public void setGadgetForm(GadgetForm form) {
    this.form = form;
  }

  private static native void hideManager()
  /*-{
    $wnd.jQuery(".manager").slideUp("fast");
    $wnd.jQuery(".getManager").slideUp("fast");
  }-*/;

  private static native void showManager()
  /*-{
   $wnd.jQuery(".getManager").slideDown("fast");
  }-*/;

  public void launchGear() {
    if (form != null)
      form.showForm();
  }

  public void setTitle(String title) {
    this.title = title;
  }
}
@@ -1797,33 +1797,38 @@
 /*-%7B%0A      $wnd.
-$
+jQuery
 (%22#containerPort
@@ -1849,33 +1849,38 @@
 th);%0A      $wnd.
-$
+jQuery
 (%22.containerPort
@@ -1901,33 +1901,38 @@
 th);%0A      $wnd.
-$
+jQuery
 (%22.x-column-inne
@@ -1952,33 +1952,38 @@
 th);%0A      $wnd.
-$
+jQuery
 (%22#containerPane
@@ -4098,33 +4098,38 @@
 -%7B%0A        $wnd.
-$
+jQuery
 (%22#containerPort
@@ -4161,17 +4161,22 @@
    $wnd.
-$
+jQuery
 (%22.conta
@@ -4216,17 +4216,22 @@
    $wnd.
-$
+jQuery
 (%22#conta
@@ -4270,17 +4270,22 @@
    $wnd.
-$
+jQuery
 (%22.x-col
@@ -4324,17 +4324,22 @@
    $wnd.
-$
+jQuery
 (%22#maxim
@@ -4379,17 +4379,17 @@
 %25;paddin
-f
+g
 :0;margi
@@ -4409,17 +4409,22 @@
    $wnd.
-$
+jQuery
 (%22#%22+id)

Replace $ by jQuery in JSNI methods