/**
 * 
 */
package org.richfaces.realworld.tags;

import java.io.IOException;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

import org.richfaces.component.UIDatascroller;
import org.richfaces.renderkit.html.DatascrollerTemplate;

/**
 * @author Andrey Markavtsov
 *
 */
//TODO nick - remove this
public class RealworldImageScrollerRenderer extends DatascrollerTemplate {
	
	@Override
	public void renderPager(FacesContext context, UIComponent component,
			int pageIndex, int count) throws IOException {
		  ResponseWriter out = context.getResponseWriter();
	        UIDatascroller scroller = (UIDatascroller) component;
	        int currentPage = pageIndex;

	        int maxPages = scroller.getMaxPages();
	        if (maxPages <= 1) {
	            maxPages = 1;
	        }
	        
	        int pageCount = count;
	        if (pageCount <= 1) {
	            return;
	        }

	        int delta = maxPages / 2;

	        int pages;
	        int start;
	        if (pageCount > maxPages && currentPage > delta) {
	            pages = maxPages;
	            start = currentPage - pages / 2 - 1;
	            if (start + pages > pageCount) {
	                start = pageCount - pages;
	            }
	        } else {
	            pages = pageCount < maxPages ? pageCount : maxPages;
	            start = 0;
	        }

	        for (int i = start, size = start + pages; i < size; i++) {

	            boolean isCurrentPage = (i + 1 == currentPage);
	            String styleClass;
	            String style;
				if (isCurrentPage) {
				    styleClass = scroller.getSelectedStyleClass();
				    style = scroller.getSelectedStyle();
				} else {
					styleClass = scroller.getInactiveStyleClass();
				    style = scroller.getInactiveStyle();
				}
				if (styleClass==null){
				    styleClass="";
				}

	            out.startElement("td", component);
	            out.startElement("div", component);
	            
	            if (!isCurrentPage) {
	            	out.writeAttribute("onclick", getOnClick(Integer.toString(i + 1)), null);
	            }
	            
	            renderPageVar(context, component, i);
	            
	            if (isCurrentPage) {
	                out.writeAttribute("class", styleClass, null);
	            	UIComponent facet = component.getFacet("activePage");
	            	if (facet != null) {
		            	renderChild(context, facet);
	            	}
	            } else {
	            	out.writeAttribute("class", styleClass, null);
	            	UIComponent facet = component.getFacet("inactivePage");
	            	if (facet != null) {
		            	renderChild(context, facet);
	            	}
	            }
	            if (null != style) out.writeAttribute("style", style, null);
	            //out.writeText(Integer.toString(i + 1), null);
	            //renderChild(context, link);
	            out.endElement("div");
	            out.endElement("td");
	        }
	}
	
	void renderPageVar(FacesContext context, UIComponent component, int i) {
		 String varName = (String) component.getAttributes().get("pageVar");
	        if (varName != null && varName.length() > 0) {
	            context.getExternalContext()
	                    .getRequestMap().put(varName, new Integer(i));
	        }
	}
	
	@Override
	protected Class getComponentClass() {
		return super.getComponentClass();
	}

}
