package sometest.client;

import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.events.CloseClientEvent;
import com.smartgwt.client.widgets.events.RightMouseDownEvent;
import com.smartgwt.client.widgets.events.RightMouseDownHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.CellClickEvent;
import com.smartgwt.client.widgets.grid.events.CellClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.menu.Menu;
import com.smartgwt.client.widgets.menu.MenuItem;

public class MyTestController {
	private VLayout myLayout;

	private ListGrid myGrid;

	private ListGridRecord[] lastSelectionForNaming;

	public MyTestController(SmartGWTGridTest mainView) {
		init();

		useContextMenu();

		addSomeTestData();
	}
	
	private void useContextMenu() {
		final Menu contextMenu = new Menu();
		contextMenu.setShowShadow(true);
		contextMenu.setShadowDepth(10);
		
		
		myGrid.addRightMouseDownHandler(new RightMouseDownHandler() {

			@Override
			public void onRightMouseDown(RightMouseDownEvent event) {
				String selectedRecordGroup = myGrid.getSelectedRecord().getAttribute("Col3ToBeGrouped");
				if (contextMenu.getItem(1) != null) {
					MenuItem item = contextMenu.getItem(1);
					contextMenu.removeItem(item);
				}

				contextMenu.addItem(new MenuItem("Regroup to " + "test")); 
			}
		});
		
		myGrid.setContextMenu(contextMenu);
		
		contextMenu.addCellClickHandler(new CellClickHandler() {
			public void onCellClick(CellClickEvent event) {
				lastSelectionForNaming = myGrid.getSelection();

				if (event.getRowNum() == 0) {
					changeWithWindow();
				} 
			}
		});
	}
	
	

	protected void changeWithWindow() {
		final Window winModal = new Window();
		winModal.setWidth(400);
		winModal.setHeight(150);
		winModal.setTitle("Enter Name");
		winModal.setShowMinimizeButton(false);
		winModal.setIsModal(true);
		winModal.setShowModalMask(true);
		winModal.centerInPage();
		winModal.addCloseClickHandler(new CloseClickHandler() {
			public void onCloseClick(CloseClientEvent event) {
				winModal.destroy();
			}
		});

		// Name Field
		final TextItem textItem = new TextItem();
		textItem.setTitle("<b>Name</b>");

		// SaveButton
		final IButton btnSaveName = new IButton("Save");
		btnSaveName.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				lastSelectionForNaming = myGrid.getSelection();

				for (int i = 0; i < lastSelectionForNaming.length; i++) {
					String name = "Ungrouped";
					if (textItem.getValue() != null) {
						name = textItem.getValue().toString();
					}

					lastSelectionForNaming[i].setAttribute("Col3ToBeGrouped", name);
				}

				myGrid.groupBy("Col3ToBeGrouped");
				myGrid.redraw();
				
				winModal.destroy();
			}
		});

		DynamicForm form = new DynamicForm();

		form.setFields(textItem);

		HLayout formLayout = new HLayout();
		formLayout.setMargin(20);
		formLayout.addMember(form);
		formLayout.addMember(btnSaveName);

		winModal.addItem(formLayout);
		winModal.show();
	}

	private void init() {
		myLayout = new VLayout(2);
		myLayout.setSize("100%", "100%");

		myGrid = new ListGrid();
		myGrid.setHeight(400);
		myGrid.setShowAllRecords(true);
		myGrid.setShowEmptyMessage(false);
		myGrid.setEmptyMessage("Loading...");

		ListGridField col1 = new ListGridField("Col1", "Col1", 100);
		ListGridField col2 = new ListGridField("Col2", "Col2", 100);

		// This is the field to create a group
		ListGridField col3ForGrouping = new ListGridField("Col3ToBeGrouped",
				"Col3ToBeGrouped", 70);
		col3ForGrouping.setCanEdit(true);

		myGrid.setFields(col1, col2, col3ForGrouping);

		myGrid.groupBy("Col3ToBeGrouped");

		myGrid.draw();

		myLayout.addMember(myGrid);

		RootPanel.get().add(myLayout);
	}

	private void addSomeTestData() {
		ListGridRecord[] testDataList = new ListGridRecord[4];

		testDataList[0] = createRecord("A1", "A1", "Ungrouped");
		testDataList[1] = createRecord("B1", "B1", "Ungrouped");
		testDataList[2] = createRecord("C1", "C1", "Ungrouped");
		testDataList[3] = createRecord("D1", "D1", "Ungrouped");

		myGrid.setData(testDataList);
	}

	private ListGridRecord createRecord(String col1, String col2,
			String col3ToBeGrouped) {
		ListGridRecord retVal = new ListGridRecord();
		retVal.setAttribute("Col1", col1);
		retVal.setAttribute("Col2", col2);
		retVal.setAttribute("Col3ToBeGrouped", col3ToBeGrouped);

		return retVal;
	}

}
