package org.jboss.as.console.client.shared.help;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import org.jboss.as.console.client.Console;
import org.jboss.as.console.client.widgets.nav.AriaLink;
import org.jboss.ballroom.client.widgets.forms.FormAdapter;
import org.jboss.dmr.client.ModelNode;

import java.util.List;

/**
 * Displays attribute descriptions for form items.
 *
 * @author Heiko Braun
 * @date 6/8/11
 */
public class FormHelpPanel {

    private DisclosurePanel helpPanel;
    private AddressCallback address;
    private FormAdapter form;
    private boolean hasBeenBuild;
    private boolean isAligned;

    public FormHelpPanel(AddressCallback address, FormAdapter form) {
        this.address = address;
        this.form = form;
    }

    public void setAligned(boolean b)
    {
        this.isAligned = b;
    }

    public Widget asWidget()
    {
        //ImageResource helpIcon = Icons.INSTANCE.noIcon();
        //helpPanel = new DisclosurePanel(helpIcon, helpIcon, "[help]");

        AriaLink header = new AriaLink (Console.CONSTANTS.help_need_help());
        header.addStyleName("help-panel-header");
        header.getElement().setAttribute("style" , "padding-top:5px;");
        header.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                helpPanel.setOpen(!helpPanel.isOpen());
            }
        });

        helpPanel = new DisclosurePanel(header);

        helpPanel.addStyleName( isAligned ? "help-panel-aligned" : "help-panel");

        final String popupStyle = isAligned ? "help-panel-aligned-open" : "help-panel-open";

        helpPanel.addOpenHandler(new OpenHandler<DisclosurePanel>() {

            @Override
            public void onOpen(OpenEvent<DisclosurePanel> event) {
                event.getTarget().addStyleName(popupStyle);
                helpPanel.getHeaderTextAccessor().setText(Console.CONSTANTS.help_close_help());
                buildAttributeHelp();
            }
        });

        helpPanel.addCloseHandler(new CloseHandler<DisclosurePanel>() {
            @Override
            public void onClose(CloseEvent<DisclosurePanel> event) {
                helpPanel.getHeaderTextAccessor().setText(Console.CONSTANTS.help_need_help());
                event.getTarget().removeStyleName(popupStyle);
            }
        });


        return helpPanel;

    }

    private void buildAttributeHelp() {

        if(!hasBeenBuild)
        {
            Console.getHelpSystem().getAttributeDescriptions(
                    address.getAddress(), form, new AsyncCallback<List<FieldDesc>>() {
                @Override
                public void onSuccess(List<FieldDesc> result) {
                    helpPanel.clear();

                    final SafeHtmlBuilder html = new SafeHtmlBuilder();
                    html.appendHtmlConstant("<table class='help-attribute-descriptions'>");
                    for(FieldDesc field : result)
                    {
                        html.appendHtmlConstant("<tr class='help-field-row'>");
                        html.appendHtmlConstant("<td class='help-field-name'>");

                        String ref = field.getRef();
                        String title = form.getFormItemTitle(ref);
                        html.appendEscaped(title).appendEscaped(": ");
                        html.appendHtmlConstant("</td>");
                        html.appendHtmlConstant("<td class='help-field-desc'>");
                        try {
                            html.appendHtmlConstant(field.getDesc());
                        } catch (Throwable e) {
                            // ignore parse errors
                            html.appendHtmlConstant("<i>Failed to parse description</i>");
                        }
                        html.appendHtmlConstant("</td>");
                        html.appendHtmlConstant("</tr>");
                    }
                    html.appendHtmlConstant("</table>");

                    helpPanel.add(new HTML(html.toSafeHtml()));
                    hasBeenBuild = true;
                }

                @Override
                public void onFailure(Throwable caught) {
                    Log.error("Failed to retrieve attribute description", caught.getMessage());
                    helpPanel.clear();
                    helpPanel.add(new HTML("<ul><li>Failed to retrieve attribute descriptions.</li></ul>"));
                }
            });
        }
    }

    public interface AddressCallback
    {
        ModelNode getAddress();
    }
}

@@ -3493,24 +3493,637 @@
 ptions'%3E%22);%0A
+%0A                    if(result.isEmpty())%0A                    %7B%0A                        Log.error(%22Failed to retrieve attribute descriptions: %22+address.getAddress());%0A%0A                        html.appendHtmlConstant(%22%3Ctr class='help-field-row'%3E%22);%0A                        html.appendHtmlConstant(%22%3Ctd class='help-field-name' colspan=2%3E%22);%0A                        html.appendEscaped(%22Attribute descriptions not available.%22);%0A                        html.appendHtmlConstant(%22%3C/td%3E%22);%0A                        html.appendHtmlConstant(%22%3C/tr%3E%22);%0A                    %7D%0A                    else%0A                    %7B%0A    
             
@@ -4172,34 +4172,42 @@
                 
+   
+ 
 %7B%0A
+    
                 
@@ -4286,32 +4286,36 @@
                 
+    
 html.appendHtmlC
@@ -4376,24 +4376,28 @@
             
+  
+  
 String ref =
@@ -4409,24 +4409,28 @@
 d.getRef();%0A
+    
             
@@ -4500,32 +4500,36 @@
                 
+    
 html.appendEscap
@@ -4575,32 +4575,36 @@
                 
+ 
+   
 html.appendHtmlC
@@ -4613,32 +4613,36 @@
 stant(%22%3C/td%3E%22);%0A
+    
                 
@@ -4702,24 +4702,28 @@
 d-desc'%3E%22);%0A
+    
             
@@ -4760,32 +4760,36 @@
                 
+    
 html.appendHtmlC
@@ -4838,16 +4838,20 @@
         
+    
 %7D catch 
@@ -4894,16 +4894,20 @@
         
+   
+ 
 // ignor
@@ -4921,16 +4921,20 @@
  errors%0A
+    
         
@@ -5032,34 +5032,40 @@
                 
-%7D%0A
+    %7D%0A  
                 
@@ -5064,32 +5064,34 @@
                 
+  
 html.appendHtmlC
@@ -5124,32 +5124,36 @@
                 
+    
 html.appendHtmlC
@@ -5162,32 +5162,58 @@
 stant(%22%3C/tr%3E%22);%0A
+                        %7D%0A
                 

Fix help panel when no desc available