package com.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Timer;
import com.smartgwt.client.types.Encoding;
import com.smartgwt.client.types.FormMethod;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.HTMLFlow;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.UploadItem;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class FileUploadApplication implements EntryPoint {
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		VLayout mainlayout = new VLayout();
		mainlayout.setPadding(20);
		mainlayout.setShowEdges(true);
		final DynamicForm form = new DynamicForm();
		form.setEncoding(Encoding.MULTIPART);
		form.setID("form");
		form.setMethod(FormMethod.POST);
		form.setWrapItemTitles(false);
		String htmlstr = "<iframe id=\"iframeid\" name=\"iframename\" style=\"position:absolute;width:0;height:0;border:0\"> </iframe>";

		final HTMLFlow htmlFlow = new HTMLFlow();
		
		htmlFlow.setContents(htmlstr);
		htmlFlow.visibleAtPoint(1, 1);
		
		final SelectItem type = new SelectItem();
		type.setTitle("Logo Format");
		type.setValueMap("GIF","JPG","PNG");
		type.setDefaultValue("PNG");
		
		final UploadItem upload  = new UploadItem("Select File");
		form.setTarget("iframename");
		form.setFields(type,upload);
		
		IButton uploadbtn = new IButton();
		uploadbtn.setTitle("Upload");		
		
		uploadbtn.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {	

			public void onClick(
					com.smartgwt.client.widgets.events.ClickEvent event) {
				String logotype = type.getDisplayValue();
				
				if((logotype == null) || (logotype == "&nbsp;")) {
					SC.say("Upload Logo","Please select the logo format");
				
				} else {
							form.setAction("http://200.201.202.102:8080/MashupEngine/UpdateOrganization?UA=AdminClient&PLFO=true&ORID=test&OPLT=png&SESID=7658756a-34d2-48ff-9376-87f3a8404aca");
							form.submitForm();
//							NowTry();
							
								Timer t = new Timer() {

									public void run() {
										
										form.clearValues();
										
										NowTry();
									}
								};				
								t.schedule(5000);				
				}
			
		}
		});
		mainlayout.setMembersMargin(10);
		mainlayout.addMember(htmlFlow);
		mainlayout.addMember(form);
		mainlayout.addMember(uploadbtn);
		
		mainlayout.draw();	
	}
	public static native void NowTry() /*-{

	if (document.getElementById("result") != null){
		var iframeData = result.document.getElementById("result").innerHTML
		alert(iframeData);

	} else { alert("nope"); 
	}
}-*/;
}
