package com.ssdi.test.client;

import com.ssdi.test.client.DBListgrid;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.SortDirection;
import com.smartgwt.client.types.VisibilityMode;
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.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;

public class ChangesDisplayWindows {
	private Window changeConfirmGroupWindow; 
	ListGrid changeLogListforGroup;

	/***
	 * createWindowInfoForGroup()
	 */
	public void createWindowInfoForGroup() {
		showWindowForGroup();
	}

	/***
	 * showWindowForGroup()
	 */

	private void showWindowForGroup() {

		int browserWidth = com.google.gwt.user.client.Window.getClientWidth();
		int browserHeight = com.google.gwt.user.client.Window.getClientHeight();
		changeConfirmGroupWindow = new Window();
		if (browserWidth > 610) {
			changeConfirmGroupWindow.setWidth(610);
		} else {
			changeConfirmGroupWindow.setWidth(browserWidth);
		}
		if (browserHeight > 470) {
			changeConfirmGroupWindow.setHeight(470);
		} else {
			changeConfirmGroupWindow.setHeight(browserHeight);
		}
		changeConfirmGroupWindow.setTitle("ConfirmWindowTitle");
		changeConfirmGroupWindow.setShowMinimizeButton(false);
		changeConfirmGroupWindow.setIsModal(true);
		changeConfirmGroupWindow.setShowModalMask(true);
		changeConfirmGroupWindow.centerInPage();
		changeConfirmGroupWindow.setCanDragResize(true);
		changeConfirmGroupWindow.setKeepInParentRect(true);
		changeConfirmGroupWindow.setAutoCenter(true);
		changeConfirmGroupWindow.setMaxWidth(1024);
		changeConfirmGroupWindow.setMaxHeight(768);

		com.smartgwt.client.widgets.Canvas pollingSettingWindowMainCanvas = new com.smartgwt.client.widgets.Canvas();

		VLayout groupChangeConfLayout = new VLayout();
		groupChangeConfLayout.setHeight100();
		groupChangeConfLayout.setWidth100();
		groupChangeConfLayout.setMinWidth(540);
		groupChangeConfLayout.setMinHeight(250);
		groupChangeConfLayout.setMembersMargin(5);
		groupChangeConfLayout.setMargin(5);

		// Filter Section
		SectionStack changeConfirmStack = getGroupChangeConfirmStack();
		groupChangeConfLayout.addMember(changeConfirmStack);

		// Close window
		HLayout confirmationSectionLayout = getCloseLayout(changeConfirmGroupWindow);
		groupChangeConfLayout.addMember(confirmationSectionLayout);

		pollingSettingWindowMainCanvas.addChild(groupChangeConfLayout);
		changeConfirmGroupWindow.addItem(pollingSettingWindowMainCanvas);
		changeConfirmGroupWindow.show();
	}

	/***
	 * getGroupChangeConfirmStack()
	 * 
	 * @return
	 */
	private SectionStack getGroupChangeConfirmStack() {
		SectionStack changeConfStack = new SectionStack();
		changeConfStack.setWidth100();
		changeConfStack.setCanReorderSections(false);
		changeConfStack.setCanResizeSections(false);
		changeConfStack.setVisibilityMode(VisibilityMode.MULTIPLE);
		changeConfStack.setOverflow(Overflow.VISIBLE);

		SectionStackSection changeLogSection = new SectionStackSection();
		changeLogSection.setTitle("ChangeLogList");
		changeLogSection.setExpanded(true);

		ListGrid changeLogGrid = getChangeLogGridforGroup();
		changeLogSection.addItem(changeLogGrid);

		changeConfStack.setSections(changeLogSection);

		return changeConfStack;
	}

	/***
	 * getChangeLogGridforGroup()
	 * 
	 * @return
	 */
	private ListGrid getChangeLogGridforGroup() {

		changeLogListforGroup = new DBListgrid() {

			@Override
			public boolean isSelectionType() {
				return false;
			}

			@Override
			public String getListGridId() {

				return "changeLogListforGroup";
			}

			@Override
			public void addListGridField() {

				ListGridField dateNTimeField = new ListGridField(
						"ChangeConfirmTime");
				dateNTimeField.setWidth("25%");
				dateNTimeField.setAlign(Alignment.LEFT);
				dateNTimeField.setSortByDisplayField(true);
				dateNTimeField.setSortDirection(SortDirection.DESCENDING);

				ListGridField modelNameField = new ListGridField("Name");
				modelNameField.setWidth("25%");
				ListGridField ipAddressField = new ListGridField("Address");
				ipAddressField.setWidth("25%");
				ListGridField changeAvailField = new ListGridField(
						"Availability");
				changeAvailField.setWidth("25%");

				setFields(dateNTimeField, modelNameField, ipAddressField,
						changeAvailField);

			}

		};

		changeLogListforGroup.setCanAutoFitFields(false);
		changeLogListforGroup.setWidth("100%");
		changeLogListforGroup.setDrawAheadRatio(4);
		changeLogListforGroup.setCanExpandRecords(true);
		changeLogListforGroup.setCanAutoFitFields(false);

		return changeLogListforGroup;

	}

	/***
	 * getCloseLayout()
	 * 
	 * @param wnd
	 * @return
	 */

	private HLayout getCloseLayout(final Window wnd) {
		HLayout confirmationSectionLayout = new HLayout();
		confirmationSectionLayout.setWidth100();
		confirmationSectionLayout.setHeight(30);
		confirmationSectionLayout.setMargin(5);
		confirmationSectionLayout.setMembersMargin(10);
		confirmationSectionLayout.setAlign(Alignment.CENTER);

		IButton closeButton = new IButton("Close");
		closeButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				wnd.destroy();
			}
		});
		confirmationSectionLayout.addMember(closeButton);
		return confirmationSectionLayout;
	}

}
