package com.inepex.ineForm.client.widget;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.inject.Inject;
import com.inepex.ineFrame.client.async.IneDispatch;
import com.inepex.ineFrame.client.async.IneDispatchBase.SuccessCallback;
import com.inepex.ineFrame.client.misc.HandlerAwareComposite;
import com.inepex.inei18n.client.I18nStore_Client;
import com.inepex.inei18n.shared.ChangeLanguageAction;
import com.inepex.inei18n.shared.SimpleResult;

/**
 * 
 * An Anchor based widget to select between Hungarian and English language
 * 
 */
public class LanguageChangeWidget extends HandlerAwareComposite {

	final Anchor currentLangLabel = new Anchor();
	
	IneDispatch ineDispatch;
	
	@Inject
	public LanguageChangeWidget(IneDispatch ineDispatch) {
		this.ineDispatch = ineDispatch;
		setLabelPropsForCurrentLang();
		initWidget(currentLangLabel);
	}
	
	public void setLabelPropsForCurrentLang() {
		
		/**
		 * We use un-internationalized strings purposely!!!
		 */
		if ("en".equals(I18nStore_Client.currentLanguage))
			currentLangLabel.setText("Magyar");
		else
			currentLangLabel.setText("English");
	}
	
	@Override
	protected void onAttach() {
		currentLangLabel.addClickHandler(new LangChangeClickHandler());
		super.onAttach();
	}
	
	private class LangChangeClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			String requestedLang = "en".equals(I18nStore_Client.currentLanguage) ? "hu" : "en";
			ChangeLanguageAction action = new ChangeLanguageAction(requestedLang);
			ineDispatch.execute(action, new LangChgCallback());
		}
	}
	
	private class LangChgCallback extends SuccessCallback<SimpleResult> {
		
		/**
		 *  you should reload the window to affect the language changes and let widgets, assists, ... to use the new lang 
		 */
		@Override
		public void onSuccess(SimpleResult result) {
			Window.Location.reload();
		}
	}
	
	
}

@@ -835,16 +835,66 @@
 spatch;%0A
+%09private final I18nStore_Client i18nStore_Client;%0A
 %09%0A%09@Inje
@@ -948,16 +948,51 @@
 Dispatch
+, I18nStore_Client i18nStore_Client
 ) %7B%0A%09%09th
@@ -1021,16 +1021,60 @@
 spatch;%0A
+%09%09this.i18nStore_Client = i18nStore_Client;%0A
 %09%09setLab
@@ -1259,33 +1259,33 @@
 if (%22en%22.equals(
-I
+i
 18nStore_Client.
@@ -1276,33 +1276,36 @@
 18nStore_Client.
-c
+getC
 urrentLanguage))
@@ -1302,16 +1302,18 @@
 Language
+()
 ))%0A%09%09%09cu
@@ -1685,17 +1685,17 @@
 .equals(
-I
+i
 18nStore
@@ -1702,17 +1702,20 @@
 _Client.
-c
+getC
 urrentLa
@@ -1720,16 +1720,18 @@
 Language
+()
 ) ? %22hu%22

remove static variable from singleton class