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

import com.google.gwt.event.shared.GwtEvent;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ContentSlot;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.proxy.Place;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.Proxy;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;
import com.gwtplatform.mvp.client.proxy.RevealContentHandler;
import org.jboss.as.console.client.core.BootstrapContext;
import org.jboss.as.console.client.core.Header;
import org.jboss.as.console.client.core.MainLayoutPresenter;
import org.jboss.as.console.client.core.NameTokens;
import org.jboss.as.console.client.domain.model.SimpleCallback;
import org.jboss.as.console.client.shared.model.SubsystemRecord;
import org.jboss.as.console.client.shared.model.SubsystemStore;

import java.util.List;

/**
 * @author Heiko Braun
 * @date 11/2/11
 */
public class StandaloneRuntimePresenter extends Presenter<StandaloneRuntimePresenter.MyView, StandaloneRuntimePresenter.MyProxy> {

    private final PlaceManager placeManager;
    private boolean hasBeenRevealed = false;

    @ContentSlot
    public static final GwtEvent.Type<RevealContentHandler<?>> TYPE_MainContent =
            new GwtEvent.Type<RevealContentHandler<?>>();
    private SubsystemStore subsysStore;
    private BootstrapContext bootstrap;

    private PlaceRequest lastSubPlace;
    private Header header;

    @ProxyCodeSplit
    @NameToken(NameTokens.StandaloneRuntimePresenter)
    public interface MyProxy extends Proxy<StandaloneRuntimePresenter>, Place {
    }

    public interface MyView extends View {
        void setPresenter(StandaloneRuntimePresenter presenter);
        void setSubsystems(List<SubsystemRecord> result);
    }

    @Inject
    public StandaloneRuntimePresenter(
            EventBus eventBus, MyView view, MyProxy proxy,
            PlaceManager placeManager, SubsystemStore subsysStore,
            BootstrapContext bootstrap, Header header) {
        super(eventBus, view, proxy);

        this.placeManager = placeManager;
        this.bootstrap = bootstrap;
        this.subsysStore = subsysStore;
        this.header = header;
    }

    @Override
    protected void onBind() {
        super.onBind();
        getView().setPresenter(this);
    }


    @Override
    protected void onReset() {

        super.onReset();


        header.highlight(NameTokens.StandaloneRuntimePresenter);

        String currentToken = placeManager.getCurrentPlaceRequest().getNameToken();
        if(!currentToken.equals(getProxy().getNameToken()))
        {
            lastSubPlace = placeManager.getCurrentPlaceRequest();
        }
        else if(lastSubPlace!=null)
        {
            placeManager.revealPlace(lastSubPlace);
        }

        // first request, select default contents
        if(!hasBeenRevealed && NameTokens.StandaloneRuntimePresenter.equals(currentToken))
        {

            if (lastSubPlace != null)
            {
                placeManager.revealPlace(lastSubPlace);
            }
            else
            {
                placeManager.revealPlace(new PlaceRequest(NameTokens.StandaloneServerPresenter));
            }

            hasBeenRevealed = true;
        }
        else if (!NameTokens.StandaloneRuntimePresenter.equals(currentToken))
        {
            subsysStore.loadSubsystems("default", new SimpleCallback<List<SubsystemRecord>>() {
                @Override
                public void onSuccess(List<SubsystemRecord> result) {
                    getView().setSubsystems(result);
                }
            });

        }
    }

    @Override
    protected void revealInParent() {
        RevealContentEvent.fire(this, MainLayoutPresenter.TYPE_MainContent, this);
    }
}

@@ -3216,71 +3216,266 @@
 aled
- && NameTokens.StandaloneRuntimePresenter.equals(currentToken))
+)%0A        %7B%0A%0A            subsysStore.loadSubsystems(%22default%22, new SimpleCallback%3CList%3CSubsystemRecord%3E%3E() %7B%0A                @Override%0A                public void onSuccess(List%3CSubsystemRecord%3E result) %7B%0A                    getView().setSubsystems(result);%0A
 %0A   
@@ -3471,35 +3471,32 @@
 sult);%0A%0A        
-%7B%0A%0A
             if (
@@ -3521,34 +3521,48 @@
 ll)%0A            
-%7B%0A
+        %7B%0A      
                 
@@ -3553,32 +3553,34 @@
                 
+  
 placeManager.rev
@@ -3607,34 +3607,44 @@
 e);%0A            
-%7D%0A
+        %7D%0A  
             else
@@ -3635,24 +3635,30 @@
             
+      
 else%0A       
@@ -3654,34 +3654,50 @@
 lse%0A            
+   
-%7B%0A
+     %7B%0A        
                 
@@ -3790,27 +3790,24 @@
         
-%7D%0A%0A
         
     hasB
@@ -3802,44 +3802,24 @@
     
+%7D%0A
     
-hasBeenRevealed = true;%0A
+  
         
 %7D%0A  
@@ -3806,32 +3806,34 @@
 %7D%0A              
+  
 %7D%0A        else i
@@ -3830,377 +3830,64 @@
     
-else if (!NameTokens.StandaloneRuntimePresenter.equals(currentToken))%0A        %7B%0A            subsysStore.loadSubsystems(%22default%22, new SimpleCallback%3CList%3CSubsystemRecord%3E%3E() %7B%0A                @Override%0A                public void onSuccess(List%3CSubsystemRecord%3E result) %7B%0A                    getView().setSubsystems(result);%0A                %7D%0A            %7D);%0A%0A        %7D
+    %7D);%0A%0A%0A            hasBeenRevealed = true;%0A        %7D%0A
 %0A   

fix highlight lhs nav in standalone runtime view