package org.jboss.as.console.client.shared.subsys.jca.wizard;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import org.jboss.as.console.client.Console;
import org.jboss.as.console.client.shared.help.FormHelpPanel;
import org.jboss.as.console.client.shared.subsys.Baseadress;
import org.jboss.as.console.client.shared.subsys.jca.model.ResourceAdapter;
import org.jboss.ballroom.client.widgets.forms.ComboBoxItem;
import org.jboss.ballroom.client.widgets.forms.Form;
import org.jboss.ballroom.client.widgets.forms.FormValidation;
import org.jboss.ballroom.client.widgets.forms.TextBoxItem;
import org.jboss.ballroom.client.widgets.window.DialogueOptions;
import org.jboss.ballroom.client.widgets.window.WindowContentBuilder;
import org.jboss.dmr.client.ModelNode;

/**
 * @author Heiko Braun
 * @date 7/20/11
 */
public class AdapterStep1 {

    NewAdapterWizard parent;

    public AdapterStep1(NewAdapterWizard parent) {
        this.parent = parent;
    }

    public Widget asWidget() {

        VerticalPanel layout = new VerticalPanel();
        layout.setStyleName("window-content");

        layout.add(new HTML("<h2>"+ Console.CONSTANTS.subsys_jca_ra_step1()+"</h2>"));

        final Form<ResourceAdapter> form = new Form(ResourceAdapter.class);

        TextBoxItem archiveItem = new TextBoxItem("archive", "Archive");

        // TODO: https://issues.jboss.org/browse/AS7-1346
        //TextBoxItem nameItem = new TextBoxItem("name", "Name");

        TextBoxItem jndiItem = new TextBoxItem("jndiName", "JNDI");
        TextBoxItem classItem = new TextBoxItem("connectionClass", "Connection Class");
        ComboBoxItem txItem = new ComboBoxItem("transactionSupport", "TX");
        txItem.setDefaultToFirstOption(true);
        txItem.setValueMap(new String[]{"NoTransaction", "LocalTransaction", "XATransaction"});

        form.setFields(archiveItem, jndiItem, classItem, txItem);

        final FormHelpPanel helpPanel = new FormHelpPanel(
                new FormHelpPanel.AddressCallback() {
                    @Override
                    public ModelNode getAddress() {
                        ModelNode address = Baseadress.get();
                        address.add("subsystem", "resource-adapters");
                        address.add("resource-adapter", "*");
                        return address;
                    }
                }, form
        );
        layout.add(helpPanel.asWidget());

        layout.add(form.asWidget());

        DialogueOptions options = new DialogueOptions(

                // save
                "Next &rsaquo;&rsaquo;",
                new ClickHandler() {
                    @Override
                    public void onClick(ClickEvent event) {
                        FormValidation validation = form.validate();
                        if(!validation.hasErrors())
                            parent.onCompleteStep1(form.getUpdatedEntity());
                    }
                },

                // cancel
                "cancel",
                new ClickHandler() {
                    @Override
                    public void onClick(ClickEvent event) {
                        parent.getPresenter().closeDialoge();
                    }
                }

        );

        // ----------------------------------------

        return new WindowContentBuilder(layout, options).build();
    }
}

@@ -1646,32 +1646,33 @@
 ame%22);%0A%0A        
+ 
 TextBoxItem jndi
@@ -1671,20 +1671,20 @@
 tem jndi
-Item
+Name
  = new T
@@ -1711,18 +1711,494 @@
 %22, %22JNDI
-%22)
+ Name%22) %7B%0A            @Override%0A            public boolean validate(String value) %7B%0A%0A                boolean isSet = value!=null && !value.isEmpty();%0A                boolean validPrefix = value.startsWith(%22java:/%22) %7C%7C value.startsWith(%22java:jboss/%22);%0A                return isSet&&validPrefix;%0A            %7D%0A%0A            @Override%0A            public String getErrMessage() %7B%0A                return %22JNDI name has to start with 'java:/' or 'java:jboss/'%22;%0A            %7D%0A        %7D
 ;%0A      
@@ -2538,20 +2538,20 @@
 em, jndi
-Item
+Name
 , classI

verify RA jndi names