package com.dreamer.devcentre.client.xletProfiling.view;

import com.dreamer.devcentre.client.xletProfiling.presenter.WebSimulatorPresenter.WebSimulatorInterface;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.InputElement;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.types.FormItemType;
import com.smartgwt.client.types.ListGridEditEvent;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.events.ChangedEvent;
import com.smartgwt.client.widgets.grid.events.ChangedHandler;

public class WebSimulator extends Composite implements WebSimulatorInterface {

	private static WebSimulatorUiBinder uiBinder = GWT
			.create(WebSimulatorUiBinder.class);

	interface WebSimulatorUiBinder extends UiBinder<Widget, WebSimulator> {
	}

	@UiField
	VerticalPanel webListGrid;

	@UiField
	TextBox aliasTextBox;

	@UiField
	TextBox xletName;

	@UiField
	InputElement raBtn1;

	@UiField
	InputElement raBtn2;

	@UiField
	InputElement raBtn3;

	@UiField
	FileUpload fileUploadBtn;

	@UiField
	Button submit;

	@UiField
	Button cancel;

	@UiField
	HTMLPanel htmlPanel;
	
	@UiField
	FormPanel formPanel;
	
	private ListGrid listGrid = new ListGrid();

	public WebSimulator() {
		initWidget(uiBinder.createAndBindUi(this));
		makeListGridField();
	}

	@Override
	public Widget getViewWidget() {
		return this;
	}

	private void makeListGridField() {
		DataSource dataSource = new CompanyDataSource("companyList");
		dataSource.setClientOnly(true);

		// listGrid.setAlwaysShowEditors(true);
		listGrid.setHeight(400);
		listGrid.setWidth(850);
		listGrid.setShowAllRecords(true);
		listGrid.setDataSource(dataSource);
//		listGrid.setHeaderTitleStyle("gwt-ListGrid-header");
		ListGridField date = new ListGridField("date", "Date", 132);
		date = setFieldAlignCenter(date);
		ListGridField alias = new ListGridField("alias", "Alias", 113);
		alias = setFieldAlign(alias);
		ListGridField xletName = new ListGridField("xletName", "Xlet Name", 267);
		xletName = setFieldAlign(xletName);
		ListGridField timeStamp = new ListGridField("timeStamp", "TimeStamp",
				97);
		timeStamp = setFieldAlignCenter(timeStamp);
		ListGridField iteration = new ListGridField("iteration", "Iteration",
				57);
		iteration = setFieldAlign(iteration);
		ListGridField status = new ListGridField("status", "Status", 77);
		status = setFieldAlign(status);
		status.setCanEdit(true);
		ListGridField actions = new ListGridField("actions", "Actions", 102);
		actions.setValueMap("Choose Actions", "Delete", "View Result");
//		SelectItem actions = new SelectItem("actions", "Actions");
//		actions = setFieldAlignCenter(actions);
//		actions.setValueMap("Choose Actions", "Delete", "View Result");
//		actions.addChangedHandler(new ChangedHandler() {
//			
//			@Override
//			public void onChanged(ChangedEvent event) {
//				Window.alert("fuck");
//				
//			}
//		});
		
		listGrid.setAutoFetchData(true);
		listGrid.setEditEvent(ListGridEditEvent.CLICK);
		listGrid.setFields(date, alias, xletName, timeStamp, iteration, status, actions);
		webListGrid.add(listGrid);
	}

	private ListGridField setFieldAlign(ListGridField field) {
		field.setAlign(Alignment.CENTER);
		field.setCellAlign(Alignment.LEFT);
		field.setCanEdit(false);
		return field;
	}

	private ListGridField setFieldAlignCenter(ListGridField field) {
		field.setAlign(Alignment.CENTER);
		field.setCellAlign(Alignment.CENTER);
		 field.setCanEdit(true);
		if (field.getName().equals("actions")) {
			field.setCanEdit(false);
		} else {
			field.setCanEdit(false);
		}
		return field;
	}

	@Override
	public HasClickHandlers getSubmitButton() {
		return submit;
	}

	@Override
	public FormPanel getFormPanel() {
		return formPanel;
	}

	@Override
	public ListGrid getListGrid() {
		return listGrid;
	}

	@Override
	public HasClickHandlers getCancelButton() {
		return cancel;
	}

	@Override
	public TextBox getAliasTextBox() {
		return aliasTextBox;
	}

	@Override
	public FileUpload getFileUploadBtn() {
		return fileUploadBtn;
	}

	@Override
	public InputElement[] getRadioBtn() {
		InputElement[] input = { raBtn1, raBtn2, raBtn3 };
		return input;
	}

	@Override
	public TextBox getXletName() {
		return xletName;
	}

	@Override
	public HTMLPanel getHtmlPanel() {
		return htmlPanel;
	}

}
