package es.ejie.frmk.presentacion.iu.taglib.html.table.pagination;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import es.ejie.frmk.infraestructura.traza.Q70TraceLevel;
import es.ejie.frmk.infraestructura.traza.Q70Traza;
import es.ejie.frmk.presentacion.iu.taglib.base.Q70Constants;

/**
 * @author fgiust
 * @version 1.0
 */
public class Q70Pagination
{

	/**
	 * Instancia del log de trazas
	 */
	
	private static Q70Traza traza = Q70Traza.getSingleton();

	/**
	 * MessageFormat for urls
	 */
	private MessageFormat urlFormat;

	/**
	 * first page
	 */
	private Integer firstPage;

	/**
	 * last page
	 */
	private Integer lastPage;

	/**
	 * previous page
	 */
	private Integer previousPage;

	/**
	 * next page
	 */
	private Integer nextPage;

	/**
	 * ArrayList containg Q70NumberedPage objects
	 * @see org.displaytag.pagination.Q70NumberedPage
	 */
	private ArrayList pages = new ArrayList();

	/**
	 * Constructor for Q70Pagination
	 * @param urlFormatString String
	 */
	public Q70Pagination(String urlFormatString)
	{
		this.urlFormat = new MessageFormat(urlFormatString);
	}

	/**
	 * add a page
	 * @param number int page number
	 * @param isSelected is the page selected?
	 */
	public void addPage(int number, boolean isSelected)
	{
		if (traza.isTraceActive()){
			traza.trace(Q70Constants.COMPONENTE_CUSTOM_TAG, Q70TraceLevel.DEBUG, "addpage: " + number);
		}
		pages.add(new Q70NumberedPage(number, isSelected));
	}

	/**
	 * first page selected?
	 * @return boolean
	 */
	public boolean isFirst()
	{
		return firstPage == null;
	}

	/**
	 * last page selected?
	 * @return boolean
	 */
	public boolean isLast()
	{
		return lastPage == null;
	}

	/**
	 * only one page?
	 * @return boolean
	 */
	public boolean isOnePage()
	{
		return (pages == null) || pages.size() <= 1;
	}

	/**
	 * Get the number of the first page
	 * @return Integer number of the first page
	 */
	public Integer getFirst()
	{
		return firstPage;
	}

	/**
	 * Set the number of the first page
	 * @param first Integer number of the first page
	 */
	public void setFirst(Integer first)
	{
		firstPage = first;
	}

	/**
	 * Get the number of the last page
	 * @return Integer number of the last page
	 */
	public Integer getLast()
	{
		return lastPage;
	}

	/**
	 * Set the number of the last page
	 * @param last Integer number of the last page
	 */
	public void setLast(Integer last)
	{
		lastPage = last;
	}

	/**
	 * Get the number of the previous page
	 * @return Integer number of the previous page
	 */
	public Integer getPrevious()
	{
		return previousPage;
	}

	/**
	 * Set the number of the previous page
	 * @param previous Integer number of the previous page
	 */
	public void setPrevious(Integer previous)
	{
		previousPage = previous;
	}

	/**
	 * Get the number of the next page
	 * @return Integer number of the next page
	 */
	public Integer getNext()
	{
		return nextPage;
	}

	/**
	 * Set the number of the next page
	 * @param next Integer number of the next page
	 */
	public void setNext(Integer next)
	{
		nextPage = next;
	}

	/**
	 * returns the appropriate banner for the pagination
	 * @param numberedPageFormat String to be used for a not selected page
	 * @param numberedPageSelectedFormat String to be used for a selected page
	 * @param numberedPageSeparator separator beetween pages
	 * @param fullBanner String basic banner
	 * @return String formatted banner whith pages
	 */
	public String getFormattedBanner(
		String numberedPageFormat,
		String numberedPageSelectedFormat,
		String numberedPageSeparator,
		String fullBanner, 
		boolean isPOST)
	{
		StringBuffer buffer = new StringBuffer(100);

		// numbered page list
		Iterator pageIterator = pages.iterator();

		while (pageIterator.hasNext())
		{

			// get Q70NumberedPage from iterator
			Q70NumberedPage page = (Q70NumberedPage) pageIterator.next();

			Integer pageNumber = new Integer(page.getNumber());

			String urlString = this.urlFormat.format(new Object[] { pageNumber });
			
			//Compronbar si hay que hacer paginacion en modo POST.
			if (isPOST)	
				urlString = "javascript:frmkPost('" + urlString + "')";
		
			
			// needed for MessageFormat : page number/url
			Object[] pageObjects = { pageNumber, urlString };

			// selected page need a different formatter
			if (page.getSelected())
			{
				buffer.append(MessageFormat.format(numberedPageSelectedFormat, pageObjects));
			}
			else
			{
				buffer.append(MessageFormat.format(numberedPageFormat, pageObjects));
			}

			// next? add page separator
			if (pageIterator.hasNext())
			{
				buffer.append(numberedPageSeparator);
			}
		}

		// String for numbered pages
		String numberedPageString = buffer.toString();

		//  Object array
		//  {0} full String for numbered pages
		//  {1} first page url
		//  {2} previous page url
		//  {3} next page url
		//  {4} last page url
		Object[] pageObjects = new Object[5];
		pageObjects[0] = numberedPageString;

		if (isPOST)
		{ 	
			pageObjects[1] = (getFirst()==null?this.urlFormat.format(new Object[] { getFirst()}): "javascript:frmkPost('" + this.urlFormat.format(new Object[] { getFirst()}) + "')" );
			pageObjects[2] = (getPrevious()==null?this.urlFormat.format(new Object[] { getPrevious()}): "javascript:frmkPost('" + this.urlFormat.format(new Object[] { getPrevious()}) + "')" );
			pageObjects[3] = (getNext()==null?this.urlFormat.format(new Object[] { getNext()}): "javascript:frmkPost('" + this.urlFormat.format(new Object[] { getNext()}) + "')" );
			pageObjects[4] = (getLast()==null?this.urlFormat.format(new Object[] { getLast()}): "javascript:frmkPost('" + this.urlFormat.format(new Object[] { getLast()}) + "')" );			
		}
		else
		{
			pageObjects[1] = this.urlFormat.format(new Object[] { getFirst()});
			pageObjects[2] = this.urlFormat.format(new Object[] { getPrevious()});
			pageObjects[3] = this.urlFormat.format(new Object[] { getNext()});
			pageObjects[4] = this.urlFormat.format(new Object[] { getLast()});
		}	


		// return the full banner
		return MessageFormat.format(fullBanner, pageObjects);
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString()
	{
		return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE)
			.append("firstPage", this.firstPage)
			.append("lastPage", this.lastPage)
			.append("nextPage", this.nextPage)
			.append("previousPage", this.previousPage)
			.append("pages", this.pages)
			.append("urlFormat", this.urlFormat)
			.toString();
	}
}