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

import org.sagebionetworks.web.client.GlobalApplicationState;
import org.sagebionetworks.web.client.SynapseJSNIUtils;
import org.sagebionetworks.web.client.place.Profile;
import org.sagebionetworks.web.client.place.Synapse.ProfileArea;
import org.sagebionetworks.web.client.security.AuthenticationController;
import org.sagebionetworks.web.client.view.users.RegisterAccountViewImpl;
import org.sagebionetworks.web.client.widget.ReactComponentDiv;

import com.google.gwt.dom.client.Element;
import com.google.gwt.place.shared.Place;
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 LoginWidgetViewImpl implements LoginWidgetView, IsWidget {
	public interface LoginWidgetViewImplUiBinder extends UiBinder<Widget, LoginWidgetViewImpl> {
	}

	@UiField
	ReactComponentDiv srcLoginContainer;
	Widget widget;
	SynapseJSNIUtils jsniUtils;
	GlobalApplicationState globalAppState;
	AuthenticationController authController;	

	@Inject
	public LoginWidgetViewImpl(LoginWidgetViewImplUiBinder binder, SynapseJSNIUtils jsniUtils,
			GlobalApplicationState globalAppState, AuthenticationController authController) {
		widget = binder.createAndBindUi(this);
		this.jsniUtils = jsniUtils;
		this.globalAppState = globalAppState;
		this.authController = authController;		
	}

	public void postLogin() {
		Place defaultPlace = new Profile(Profile.VIEW_PROFILE_TOKEN, ProfileArea.PROJECTS);
		globalAppState.gotoLastPlace(defaultPlace);
		authController.checkForUserChange();
	}

	private static native void _createSRCLogin(Element el, LoginWidgetViewImpl loginWidgetView,
			String googleSSORedirectUrl) /*-{
		try {
			if ($wnd.SRC && $wnd.SRC.SynapseComponents) {
				function sessionCallback() {
					loginWidgetView.@org.sagebionetworks.web.client.widget.login.LoginWidgetViewImpl::postLogin()();
				}
	
				var props = {
					googleRedirectUrl : googleSSORedirectUrl,
					sessionCallback : sessionCallback
				};			
				$wnd.ReactDOM.render($wnd.React.createElement(
						$wnd.SRC.SynapseComponents.LoginPage, props, null), el);
			}
		} catch (err) {
			console.error(err);
		}
	}-*/;

	@Override
	public Widget asWidget() {
		_createSRCLogin(srcLoginContainer.getElement(), this,
				RegisterAccountViewImpl.GOOGLE_OAUTH_CALLBACK_URL);
		return widget;
	}

	@Override
	public void clear() {
	}

	@Override
	public void setVisible(boolean visible) {
		widget.setVisible(visible);
	}
}

@@ -1144,17 +1144,16 @@
 troller;
-%09
 %0A%0A%09@Inje
@@ -1485,16 +1485,209 @@
 ller;%09%09%0A
+%09%09widget.addAttachHandler(event -%3E %7B%0A%09%09%09if (event.isAttached()) %7B%0A%09%09%09%09_createSRCLogin(srcLoginContainer.getElement(), this,%0A%09%09%09%09%09%09RegisterAccountViewImpl.GOOGLE_OAUTH_CALLBACK_URL);%0A%09%09%09%7D%0A%09%09%7D);%0A
 %09%7D%0A%0A%09pub
@@ -2026,58 +2026,8 @@
 y %7B%0A
-%09%09%09if ($wnd.SRC && $wnd.SRC.SynapseComponents) %7B%0A%09
 %09%09%09f
@@ -2054,17 +2054,16 @@
 ack() %7B%0A
-%09
 %09%09%09%09logi
@@ -2162,14 +2162,11 @@
 %0A%09%09%09
-%09
 %7D%0A
-%09%0A%09
+%0A
 %09%09%09v
@@ -2182,17 +2182,16 @@
 = %7B%0A%09%09%09%09
-%09
 googleRe
@@ -2228,17 +2228,16 @@
 rl,%0A%09%09%09%09
-%09
 sessionC
@@ -2266,19 +2266,14 @@
 ack%0A
-%09
 %09%09%09%7D;
-%09%09%09%0A%09
+%0A
 %09%09%09$
@@ -2318,17 +2318,16 @@
 lement(%0A
-%09
 %09%09%09%09%09$wn
@@ -2380,21 +2380,16 @@
 ), el);%0A
-%09%09%09%7D%0A
 %09%09%7D catc
@@ -2476,120 +2476,8 @@
 ) %7B%0A
-%09%09_createSRCLogin(srcLoginContainer.getElement(), this,%0A%09%09%09%09RegisterAccountViewImpl.GOOGLE_OAUTH_CALLBACK_URL);%0A
 %09%09re

revert changes to try to fix local Login page SRC issue