package com.ssdi.test.client;


import com.google.gwt.core.client.GWT;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.AutoFitWidthApproach;
import com.smartgwt.client.types.BackgroundRepeat;
import com.smartgwt.client.types.ListGridComponent;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.SelectionAppearance;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.types.Side;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
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.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.menu.Menu;
import com.smartgwt.client.widgets.menu.MenuItem;
import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;

public class MainWindowView {

	IButton showBannerButton;
	private static MainWindowView instance;

	public static MainWindowView getInstance() {
		if (instance == null) {
			instance = new MainWindowView();
		}
		return instance;

	}

	private VLayout mainContentHLayout;
	private VLayout mainPanelAndBannerLayout;
	private HLayout mainPanelTitleHLayout;
	private HLayout mainPanelOuter;
	private TabSet groupDetailTabSet;
	
	public VLayout getMainContentHLayout() {
		return mainContentHLayout;
	}

	private MainWindowView() {

		mainContentHLayout = new VLayout();
		mainContentHLayout.setWidth("100%");
		mainContentHLayout.setHeight("100%");
		mainContentHLayout.setMargin(10);
		mainContentHLayout.setBorder("1px solid green");
		mainContentHLayout.setOverflow(Overflow.VISIBLE);
		mainContentHLayout.setAlign(Alignment.CENTER);

		// Level RootPanel_mainContentHLayout_mainPanelOuter
		//
		mainPanelOuter = new HLayout();
		mainPanelOuter.setWidth100();
		mainPanelOuter.setHeight100();
		mainPanelOuter.setTitle("");
		mainPanelOuter.setBorder("1px solid red");

		// Layout for SIICA Based Header
		//
		mainPanelTitleHLayout = new HLayout();
		mainPanelTitleHLayout.setWidth100();
		mainPanelTitleHLayout.setHeight(63);
		mainPanelTitleHLayout.setBorder("3px solid green");

		HLayout titleLayout = new HLayout();
		titleLayout.setWidth100();
		titleLayout.setBorder("1px solid red");
		titleLayout.setHeight(55);
		titleLayout.setBackgroundRepeat(BackgroundRepeat.NO_REPEAT);
		
		mainPanelTitleHLayout.addMember(titleLayout);
		
		mainPanelAndBannerLayout = new VLayout();
		mainPanelAndBannerLayout.setHeight("30%");
		mainPanelAndBannerLayout.setWidth100();
		mainPanelAndBannerLayout.setBorder("1px solid yellow");

		
		mainContentHLayout.addMember(mainPanelAndBannerLayout);
		mainPanelOuter.addMembers(createTabSet());
		
		// ADD MENU-START
		mainContentHLayout.addMember(mainPanelOuter);
		mainContentHLayout.show();
	}


	public TabSet createTabSet() {

		// Step3: Create Group Details Tab Set
		groupDetailTabSet = new TabSet();
		groupDetailTabSet.setTitle("Group Tab Set");
		groupDetailTabSet.setTabBarPosition(Side.TOP);
		groupDetailTabSet.setTabBarAlign(Side.LEFT);

		groupDetailTabSet.setWidth100();
		groupDetailTabSet.setHeight100();

		// Step3.1: Add Tabs
		//

		// Step3.1.1 - Add deviceListTab
		Tab deviceListTab = new Tab("Group list");

		VLayout vlayout = new VLayout();
		vlayout.setWidth100();
		vlayout.setHeight100();
		vlayout.addChild(getListGrid());
		deviceListTab.setPane(vlayout);
		groupDetailTabSet.addTab(deviceListTab);
		return groupDetailTabSet;
	}

	private IButton settingsButton;

