package org.sagebionetworks.web.client.widget.team;

import org.gwtbootstrap3.client.ui.Anchor;
import org.gwtbootstrap3.client.ui.Icon;
import org.gwtbootstrap3.client.ui.constants.IconType;
import org.sagebionetworks.repo.model.Team;
import org.sagebionetworks.web.client.DisplayConstants;
import org.sagebionetworks.web.client.DisplayUtils;
import org.sagebionetworks.web.client.GlobalApplicationState;
import org.sagebionetworks.web.client.IconsImageBundle;
import org.sagebionetworks.web.client.SageImageBundle;
import org.sagebionetworks.web.client.SynapseJSNIUtils;
import org.sagebionetworks.web.shared.WebConstants;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.inject.Inject;

public class TeamBadgeViewImpl extends FlowPanel implements TeamBadgeView {
	
	private Presenter presenter;
	SynapseJSNIUtils synapseJSNIUtils;
	GlobalApplicationState globalApplicationState;
	SageImageBundle sageImageBundle;
	IconsImageBundle iconsImageBundle;
	SimplePanel notificationsPanel;
	Long publicAclPrincipalId;
	
	@Inject
	public TeamBadgeViewImpl(SynapseJSNIUtils synapseJSNIUtils,
			GlobalApplicationState globalApplicationState,
			SageImageBundle sageImageBundle, IconsImageBundle iconsImageBundle) {
		this.synapseJSNIUtils = synapseJSNIUtils;
		this.globalApplicationState = globalApplicationState;
		this.sageImageBundle = sageImageBundle;
		this.iconsImageBundle = iconsImageBundle;
		notificationsPanel = new SimplePanel();
		notificationsPanel.addStyleName("displayInline");
		
		publicAclPrincipalId = Long.parseLong(globalApplicationState.getSynapseProperty(WebConstants.PUBLIC_ACL_PRINCIPAL_ID));
	}
	
	@Override
	public void setTeam(final Team team, Integer maxNameLength) {
		clear();
		notificationsPanel.clear();
		if(team == null)  throw new IllegalArgumentException("Team is required");
		
		if(team != null) {
			String name = maxNameLength == null ? team.getName() : DisplayUtils.stubStrPartialWord(team.getName(), maxNameLength); 
			
			final Anchor anchor = new Anchor();
			anchor.setText(name);
			anchor.addStyleName("font-size-15");
			anchor.setHref(DisplayUtils.getTeamHistoryToken(team.getId()));
			
			ClickHandler clickHandler = new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					anchor.fireEvent(event);
				}
			};
			if (team.getIcon() != null && team.getIcon().length() > 0) {
				Image profilePicture = new Image();
				profilePicture.setUrl(DisplayUtils.createTeamIconUrl(synapseJSNIUtils.getBaseFileHandleUrl(), team.getId()));
				profilePicture.setHeight("24px");
				profilePicture.addStyleName("imageButton userProfileImage displayInline margin-right-4");
				profilePicture.addClickHandler(clickHandler);
				add(profilePicture);
			} else {
				Icon defaultProfilePicture = new Icon(IconType.USERS);
				defaultProfilePicture.addStyleName("font-size-lg imageButton lightGreyText margin-right-4");
				add(defaultProfilePicture);
			}
			add(anchor);
			add(notificationsPanel);
		} 		
		
	}
	
	@Override
	public void setTeamWithoutLink(String name, String teamId) {
		clear();
		notificationsPanel.clear();
		
		InlineLabel nameLabel = new InlineLabel(name);
		nameLabel.addStyleName("font-size-13 boldText");
		
		HTML profilePicture;
		if (publicAclPrincipalId != null && Long.parseLong(teamId) == publicAclPrincipalId) {
			//profilePicture = new HTML(DisplayUtils.getFontelloIcon("globe font-size-13 userProfileImage lightGreyText margin-0-imp-before displayInline movedown-4"));
			String html = AbstractImagePrototype.create(iconsImageBundle.globe16()).getHTML();
			profilePicture = new HTML(html);
		} else {
			profilePicture = new HTML(DisplayUtils.getFontelloIcon("users font-size-13 imageButton userProfileImage lightGreyText margin-0-imp-before margin-right-5"));
		}
		profilePicture.addStyleName("displayInline margin-right-5");
		add(profilePicture);
			
		add(nameLabel);
		add(notificationsPanel);
	}
	
	@Override
	public void showLoadError(String principalId) {
		clear();
		add(new HTML(DisplayConstants.ERROR_LOADING));		
	}
	
	@Override
	public void showLoading() {
		clear();
		add(new HTML(DisplayUtils.getLoadingHtml(sageImageBundle)));
	}

	@Override
	public void showInfo(String title, String message) {
	}

	@Override
	public void showErrorMessage(String message) {
	}

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

	@Override
	public void setRequestCount(String count) {
		InlineHTML widget = new InlineHTML(DisplayUtils.getBadgeHtml(count));
		DisplayUtils.addTooltip(widget, DisplayConstants.PENDING_JOIN_REQUESTS_TOOLTIP);
		notificationsPanel.setWidget(widget);
	}

	/*
	 * Private Methods
	 */

}

@@ -727,69 +727,8 @@
 er;%0A
-import com.google.gwt.user.client.ui.AbstractImagePrototype;%0A
 impo
@@ -3319,19 +3319,16 @@
 %0A%09%09%7D %09%09%0A
-%09%09%0A
 %09%7D%0A%09%0A%09@O
@@ -3527,17 +3527,17 @@
 t-size-1
-3
+5
  boldTex
@@ -3547,15 +3547,12 @@
 ;%0A%09%09
-%0A%09%09HTML
+Icon
  pro
@@ -3659,10 +3659,8 @@
 %0A%09%09%09
-//
 prof
@@ -3680,340 +3680,122 @@
 new 
-HTML(DisplayUtils.getFontelloIcon(%22globe font-size-13 userProfileImage lightGreyText margin-0-imp-before displayInline movedown-4%22));%0A%09%09%09String html = AbstractImagePrototype.create(iconsImageBundle.globe16()).getHTML();%0A%09%09%09profilePicture = new HTML(html);%0A%09%09%7D else %7B%0A%09%09%09profilePicture = new HTML(DisplayUtils.getFontelloIcon(%22users 
+Icon(IconType.GLOBE);%0A%09%09%7D else %7B%0A%09%09%09profilePicture = new Icon(IconType.USERS);%0A%09%09%7D%0A%09%09profilePicture.addStyleName(%22
 font
@@ -3804,10 +3804,10 @@
 ize-
-13
+lg
  ima
@@ -3815,33 +3815,16 @@
 eButton 
-userProfileImage 
 lightGre
@@ -3840,96 +3840,23 @@
 gin-
-0-imp-before margin-right-5%22));%0A%09%09%7D%0A%09%09profilePicture.addStyleName(%22displayInline
+right-4
  margin-
 righ
@@ -3851,20 +3851,19 @@
  margin-
-righ
+lef
 t-5%22);%0A%09

fix public globe