package org.jboss.as.console.client.shared.runtime.tx;

import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.LayoutPanel;
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.SuspendableViewImpl;
import org.jboss.as.console.client.shared.runtime.Metric;
import org.jboss.as.console.client.standalone.runtime.TXMetricPresenter;
import org.jboss.as.console.client.widgets.nav.ServerSwitch;
import org.jboss.ballroom.client.widgets.ContentGroupLabel;
import org.jboss.ballroom.client.widgets.tabs.FakeTabPanel;
import org.jboss.ballroom.client.widgets.tools.ToolButton;
import org.jboss.ballroom.client.widgets.tools.ToolStrip;

import java.util.List;

/**
 * @author Heiko Braun
 * @date 11/3/11
 */
public class TXMetricViewImpl extends SuspendableViewImpl implements TXMetricPresenter.MyView {

    private TXMetricManagement presenter;
    private TXExecutionView executionMetric;
    private TXRollbackView rollbackMetric;
    private ServerSwitch serverSwitch;

    @Override
    public void setPresenter(TXMetricManagement presenter) {
        this.presenter = presenter;
    }

    @Override
    public Widget createWidget() {

        LayoutPanel layout = new LayoutPanel();

        FakeTabPanel titleBar = new FakeTabPanel("Transactions");
        layout.add(titleBar);

        // ----

        final ToolStrip toolStrip = new ToolStrip();

        toolStrip.addToolButtonRight(new ToolButton(Console.CONSTANTS.common_label_refresh(), new ClickHandler(){
            @Override
            public void onClick(ClickEvent event) {
                presenter.refresh();
            }
        }));


        layout.add(toolStrip);

        // ---

        VerticalPanel panel = new VerticalPanel();
        panel.setStyleName("rhs-content-panel");

        ScrollPanel scrollPanel = new ScrollPanel(panel);
        layout.add(scrollPanel);

        layout.setWidgetTopHeight(titleBar, 0, Style.Unit.PX, 28, Style.Unit.PX);
        layout.setWidgetTopHeight(toolStrip, 28, Style.Unit.PX, 30, Style.Unit.PX);
        layout.setWidgetTopHeight(scrollPanel, 58, Style.Unit.PX, 100, Style.Unit.PCT);

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

        serverSwitch = new ServerSwitch();
        panel.add(serverSwitch.asWidget());

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

        HorizontalPanel outcomePanel = new HorizontalPanel();
        outcomePanel .setStyleName("fill-layout-width");

        this.executionMetric = new TXExecutionView();
        outcomePanel .add(executionMetric.asWidget());

        this.rollbackMetric = new TXRollbackView();
        outcomePanel .add(rollbackMetric.asWidget());

        panel.add(new ContentGroupLabel("Transaction Outcome"));
        panel.add(outcomePanel);

        return layout;
    }

    @Override
    public void setTxMetric(Metric txMetric) {
        this.executionMetric.addSample(txMetric);
    }

    @Override
    public void setRollbackMetric(Metric rollbackMetric) {
        this.rollbackMetric.addSample(rollbackMetric);
    }

    @Override
    public void setServerNames(List<String> serverNames) {
        serverSwitch.setServerNames(serverNames);
    }

    @Override
    public void recycleCharts() {
        executionMetric.recycle();
        rollbackMetric.recycle();
    }
}

@@ -797,24 +797,85 @@
 GroupLabel;%0A
+import org.jboss.ballroom.client.widgets.ContentHeaderLabel;%0A
 import org.j
@@ -918,24 +918,24 @@
 keTabPanel;%0A
-
 import org.j
@@ -1377,16 +1377,62 @@
 rSwitch;
+%0A    protected boolean supportServers = false;
 %0A%0A    @O
@@ -2579,32 +2579,73 @@
 --------------%0A%0A
+        if(supportServers)%0A        %7B%0A    
         serverSw
@@ -2667,24 +2667,28 @@
 erSwitch();%0A
+    
         pane
@@ -2711,32 +2711,146 @@
 ch.asWidget());%0A
+        %7D%0A        else%0A        %7B%0A            panel.add(new ContentHeaderLabel(%22Transaction Metrics%22));%0A        %7D%0A%0A
 %0A        // ----
@@ -3649,24 +3649,55 @@
 verNames) %7B%0A
+        if(supportServers)%0A    
         serv

allow re-use of view interface