package org.sagebionetworks.web.client.widget.entity.file;

import org.gwtbootstrap3.client.ui.Anchor;
import org.gwtbootstrap3.client.ui.constants.ButtonSize;
import org.gwtbootstrap3.client.ui.html.Span;
import org.sagebionetworks.web.client.DisplayUtils;
import org.sagebionetworks.web.client.PortalGinInjector;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.ButtonElement;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

public class FileDownloadButtonViewImpl implements FileDownloadButtonView {

	private Presenter presenter;
	
	@UiField
	Anchor downloadLink;
	@UiField
	Anchor downloadLink2;
	@UiField
	Span otherWidgets;
	@UiField
	ButtonElement downloadButton;

	PortalGinInjector ginInjector;
	
	boolean isExtraSmall;
	interface FileDownloadButtonViewImplUiBinder extends UiBinder<Widget, FileDownloadButtonViewImpl> {}

	private static FileDownloadButtonViewImplUiBinder uiBinder = GWT.create(FileDownloadButtonViewImplUiBinder.class);
	Widget widget;
	ClickHandler licensedDownloadClickHandler, authorizedDirectDownloadClickHandler;
	
	@Inject
	public FileDownloadButtonViewImpl(PortalGinInjector ginInjector) {
		widget = uiBinder.createAndBindUi(this);
		this.ginInjector = ginInjector;
		licensedDownloadClickHandler = event -> {
			//if there is an href, ignore it
			event.preventDefault();
			presenter.onUnauthenticatedS3DirectDownloadClicked();
		};
		
		authorizedDirectDownloadClickHandler = event -> {
			presenter.onAuthorizedDirectDownloadClicked();
		};
	
		isExtraSmall = false;
	}
	
	@Override
	public void clear() {
		downloadLink.setVisible(false);
		downloadLink2.setVisible(false);
		otherWidgets.clear();
	}
	
	@Override
	public void setIsAuthorizedDirectDownloadLink() {
		downloadLink.addClickHandler(authorizedDirectDownloadClickHandler);
		downloadLink2.addClickHandler(authorizedDirectDownloadClickHandler);
		downloadLink.setVisible(!isExtraSmall);
		downloadLink2.setVisible(isExtraSmall);
	}
	@Override
	public void setIsUnauthenticatedS3DirectDownload() {
		downloadLink.addClickHandler(licensedDownloadClickHandler);
		downloadLink2.addClickHandler(licensedDownloadClickHandler);
		updateDownloadLinkVisibility();
	}
	
	@Override
	public void setIsDirectDownloadLink(String href) {
		downloadLink.setHref(href);
		downloadLink2.setHref(href);
		updateDownloadLinkVisibility();
	}
	
	private void updateDownloadLinkVisibility() {
		downloadLink.setVisible(!isExtraSmall);
		downloadLink2.setVisible(isExtraSmall);
	}
	
	@Override
	public Widget asWidget() {
		return widget;
	}	

	@Override 
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}
	
	@Override
	public void addWidget(IsWidget w) {
		otherWidgets.add(w);
	}
	
	private void removeButtonSizeStyles(ButtonElement el) {
		for (ButtonSize size : ButtonSize.values()) {
			String cssName = size.getCssName();
			if (DisplayUtils.isDefined(cssName))
				el.removeClassName(cssName);
		}
	}
	
	@Override
	public void setButtonSize(ButtonSize size) {
		isExtraSmall = size.equals(ButtonSize.EXTRA_SMALL);
		removeButtonSizeStyles(downloadButton);
		downloadButton.addClassName(size.getCssName());
	}
	@Override
	public void showLoginS3DirectDownloadDialog(String endpoint) {
		S3DirectLoginDialog dialog = ginInjector.getS3DirectLoginDialog();
		dialog.setPresenter(presenter);
		dialog.showLoginS3DirectDownloadDialog(endpoint);
	}
	@Override
	public void showS3DirectDownloadDialog() {
		S3DirectLoginDialog dialog = ginInjector.getS3DirectLoginDialog();
		dialog.setPresenter(presenter);
		dialog.showS3DirectDownloadDialog();
	}
}

@@ -445,24 +445,80 @@
 ickHandler;%0A
+import com.google.gwt.event.shared.HandlerRegistration;%0A
 import com.g
@@ -1342,24 +1342,112 @@
 ickHandler;%0A
+%09HandlerRegistration downloadLinkHandlerRegistration, downloadLink2HandlerRegistration;%0A
 %09%0A%09@Inject%0A%09
@@ -2024,32 +2024,347 @@
 s.clear();%0A%09%7D%0A%09%0A
+%09private void clearClickHandlers() %7B%0A%09%09if (downloadLinkHandlerRegistration != null) %7B%0A%09%09%09downloadLinkHandlerRegistration.removeHandler();%0A%09%09%7D%0A%09%09if (downloadLink2HandlerRegistration != null) %7B%0A%09%09%09downloadLink2HandlerRegistration.removeHandler();%0A%09%09%7D%0A%09%09downloadLink.setHref(null);%0A%09%09downloadLink2.setHref(null);%0A%09%7D%0A%09%0A
 %09@Override%0A%09publ
@@ -2407,24 +2407,82 @@
 dLink() %7B%0A%09%09
+clearClickHandlers();%0A%09%09downloadLinkHandlerRegistration = 
 downloadLink
@@ -2531,32 +2531,67 @@
 lickHandler);%0A%09%09
+downloadLink2HandlerRegistration = 
 downloadLink2.ad
@@ -2793,24 +2793,82 @@
 nload() %7B%0A%09%09
+clearClickHandlers();%0A%09%09downloadLinkHandlerRegistration = 
 downloadLink
@@ -2909,32 +2909,67 @@
 lickHandler);%0A%09%09
+downloadLink2HandlerRegistration = 
 downloadLink2.ad
@@ -3111,24 +3111,48 @@
 ing href) %7B%0A
+%09%09clearClickHandlers();%0A
 %09%09downloadLi

clear any previous click handler before setting