/**
 * 
 */
package org.richfaces.component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.FacesEvent;
import javax.faces.event.PhaseId;

import org.ajax4jsf.component.AjaxComponent;
import org.ajax4jsf.component.UIDataAdaptor;
import org.ajax4jsf.context.AjaxContext;
import org.ajax4jsf.event.AjaxEvent;
import org.ajax4jsf.model.DataComponentState;
import org.ajax4jsf.model.DataVisitor;
import org.ajax4jsf.model.ExtendedDataModel;
import org.ajax4jsf.model.Range;
import org.apache.commons.collections.iterators.IteratorChain;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.richfaces.event.AttributeHolder;
import org.richfaces.event.ScrollableGridViewEvent;
import org.richfaces.event.sort.MultiColumnSortListener;
import org.richfaces.event.sort.SingleColumnSortListener;
import org.richfaces.event.sort.SortEvent;
import org.richfaces.event.sort.SortListener;
import org.richfaces.model.DataModelCache;
import org.richfaces.model.ScrollableTableDataModel;
import org.richfaces.model.ScrollableTableDataRange;
import org.richfaces.model.SortOrder;
import org.richfaces.model.internal.ComponentSortableDataModel;


/**
 * @author Anton Belevich
 *
 */

public abstract class UIScrollableDataTable extends UIDataAdaptor implements AjaxComponent, Sortable, Selectable, ScriptExportable{
	
	
	public static final String COMPONENT_TYPE = "org.richfaces.component.ScrollableDataTable";
	public static final String SORT_SINGLE = "single";
	public static final String SORT_MULTI = "multi";

	private final static Log log = LogFactory.getLog(UIScrollableDataTable.class);
	
	/**
	 * Flag set on each phase to determine what range of data to walk
	 * true means - locally saved ranges (for data pending update)
	 * false means - use range that comes from component state
	 */
	private boolean useSavedRanges = true;
	
	/**
	 * hold list of ranges previously accessed until updates are fully done for them
	 */
	private List ranges;
	
	private Collection responseData = new ArrayList();
	
	private int reqRowsCount = -1;
	
	private String scrollPos;
	
	private SortListener sortListener;
	
	public abstract SortOrder getSortOrder();
	
	public abstract void setSortOrder(SortOrder sortOrder) ;
	
	public Collection getResponseData() {
		return responseData;
	}

	public void setResponseData(Collection responseData) {
		this.responseData = responseData;
	}
	
	protected DataComponentState createComponentState() {
	
		return new DataComponentState(){
		
			public Range getRange() {
				
				int curentRow = getFirst();

				if(reqRowsCount == -1 ){
					reqRowsCount = getRows();
				}
				
				int rows = reqRowsCount;
				
				int rowsCount = getExtendedDataModel().getRowCount();
				
				if(rows > 0){
				
					rows += curentRow;
					
					if(rows >=0){
						rows = Math.min(rows, rowsCount);
					}
					
				} else if(rowsCount >=0 ){
					rows = rowsCount;
					
				} else {
					rows = -1;
				}
				
				return new ScrollableTableDataRange(curentRow,rows, getSortOrder());
			}
		};
	}
	
	
	public void processDecodes(FacesContext faces) {
		
		useSavedRanges = false;
		
		if (log.isTraceEnabled()) {
			log.trace("UIScrollableDataTable.processDecodes(faces)");
		}
		super.processDecodes(faces);
	}
	
	public void processValidators(FacesContext faces) {
		useSavedRanges = true;
		if (log.isTraceEnabled()) {
			log.trace("UIScrollableDataTable.processValidators(faces)");
		}
		super.processValidators(faces);
	}
	
	public void processUpdates(FacesContext faces) {
		useSavedRanges = true;
		
		if (log.isTraceEnabled()) {
			log.trace("UIScrollableDataTable.processUpdates(faces)");
		}
		
		super.processUpdates(faces);
		ranges = null;
	}
	
	public void encodeBegin(FacesContext context) throws IOException {
		
		if (log.isTraceEnabled()) {
			log.trace("UIScrollableDataTable.encodeBegin(context)");
		}
		
		useSavedRanges = false;
		super.encodeBegin(context);
	}
	
	protected ExtendedDataModel createDataModel() {
		
		Object value = getValue();
		
		ScrollableTableDataModel model = null;
		
		if (value instanceof ScrollableTableDataModel) {
			
			if (log.isDebugEnabled()) {
				log.debug("Found instanceof " + value.getClass() + " will use it unwrapped");
			}
			model = (ScrollableTableDataModel) value;
		
		} else {
			model = new ComponentSortableDataModel(getVar(), value);
		}
		
		if (isCacheable()) {
			
			if (log.isDebugEnabled()) {
				log.debug("Initializing cache of type " + DataModelCache.class);
			}

			model = new DataModelCache(model);
		}
		
		return model;		
	}
	
		
	public Object saveState(FacesContext context) {
		
		Object values[] = new Object[4];
		values[0] = super.saveState(context);
		values[1] = ranges;
		values[2] = scrollPos;
		values[3] = saveAttachedState(context, sortListener);
		
		return (Object)values;
	
	}
	
