package ca.patricklam.judodb.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class ConfigWidget extends Composite {
	interface MyUiBinder extends UiBinder<Widget, ConfigWidget> {}
	public static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);
	
	private final JudoDB jdb;
	private static final String PULL_CONFIG_URL = JudoDB.BASE_URL + "pull_config.php";
	//private static final String PUSH_CONFIG_URL = JudoDB.BASE_URL + "push_config.php";
	private static final String CALLBACK_URL_SUFFIX_Q = "?callback=";

	public ConfigWidget(JudoDB jdb) {
		this.jdb = jdb;
		initWidget(uiBinder.createAndBindUi(this));
		jdb.pleaseWait();
		getJson(jdb.jsonRequestId++, PULL_CONFIG_URL + CALLBACK_URL_SUFFIX_Q, this);
	}
	
	/**
	 * Make call to remote server.
	 */
	public native static void getJson(int requestId, String url,
	      ConfigWidget handler) /*-{
	   var callback = "callback" + requestId;

	   var script = document.createElement("script");
	   script.setAttribute("src", url+callback);
	   script.setAttribute("type", "text/javascript");
	   window[callback] = function(jsonObj) {
	     handler.@ca.patricklam.judodb.client.ListWidget::handleJsonResponse(Lcom/google/gwt/core/client/JavaScriptObject;)(jsonObj);
	     window[callback + "done"] = true;
	   }

	   setTimeout(function() {
	     if (!window[callback + "done"]) {
	       handler.@ca.patricklam.judodb.client.ListWidget::handleJsonResponse(Lcom/google/gwt/core/client/JavaScriptObject;)(null);
	     }

	     document.body.removeChild(script);
	     delete window[callback];
	     delete window[callback + "done"];
	   }, 5000);

	   document.body.appendChild(script);
	  }-*/;

	/**
	 * Handle the response to the request for data from a remote server.
	 */
	public void handleJsonResponse(JavaScriptObject jso) {
		if (jso == null) {
			jdb.displayError("Couldn't retrieve JSON (config)");
			return;
		}

	    //this.config = jso.cast();
	    jdb.clearStatus();
	}
}

@@ -708,16 +708,916 @@
 ack=%22;%0A%0A
+%09// club data:%0A%09// nom du club%0A%09// numero (Judo QC) du club%0A%09%0A%09// montants (should theoretically be stored with the session)%0A%09// passeport judo QC %5B5%5D%0A%09// frais non-anjou %5B5%5D%0A%09// penalty for prorata (percent of cost) %5B5%5D%0A%0A%09// misc:%0A%09// age veteran %5B35%5D%0A%09%0A%09// for each session:%0A%09// seqno%0A%09// abbrev %5BA12%5D%0A%09// effective_year %5B2013%5D%0A%09// debut_cours, fin_cours %5BnewDate(%221/Sep/2012%22), newDate(%2211/May/2013%22))%5D%0A%09// liste des prix:%0A%09//  division, 1 session, 2 sessions, judoQC%0A%0A%09// for each division:%0A%09// name %5BJunior%5D%0A%09// abbrev %5BU20N%5D%0A%09// years_ago %5B20%5D%0A%09// noire %5Btrue%5D%0A%09// aka %5BU20%5D%0A%0A%09// for each cours:%0A%09// %09%09new Cours(%220%22, %22Adultes (LM2015-2145, V2000-2145)%22, %22LM2015 V2000%22, %22%22),%0A%09// seqno %5B0%5D%0A%09// name %5BAdultes (LM2015-2145, V2000-2145)%5D%0A%09// abbrev %5BLM2015 V2000%5D%0A%09// entraineur%0A%09// which sessions does the cours apply to?%0A%09%0A%09// for each escompte:%0A%09// seqno%0A%09// name %5B2e membre%5D%0A%09// percent %5B10%5D%0A%09%0A
 %09public 

add fields we're going to permit editing