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

import static org.sagebionetworks.web.client.DisplayUtils.newWindow;

import org.gwtbootstrap3.client.ui.Button;
import org.gwtbootstrap3.client.ui.constants.ButtonSize;
import org.gwtbootstrap3.client.ui.constants.ButtonType;
import org.gwtbootstrap3.client.ui.html.Div;
import org.sagebionetworks.web.client.DisplayUtils;
import org.sagebionetworks.web.shared.WikiPageKey;

import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

public class ButtonLinkWidgetViewImpl extends Div implements ButtonLinkWidgetView {
	private Button button;
	public static final ClickHandler BUTTON_LINK_CLICK_HANDLER = event -> {
		event.preventDefault();
		Widget panel = (Widget)event.getSource();
		String href = panel.getElement().getAttribute("href");
		boolean openInNewWindow = panel.getElement().hasAttribute(ButtonLinkWidget.LINK_OPENS_NEW_WINDOW);
		if (openInNewWindow) {
			newWindow(href, "_blank", "");
		} else {
			Window.Location.assign(href);
		}
	};
	@Inject
	public ButtonLinkWidgetViewImpl() {
		button = new Button();
		button.addClickHandler(BUTTON_LINK_CLICK_HANDLER);
	}
	
	@Override
	public void configure(WikiPageKey wikiKey, String buttonText, final String url, boolean isHighlight, final boolean openInNewWindow) {
		clear();
		button.setText(buttonText);
		if (isHighlight)
			button.setType(ButtonType.INFO);
		button.setHref(url);
		if (openInNewWindow) {
			button.getElement().setAttribute(ButtonLinkWidget.LINK_OPENS_NEW_WINDOW, "true");	
		}
		add(button);
	}
	
	public void showError(String error) {
		clear();
		add(new HTMLPanel(DisplayUtils.getMarkdownWidgetWarningHtml(error)));
	}
	
	@Override
	public Widget asWidget() {
		return this;
	}	

	@Override
	public void setWidth(String width) {
		button.setWidth(width);
	}
	
	@Override
	public void setSize(ButtonSize size) {
		button.setSize(size);					
	}
}

@@ -416,28 +416,296 @@
 web.
-shared.WikiPageKey;%0A
+client.GWTWrapper;%0Aimport org.sagebionetworks.web.client.GlobalApplicationState;%0Aimport org.sagebionetworks.web.client.PlaceChanger;%0Aimport org.sagebionetworks.web.client.mvp.AppPlaceHistoryMapper;%0Aimport org.sagebionetworks.web.shared.WikiPageKey;%0A%0Aimport com.google.gwt.core.client.GWT;
 %0Aimp
@@ -1031,16 +1031,155 @@
 button;%0A
+%09private static PlaceChanger placeChanger;%0A%09private static AppPlaceHistoryMapper appPlaceHistoryMapper;%0A%09private static String hostPrefix;%0A
 %09public 
@@ -1541,16 +1541,202 @@
  else %7B%0A
+%09%09%09if (href.startsWith(%22#!%22) %7C%7C href.startsWith(hostPrefix)) %7B%0A%09%09%09%09GWT.debugger();%0A%09%09%09%09placeChanger.goTo(appPlaceHistoryMapper.getPlace(href.substring(href.indexOf('!'))));%0A%09%09%09%7D else %7B%0A%09
 %09%09%09Windo
@@ -1759,16 +1759,22 @@
 n(href);
+%09%0A%09%09%09%7D
 %0A%09%09%7D%0A%09%7D;
@@ -1816,19 +1816,265 @@
 iewImpl(
-) %7B
+GlobalApplicationState globalAppState, GWTWrapper gwt) %7B%0A%09%09if (placeChanger == null) %7B%0A%09%09%09placeChanger = globalAppState.getPlaceChanger();%0A%09%09%09appPlaceHistoryMapper = globalAppState.getAppPlaceHistoryMapper();%0A%09%09%09hostPrefix = gwt.getHostPrefix();%0A%09%09%7D
 %0A%09%09butto

use placechanger instead of assigning window location