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

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.user.client.ui.ScrollPanel;
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.core.NameTokens;
import org.jboss.as.console.client.domain.hosts.ServerPicker;
import org.jboss.as.console.client.plugins.RuntimeExtensionMetaData;
import org.jboss.as.console.client.plugins.RuntimeExtensionRegistry;
import org.jboss.as.console.client.plugins.RuntimeGroup;
import org.jboss.as.console.client.shared.model.SubsystemRecord;
import org.jboss.as.console.client.v3.stores.domain.HostStore;
import org.jboss.as.console.client.widgets.nav.Predicate;
import org.jboss.as.console.client.widgets.tree.GroupItem;
import org.jboss.ballroom.client.layout.LHSNavTree;
import org.jboss.ballroom.client.layout.LHSNavTreeItem;

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

/**
 * @author Heiko Braun
 * @date 11/2/11
 */
class DomainRuntimeNavigation {

    private VerticalPanel stack;
    private VerticalPanel layout;

    private ServerPicker serverPicker;

    private List<Predicate> metricPredicates = new ArrayList<Predicate>();
    private List<Predicate> runtimePredicates = new ArrayList<Predicate>();

    private ScrollPanel scroll;

    private LHSNavTree metrics;
    //private LHSTreeSection metricLeaf;
    //private LHSTreeSection runtimeLeaf;

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

        stack = new VerticalPanel();
        stack.setStyleName("fill-layout-width");


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

        serverPicker = new ServerPicker();
        stack.add(serverPicker.asWidget());

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

        metrics = new LHSNavTree("metrics");
        metrics.addStyleName("server-picker-stack");
        metrics.getElement().setAttribute("style", "padding: 5px;");
        metrics.getElement().setAttribute("aria-label", "Runtime Monitoring");

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

        //metricLeaf = new LHSTreeSection("Server Status");
        //metrics.addItem(metricLeaf);

        LHSNavTreeItem datasources = new LHSNavTreeItem("Datasources", NameTokens.DataSourceMetricPresenter);
        LHSNavTreeItem jmsQueues = new LHSNavTreeItem("JMS Destinations", NameTokens.JmsMetricPresenter);
        LHSNavTreeItem web = new LHSNavTreeItem("Web", NameTokens.WebMetricPresenter);
        LHSNavTreeItem jpa = new LHSNavTreeItem("JPA", NameTokens.JPAMetricPresenter);
        LHSNavTreeItem ws = new LHSNavTreeItem("Webservices", NameTokens.WebServiceRuntimePresenter);
        LHSNavTreeItem naming = new LHSNavTreeItem("JNDI View", NameTokens.JndiPresenter);

        metricPredicates.add(new Predicate("datasources", datasources));
        metricPredicates.add(new Predicate("messaging", jmsQueues));
        metricPredicates.add(new Predicate("web", web));
        metricPredicates.add(new Predicate("jpa", jpa));
        metricPredicates.add(new Predicate("webservices", ws));
        metricPredicates.add(new Predicate("naming", naming));


        // Extension based additions
        RuntimeExtensionRegistry registry = Console.getRuntimeLHSItemExtensionRegistry();
        List<RuntimeExtensionMetaData> menuExtensions = registry.getExtensions();
        for (RuntimeExtensionMetaData ext : menuExtensions) {

            if(RuntimeGroup.METRICS.equals(ext.getGroup()))
            {
                metricPredicates.add(
                        new Predicate(
                                ext.getKey(), new LHSNavTreeItem(ext.getName(), ext.getToken())
                        )
                );
            }
            else if(RuntimeGroup.OPERATiONS.equals(ext.getGroup()))
            {
                runtimePredicates.add(
                        new Predicate(
                                ext.getKey(), new LHSNavTreeItem(ext.getName(), ext.getToken())
                        )
                );
            }
            else
            {
                Log.warn("Invalid runtime group for extension: " + ext.getGroup());
            }
        }


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

        metrics.expandTopLevel();

        stack.add(metrics);

        layout.add(stack);

        scroll = new ScrollPanel(layout);

        return scroll;
    }

    public void setSubsystems(List<SubsystemRecord> subsystems) {

        metrics.removeItems();

        metrics.setVisible(false);

        if(subsystems.isEmpty()) return;

        metrics.setVisible(true);

        final GroupItem platformGroup = new GroupItem("Platform");

        platformGroup.addItem(new LHSNavTreeItem("JVM", NameTokens.HostVMMetricPresenter));
        platformGroup.addItem(new LHSNavTreeItem("Environment", NameTokens.EnvironmentPresenter));

        metrics.addItem(platformGroup);
        //platformGroup.setState(true);

        final GroupItem subsystemGroup = new GroupItem("Subsystems");
        // match subsystems
        for(SubsystemRecord subsys : subsystems)
        {

            for(Predicate predicate : metricPredicates)
            {
                if(predicate.matches(subsys.getKey()))
                    subsystemGroup.addItem(predicate.getNavItem());
            }
        }

        metrics.addItem(subsystemGroup);
        subsystemGroup.setState(true);
        platformGroup.setState(true);


    }

    public void setTopology(String selectedHost, String selectedServer, HostStore.Topology topology) {
        serverPicker.setTopology(selectedHost, selectedServer, topology);
    }
}

@@ -89,16 +89,61 @@
 nt.Log;%0A
+import com.google.gwt.core.client.Scheduler;%0A
 import c
@@ -879,16 +879,75 @@
 upItem;%0A
+import org.jboss.ballroom.client.layout.LHSHighlightEvent;%0A
 import o
@@ -990,16 +990,16 @@
 avTree;%0A
-
 import o
@@ -1177,16 +1177,69 @@
 igation 
+implements LHSHighlightEvent.NavItemSelectionHandler 
 %7B%0A%0A    p
@@ -1558,26 +1558,24 @@
 etrics;%0A    
-//
 private LHST
@@ -1577,72 +1577,37 @@
  LHS
-TreeSection metricLeaf;%0A    //private LHSTreeSection runtimeLeaf
+HighlightEvent highlightEvent
 ;%0A%0A 
@@ -5685,24 +5685,321 @@
 ate(true);%0A%0A
+        if(highlightEvent!=null)%0A        %7B%0A            Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() %7B%0A                @Override%0A                public void execute() %7B%0A                    metrics.onSelectedNavTree(highlightEvent);%0A                %7D%0A            %7D);%0A        %7D%0A%0A
 %0A    %7D%0A%0A    
@@ -6176,11 +6176,130 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public void onSelectedNavTree(LHSHighlightEvent event) %7B%0A        this.highlightEvent = event;%0A    %7D
 %0A%7D%0A

Fix highlight in domain runtime navigation