package scoutmgr.client.application.scout;

import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.mvp.client.HasUiHandlers;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.gwtplatform.mvp.shared.proxy.PlaceRequest;
import org.realityforge.replicant.client.EntityRepository;
import scoutmgr.client.application.ApplicationPresenter;
import scoutmgr.client.entity.Person;
import scoutmgr.client.net.ScoutmgrDataLoaderService;
import scoutmgr.client.place.NameTokens;
import scoutmgr.client.service.PersonnelService;
import scoutmgr.client.view.model.ScoutViewModel;

public class ScoutPresenter
  extends Presenter<ScoutPresenter.View, ScoutPresenter.Proxy>
  implements ScoutUiHandlers
{
  private Integer _scoutID;

  @Inject
  private PlaceManager _placeManager;


  @Inject
  private ScoutmgrDataLoaderService _dataloader;

  @Inject
  private EntityRepository _entityRepository;

  @Inject
  private PersonnelService _personnelService;

  @ProxyStandard
  @NameToken( { NameTokens.SCOUT } )
  interface Proxy
    extends ProxyPlace<ScoutPresenter>
  {
  }

  interface View
    extends com.gwtplatform.mvp.client.View, HasUiHandlers<ScoutUiHandlers>
  {
    void setScout( ScoutViewModel viewModel );

    void showLoadingMessage();
  }

  @Inject
  ScoutPresenter(  final EventBus eventBus,
                   final View view,
                   final Proxy proxy )
  {
    super( eventBus, view, proxy, ApplicationPresenter.SLOT_MAIN_CONTENT );

    getView().setUiHandlers( this );
  }

  @Override
  public void prepareFromRequest( final PlaceRequest request )
  {
    super.prepareFromRequest( request );
    final String idStr = request.getParameter( "id", null );
    if ( null != idStr )
    {
      _scoutID = Integer.valueOf( idStr );
      configureForScout( _scoutID );
    }
    else
    {
      _placeManager.revealErrorPlace( "Invalid URL" );
    }
  }

  private void configureForScout( final Integer id )
  {
    final Person person = _entityRepository.findByID( Person.class, id );
    if ( null != person )
    {
      final ScoutViewModel viewModel = new ScoutViewModel( person );
      getView().setScout( viewModel );
    }
    else
    {
      getView().showLoadingMessage();
    }
  }
}

@@ -495,16 +495,303 @@
 equest;%0A
+import javax.annotation.Nonnull;%0Aimport org.realityforge.replicant.client.EntityChangeBroker;%0Aimport org.realityforge.replicant.client.EntityChangeEvent;%0Aimport org.realityforge.replicant.client.EntityChangeListener;%0Aimport org.realityforge.replicant.client.EntityChangeListenerAdapter;%0A
 import o
@@ -1331,17 +1331,16 @@
 nager;%0A%0A
-%0A
   @Injec
@@ -1932,16 +1932,79 @@
 xy proxy
+,%0A                   final ScoutmgrDataLoaderService dataLoader
  )%0A  %7B%0A 
@@ -2075,24 +2075,55 @@
 CONTENT );%0A%0A
+    _dataloader = dataLoader;%0A%0A
     getView(
@@ -2152,16 +2152,217 @@
 );%0A  %7D%0A%0A
+  @Override%0A  protected void onReveal()%0A  %7B%0A    getView().showLoadingMessage();%0A    super.onReveal();%0A  %7D%0A%0A  @Override%0A  protected void onHide()%0A  %7B%0A    super.onHide();%0A   unsubscribeFromScout();%0A  %7D%0A%0A
   @Overr
@@ -2561,24 +2561,54 @@
 Str )%0A    %7B%0A
+      unsubscribeFromScout();%0A
       _scout
@@ -2634,24 +2634,203 @@
 f( idStr );%0A
+      getView().showLoadingMessage();%0A      _dataloader.getSession().subscribeToPerson( _scoutID, new Runnable()%0A      %7B%0A        @Override%0A        public void run()%0A        %7B%0A    
       config
@@ -2850,24 +2850,45 @@
 _scoutID );%0A
+        %7D%0A      %7D );%0A
     %7D%0A    el
@@ -2962,16 +2962,198 @@
  %7D%0A  %7D%0A%0A
+  private void unsubscribeFromScout()%0A  %7B%0A    if ( null != _scoutID )%0A    %7B%0A      _dataloader.getSession().unsubscribeFromPerson( _scoutID, null );%0A      _scoutID = null;%0A    %7D%0A  %7D%0A%0A
   privat

Handle subscription/unsub of scout during navigation in and out of scout page.