package scoutmgr.client.application.admin.badges;

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.HasUiHandlers;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.annotations.ContentSlot;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.proxy.NavigationEvent;
import com.gwtplatform.mvp.client.proxy.NavigationHandler;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.gwtplatform.mvp.client.proxy.RevealContentHandler;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nonnull;
import org.realityforge.replicant.client.EntityRepository;
import scoutmgr.client.application.ApplicationPresenter;
import scoutmgr.client.entity.BadgeCategory;
import scoutmgr.client.entity.comparator.BadgeCategoryComparator;
import scoutmgr.client.event.MetadataLoadedEvent;
import scoutmgr.client.place.NameTokens;
import scoutmgr.client.view.model.BadgeCategoryViewModel;
import scoutmgr.client.view.model.comparator.ViewModelComparator;

public class BadgesPresenter
  extends Presenter<BadgesPresenter.View, BadgesPresenter.Proxy>
  implements NavigationHandler, BadgesUiHandlers
{
  private final ViewModelComparator<BadgeCategoryViewModel> _badgeCategoryComparator;
  private List<BadgeCategoryViewModel> _badgeCategoryViewModels;

  @ProxyStandard
  @NameToken( { NameTokens.ADMIN_BADGES, NameTokens.ADMIN_BADGES_LEVEL } )
  interface Proxy
    extends ProxyPlace<BadgesPresenter>
  {
  }

  interface View
    extends com.gwtplatform.mvp.client.View, HasUiHandlers<BadgesUiHandlers>
  {
    void setBadgeCategoryViewModels( List<BadgeCategoryViewModel> badgeCategoryViewModels );
  }

  @ContentSlot
  public static final GwtEvent.Type<RevealContentHandler<?>> SLOT_LEFT_NAV = new GwtEvent.Type<>();
  @ContentSlot
  public static final GwtEvent.Type<RevealContentHandler<?>> SLOT_BADGES_CONTENT = new GwtEvent.Type<>();

  @Inject
  private PlaceManager _placeManager;

  @javax.inject.Inject
  private EntityRepository _repository;

  @Inject
  private NavPresenter _navPresenter;

  @Inject
  private BadgeLevelPresenter _badgeLevelPresenter;

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

    getView().setUiHandlers( this );

    _badgeCategoryViewModels = new ArrayList<>(  );

    _badgeCategoryComparator = new ViewModelComparator<>( new BadgeCategoryComparator() );

    eventBus.addHandler( MetadataLoadedEvent.TYPE, new MetadataLoadedEvent.Handler()
    {
      @Override
      public void onMetadataLoaded( @Nonnull final MetadataLoadedEvent event )
      {
        setBadgesToUI();
      }
    } );
  }

  protected void onBind()
  {
    super.onBind();
    setInSlot( SLOT_LEFT_NAV, _navPresenter );
    addRegisteredHandler( NavigationEvent.getType(), this );
  }

  @Override
  public void onNavigation( final NavigationEvent navigationEvent )
  {
    if ( NameTokens.ADMIN_BADGES_LEVEL.equals( navigationEvent.getRequest().getNameToken()))
    {
      setInSlot( SLOT_BADGES_CONTENT, _badgeLevelPresenter );
    }
  }

  @Override
  protected void onReveal()
  {
    super.onReveal();
    setBadgesToUI();
  }

  private void setBadgesToUI()
  {
    final ArrayList<BadgeCategory> categories = _repository.findAll( BadgeCategory.class );
    _badgeCategoryViewModels.clear();
    for ( final BadgeCategory category : categories )
    {
      _badgeCategoryViewModels.add( buildBadgeCategoryViewModel( category ) );
    }
    Collections.sort( _badgeCategoryViewModels, _badgeCategoryComparator );
    getView().setBadgeCategoryViewModels( _badgeCategoryViewModels );
  }

  private BadgeCategoryViewModel buildBadgeCategoryViewModel( final BadgeCategory category )
  {
    final BadgeCategoryViewModel viewModel =
      new BadgeCategoryViewModel( category.getName(), category );
    populateBadgeCategoryViewModel( category, viewModel );
    return viewModel;
  }

  private void populateBadgeCategoryViewModel( final BadgeCategory category, final BadgeCategoryViewModel viewModel )
  {
    viewModel.setName( category.getName() );
    viewModel.setLevel( category.getScoutLevel() );
    viewModel.setRank( category.getRank() );
  }
}

@@ -807,24 +807,57 @@
 .util.List;%0A
+import java.util.logging.Logger;%0A
 import javax
@@ -3491,100 +3491,8 @@
  %7D%0A%0A
-  @Override%0A  protected void onReveal()%0A  %7B%0A    super.onReveal();%0A    setBadgesToUI();%0A  %7D%0A%0A
   pr

Send badges to UI on metadata loaded rather than onReveal