	public ListGrid getListGrid() {
		final ListGrid testGrid = new ListGrid() {
			@Override
			protected Canvas createRecordComponent(final ListGridRecord record,
					Integer columnNumber) {
				String str = GWT.getHostPageBaseURL()
						+ "images/StatusUpdate_Start.png";
				String fieldName = this.getFieldName(columnNumber);
				// artf9952 : [MIB] Discovery & Edit Group settings has changed
				// to IMenu Button
				if (fieldName.equals("operations")) {

					settingsButton = new IButton("Settings" + ""+ Canvas.imgHTML(GWT.getHostPageBaseURL()+"images/sort_descending.png", 16, 16));
					settingsButton.setAutoFit(true);

					settingsButton
							.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {

								@Override
								public void onClick(ClickEvent event) {
									getGroupsettingsMenu();
								}
							});
					return settingsButton;
				}
				return null;
			}
		};
		testGrid.setShowRecordComponents(true);
		testGrid.setWidth100();
		testGrid.setHeight100();

		testGrid.setMargin(10);
		testGrid.setLeaveScrollbarGap(false);
		testGrid.setShowRollOver(false);
		testGrid.setShowFilterEditor(false);
		testGrid.setCanAutoFitFields(false);
		testGrid.setCanResizeFields(true);
		testGrid.setSelectionType(SelectionStyle.SIMPLE);
		testGrid.setSelectionAppearance(SelectionAppearance.CHECKBOX);
		testGrid.setCellHeight(30);
		testGrid.setHeaderHeight(30);
		testGrid.setAutoFitWidthApproach(AutoFitWidthApproach.BOTH);
		testGrid.setShowEmptyMessage(true);
		testGrid.setShowFilterEditor(false);
		testGrid.setFilterOnKeypress(true);
		testGrid.setFetchDelay(500);
		testGrid.setWarnOnRemoval(true);
		testGrid.setShowRecordComponents(true);
		testGrid.setShowRecordComponentsByCell(true);

		testGrid.setShowRecordComponentsByCell(true);
		testGrid.setGridComponents(new Object[] { ListGridComponent.HEADER,
				ListGridComponent.FILTER_EDITOR, ListGridComponent.BODY });
		ListGridField test1 = new ListGridField("test1", "Test1");
		ListGridField test2 = new ListGridField("test2", "Test2");
		ListGridField test3 = new ListGridField("test3", "Test3");
		ListGridField test4 = new ListGridField("test4", "Test4");
		ListGridField operations = new ListGridField("operations", "Operations");

		testGrid.setFields(test1, test2, test3, test4, operations);

		Record[] records = new Record[50];

		for (int i = 0; i < 50; i++) {
			records[i] = new Record();
			records[i].setAttribute("test1", "test" + i);
			records[i].setAttribute("test2", "test" + i);
			records[i].setAttribute("test3", "test" + i);
			records[i].setAttribute("test4", "test" + i);
		}
		testGrid.setData(records);
		return testGrid;
	}

	private Menu groupSettingsMenu;

	public void getGroupsettingsMenu() {

		// Group Settings Menu Items
		groupSettingsMenu = new Menu();

		MenuItem[] settingsMenuItems = new MenuItem[3];

		int itemNumber = 0;
		{

			// Group Settings For Discovery Settings
			settingsMenuItems[itemNumber] = new MenuItem("Select1");
			settingsMenuItems[itemNumber]
					.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler() {

						@Override
						public void onClick(MenuItemClickEvent event) {

						}
					});
			itemNumber++;

			// Group Settings For Schedule Settings
			settingsMenuItems[itemNumber] = new MenuItem("Select 2");
			settingsMenuItems[itemNumber]
					.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler() {

						@Override
						public void onClick(MenuItemClickEvent event) {

						}
					});
			itemNumber++;

			// Group Edit Window Open
			settingsMenuItems[itemNumber] = new MenuItem("Edit Group");
			settingsMenuItems[itemNumber]
					.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler() {

						@Override
						public void onClick(MenuItemClickEvent event) {
							// Edit Group Widow Opening
						}
					});

		}
		groupSettingsMenu.setItems(settingsMenuItems);
		groupSettingsMenu.showNextTo(settingsButton, "right");
		groupSettingsMenu.showContextMenu();
	}

}