	public void restoreState(FacesContext context, Object state) {
		Object values[] = (Object[])state;
		super.restoreState(context, values[0]);
		ranges = ((List)values[1]);
		scrollPos = (String)values[2]; 
		sortListener = (SortListener) restoreAttachedState(context, values[3]);
	}

	protected Iterator dataChildren() {
		
		IteratorChain chain = new IteratorChain();
		chain.addIterator(getFacets().values().iterator());
		for (Iterator i = getChildren().iterator(); i.hasNext(); ) {
			UIComponent kid = (UIComponent)i.next();
			if (kid instanceof Column) {
				chain.addIterator(kid.getChildren().iterator());
			}
		}
		
		return chain;
	}
	
	protected Iterator fixedChildren() {
	
		IteratorChain chain = new IteratorChain(getFacets().values().iterator());
		for (Iterator i = getChildren().iterator(); i.hasNext(); ) {
			UIComponent kid = (UIComponent)i.next();
			if (kid instanceof Column) {
				chain.addIterator(kid.getFacets().values().iterator());
			}
		}
		
		return chain;
	}
	
	public void broadcast(FacesEvent event) throws AbortProcessingException {
		
		super.broadcast(event);
		
		if (event instanceof AttributeHolder) {
			((AttributeHolder) event).applyAttributes(this);
		}

		if(event instanceof AjaxEvent){
			AjaxContext.getCurrentInstance().addComponentToAjaxRender(this);
		}else if(event instanceof SortEvent){
			processSortingChange(event);
		//	new AjaxEvent(this).queue();
		}else if(event instanceof ScrollableGridViewEvent){
		//	new AjaxEvent(this).queue();
			processScrolling(event);
		}
		
	}
	
	protected boolean broadcastLocal(FacesEvent event) {
		return super.broadcastLocal(event) || event instanceof SortEvent || event instanceof ScrollableGridViewEvent;
	}
	
	public void queueEvent(FacesEvent event) {
		
		if(event instanceof AjaxEvent){
			event.setPhaseId(PhaseId.APPLY_REQUEST_VALUES);
		}else if(event instanceof SortEvent){
			new AjaxEvent(this).queue();
			event.setPhaseId(PhaseId.APPLY_REQUEST_VALUES);
		}else if(event instanceof ScrollableGridViewEvent){
			event.setPhaseId(PhaseId.APPLY_REQUEST_VALUES);
			new AjaxEvent(this).queue();
		}
		super.queueEvent(event);
	}
	
	public void processScrolling(FacesEvent event){
		ScrollableGridViewEvent e = (ScrollableGridViewEvent)event;
		
		setFirst(e.getFirst());
		reqRowsCount = e.getRows();
		
		getFacesContext().renderResponse();
	}
	
	public void processSortingChange(FacesEvent event){
		
		SortEvent e = (SortEvent)event;
		
		getSortListener().processSort(e);
	
		setFirst(e.getFirst());
		reqRowsCount = e.getRows();
		
		
		resetDataModel();
		
		getFacesContext().renderResponse();
	}
	
	public void walk(FacesContext faces, DataVisitor visitor, Object argument) throws IOException {
		
		Range visitedRange = getComponentState().getRange();
	
		if(ranges == null){
			ranges = new ArrayList();
		}
		
		if(!ranges.contains(visitedRange)){
			ranges.add(visitedRange);
		}
				
		if(useSavedRanges){
			
			for (Iterator iter = ranges.iterator(); iter.hasNext();) {
				ScrollableTableDataRange range = (ScrollableTableDataRange) iter.next();
				
				if (log.isDebugEnabled()) {
					log.debug("Range is: " + range.getFirst() + " - " + range.getLast() + " sortOrder: " + range.getSortOrder() );
				}
				
				
				getExtendedDataModel().walk(faces, visitor,range, argument);
			}
			
		}else{
			super.walk(faces, visitor, argument);
		}
	}
	
	
	public void encodeEnd(FacesContext context) throws IOException {
		super.encodeEnd(context);
	}
	
	public boolean isCacheable() {
		return true;
	}

	public String getScrollPos() {
		return scrollPos;
	}

	public void setScrollPos(String scrollPos) {
		this.scrollPos = scrollPos;
	}

	public SortListener getSortListener() {
		if (sortListener != null) {
			return sortListener;
		}
		
		if (SORT_MULTI.equals(getSortMode())) {
		
			return MultiColumnSortListener.INSTANCE;
	
		} else {
		
			return SingleColumnSortListener.INSTANCE;
		
		}
	}

	public void setSortListener(SortListener sortListener) {
		this.sortListener = sortListener;
	}
	
	public abstract String getSortMode();
	public abstract void setSortMode(String mode);
	
	
	/* (non-Javadoc)
	 * @see org.ajax4jsf.component.UIDataAdaptor#setRowIndex(int)
	 */
	public void setRowIndex(int index) {
		if (index < 0) {
			setRowKey(null);
		}
		//super.setRowIndex(index);
	}
}
