package org.jboss.as.console.client.domain.hosts;

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.domain.model.Server;
import org.jboss.as.console.client.domain.model.ServerGroupRecord;
import org.jboss.as.console.client.shared.help.FormHelpPanel;
import org.jboss.as.console.client.widgets.forms.FormToolStrip;
import org.jboss.ballroom.client.widgets.forms.CheckBoxItem;
import org.jboss.ballroom.client.widgets.forms.ComboBoxItem;
import org.jboss.ballroom.client.widgets.forms.Form;
import org.jboss.ballroom.client.widgets.forms.NumberBoxItem;
import org.jboss.ballroom.client.widgets.forms.TextItem;
import org.jboss.ballroom.client.widgets.tables.DefaultCellTable;
import org.jboss.ballroom.client.widgets.window.Feedback;
import org.jboss.dmr.client.ModelNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Heiko Braun
 * @date 10/27/11
 */
public class ServerConfigDetails {

    private ServerConfigPresenter presenter;
    private Form<Server> form;
    private ComboBoxItem socketItem;
    private ComboBoxItem groupItem;

    public ServerConfigDetails(ServerConfigPresenter presenter) {
        this.presenter = presenter;
    }

    Widget asWidget() {
        VerticalPanel layout = new VerticalPanel();
        layout.setStyleName("fill-layout-width");

        form = new Form<Server>(Server.class);

        FormToolStrip<Server> toolStrip = new FormToolStrip<Server>(
                form, new FormToolStrip.FormCallback<Server>() {

            @Override
            public void onSave(Map<String, Object> changeset) {
                presenter.onSaveChanges(form.getUpdatedEntity(), changeset);
            }

            @Override
            public void onDelete(Server entity) {
                Feedback.confirm(
                        Console.MESSAGES.deleteServerConfig(),
                        Console.MESSAGES.deleteServerConfigConfirm(form.getEditedEntity().getName()),
                        new Feedback.ConfirmationHandler() {
                            @Override
                            public void onConfirmation(boolean isConfirmed) {
                                if (isConfirmed)
                                    presenter.tryDelete(form.getEditedEntity());
                            }
                        });
            }
        });

        toolStrip.providesDeleteOp(false);

        layout.add(toolStrip.asWidget());


        TextItem nameItem = new TextItem("name", "Name");

        CheckBoxItem startedItem = new CheckBoxItem("autoStart", Console.CONSTANTS.common_label_autoStart());

        groupItem = new ComboBoxItem("group", "Server Group");

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

        final NumberBoxItem portOffset = new NumberBoxItem("portOffset", Console.CONSTANTS.common_label_portOffset());

        socketItem = new ComboBoxItem("socketBinding", Console.CONSTANTS.common_label_socketBinding())
        {
            @Override
            public boolean validate(String value) {
                boolean parentValid = super.validate(value);
                //boolean portDefined = !portOffset.isModified();
                return parentValid ;//&& portDefined;
            }

            @Override
            public String getErrMessage() {
                return Console.MESSAGES.common_validation_portOffsetUndefined(super.getErrMessage());
            }
        };


        form.setFields(nameItem, startedItem, groupItem, socketItem, portOffset);

        final FormHelpPanel helpPanel = new FormHelpPanel(
                new FormHelpPanel.AddressCallback() {
                    @Override
                    public ModelNode getAddress() {
                        ModelNode address = new ModelNode();
                        address.add("host", presenter.getSelectedHost());
                        address.add("server-config", "*");
                        return address;
                    }
                }, form
        );
        layout.add(helpPanel.asWidget());

        form.setEnabled(false);
        layout.add(form.asWidget());

        return layout;
    }

    public void setAvailableSockets(List<String> result) {
        socketItem.clearValue();
        socketItem.clearSelection();

        socketItem.setValueMap(result);
    }

    public void bind(DefaultCellTable table) {
        form.bind(table);
    }

    public void setAvailableGroups(List<ServerGroupRecord> result) {

        List<String> names = new ArrayList<String>(result.size());
        for(ServerGroupRecord rec : result)
            names.add(rec.getName());

        groupItem.setValueMap(names);
    }
}

@@ -44,16 +44,61 @@
 hosts;%0A%0A
+import com.google.gwt.core.client.Scheduler;%0A
 import c
@@ -186,16 +186,128 @@
 Widget;%0A
+import com.google.gwt.view.client.SelectionChangeEvent;%0Aimport com.google.gwt.view.client.SingleSelectionModel;%0A
 import o
@@ -1073,24 +1073,54 @@
 .ArrayList;%0A
+import java.util.Collections;%0A
 import java.
@@ -1386,16 +1386,85 @@
 oupItem;
+%0A    private List%3CServerGroupRecord%3E groups = Collections.EMPTY_LIST;
 %0A%0A    pu
@@ -3803,17 +3803,16 @@
     %7D;%0A%0A
-%0A
         
@@ -4713,16 +4713,22 @@
 id bind(
+final 
 DefaultC
@@ -4770,16 +4770,1133 @@
 (table);
+%0A%0A        table.getSelectionModel().addSelectionChangeHandler(new SelectionChangeEvent.Handler() %7B%0A            @Override%0A            public void onSelectionChange(SelectionChangeEvent event) %7B%0A                final Server server = ((SingleSelectionModel%3CServer%3E) table.getSelectionModel()).getSelectedObject();%0A                if(server!=null && %22%22.equals(server.getSocketBinding()))%0A                %7B%0A                    // preselect inherited socket binding value%0A                    for(final ServerGroupRecord group : groups)%0A                    %7B%0A                        if(group.getName().equals(server.getGroup()))%0A                        %7B%0A                            Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() %7B%0A                                @Override%0A                                public void execute() %7B%0A                                    socketItem.setValue(group.getSocketBinding());%0A                                %7D%0A                            %7D);%0A                            break;%0A                        %7D%0A                    %7D%0A                %7D%0A            %7D%0A        %7D);
 %0A    %7D%0A%0A
@@ -5961,24 +5961,55 @@
  result) %7B%0A%0A
+        this.groups = result;%0A%0A
         List

Fix BZ 996889