package com.ssdi.test.client;

import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.types.SelectionAppearance;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.util.Offline;
import com.smartgwt.client.widgets.events.DrawEvent;
import com.smartgwt.client.widgets.events.DrawHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.events.FieldStateChangedEvent;
import com.smartgwt.client.widgets.grid.events.FieldStateChangedHandler;

public abstract class DBListgrid extends ListGrid {

	/**
	 * add list grid fields
	 */
	public abstract void addListGridField();

	/**
	 * unique id per list grid and used for storing and retrieving list grid
	 * column state
	 */
	public abstract String getListGridId();

	/**
	 * true to display the list grid with check box
	 */
	public abstract boolean isSelectionType();

	public DBListgrid() {
		setListGridProperties();
	}

	/**
	 * set basic properties
	 */
	private void setListGridProperties() {
		setID(getListGridId());
		setWidth100();
		setHeight100();
		setMargin(10);
		setLeaveScrollbarGap(false);
		setShowRollOver(false);
		setShowFilterEditor(false);
		setCanAutoFitFields(true);
		setCanResizeFields(true);

		if (isSelectionType()) {
			setSelectionType(SelectionStyle.SIMPLE);
			setSelectionAppearance(SelectionAppearance.CHECKBOX);
		}

		setShowEmptyMessage(true);
		setEmptyMessage("NoItemsToShow");
		setShowFilterEditor(false);
		setFilterOnKeypress(true);
		setFetchDelay(500);
		setWarnOnRemoval(true);
		setShowRecordComponents(true);
		setShowRecordComponentsByCell(true);
		Criteria criteria = getCriteria();
		if (criteria != null) {
			setInitialCriteria(getCriteria());
		}
		addListGridField();
		addProperties();
		applyFieldState();

	}

	public boolean isToolStripRequired() {
		return true;
	}

	/**
	 * store list grid column fields in off line
	 */
	public void storeFieldState() {
		Offline.put(getListGridId(), getViewState());
	}

	/**
	 * apply list grid column state
	 */
	public void applyFieldState() {
		try {

			final String viewState = (String) Offline.get(getId());
			if (viewState != null) {
				addDrawHandler(new DrawHandler() {
					@Override
					public void onDraw(DrawEvent event) {
						// restore any previously saved view state for this grid
						setViewState(viewState);
					}
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		FieldStateChangedHandler fieldStateChangedHandler = new FieldStateChangedHandler() {

			@Override
			public void onFieldStateChanged(FieldStateChangedEvent event) {
				Offline.put(getId(), getViewState());
				updateFieldChangeCallBack();
			}
		};
		addFieldStateChangedHandler(fieldStateChangedHandler);
	}

	private String getId() {
		return getListGridId();
	}

	public void updateFieldChangeCallBack() {

	}

	/**
	 * add list grid properties
	 */
	public void addProperties() {
		setCellHeight(30);
		setHeaderHeight(30);
	}

	/**
	 * refresh list grid
	 */
	public void refresh() {
		invalidateCache();
		scrollToRow(0);
	}

}
