/**
 * PagingForm.java
 * Created Jun 18, 2008
 * Copyright (c) TANDBERG Television 2007-2008
 */
package com.tandbergtv.watchpoint.pmm.web.title;

import static com.tandbergtv.watchpoint.pmm.web.title.TitleRequest.PAGE_SIZE;
import static com.tandbergtv.watchpoint.pmm.web.title.TitleRequest.SORT_COLUMN;
import static com.tandbergtv.watchpoint.pmm.web.title.TitleRequest.SORT_ORDER;
import static com.tandbergtv.watchpoint.pmm.web.title.TitleRequest.START;
import static com.tandbergtv.workflow.util.SortingOrder.ASCENDING;
import static com.tandbergtv.workflow.util.SortingOrder.DESCENDING;

import com.tandbergtv.workflow.web.formbeans.PaginationAndSortingForm;
import com.tandbergtv.workflow.util.SortingOrder;
import com.tandbergtv.workflow.web.table.Column;

/**
 * Provides additional support for paging and sorting when the query URL is used for search
 * 
 * @author Sahil Verma
 */
public abstract class PagingForm extends PaginationAndSortingForm {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5044591449569510290L;

	protected String query;

	protected int start;
	
	/**
	 * Creates a PagingForm
	 */
	public PagingForm() {
	}

	/**
	 * @return the query
	 */
	public String getQuery() {
		return this.query;
	}

	/**
	 * @param query the query to set
	 */
	public void setQuery(String query) {
		this.query = query;
	}

	/**
	 * @return the start
	 */
	public int getStart() {
		return this.start;
	}

	/**
	 * @param start the start to set
	 */
	public void setStart(int start) {
		this.start = start;
	}
	
	/**
	 * Returns the URL for the specified page size.
	 * 
	 * @param size
	 * @return a url if paging is allowed, null otherwise
	 */
	public String getPageUrl(String size) {
		int i = Integer.parseInt(size);

		if ((i == getPageSize()) || this.getPreviousPageSize(size) >= this.getTotalRecords())
			return null;

		return query + getUrl(0, i);
	}

	/* Given a size, return the previous size in the page size list, or 0 if none exists */
	private int getPreviousPageSize(String size) {
		int previousSize = 0;

		String[] sizes = this.getPageSizes();
		for (int i = 0; i < sizes.length; i++) {
			if (sizes[i].equals(size)) {
				previousSize = (i > 0) ? Integer.parseInt(sizes[i - 1]) : 0;
				break;
			}
		}

		return previousSize;
	}

	/**
	 * Returns the current page URL
	 * 
	 * @return
	 */
	public String getCurrentUrl() {
		return query + getUrl(start, getPageSize());
	}

	/**
	 * Gets the URL for the next page
	 * @return
	 */
	public String getNextUrl() {
		int page = getCurrentPageNo();

		if (page < getMaximumPageNumber()) {
			int start = page * getPageSize();
			int count = getPageSize();

			return query + getUrl(start, count);
		}

		return null;
	}

	/**
	 * Gets the URL of the previous page
	 * 
	 * @return
	 */
	public String getPreviousUrl() {
		int page = getCurrentPageNo() - 2;

		if (page >= 0) {
			int start = page * getPageSize();
			int count = getPageSize();

			return query + getUrl(start, count);
		}

		return null;
	}

	/**
	 * Gets the URL of the first page
	 * @return
	 */
	public String getFirstUrl() {
		if (getCurrentPageNo() == 1)
			return null;

		return query + getUrl(0, getPageSize());
	}

	/**
	 * Gets the URL of the last page
	 * 
	 * @return
	 */
	public String getLastUrl() {
		if (getCurrentPageNo() == getMaximumPageNumber())
			return null;

		int start = (getMaximumPageNumber() - 1) * getPageSize();
		int count = getPageSize();

		return query + getUrl(start, count);
	}

	/**
	 * Gets the URL without start index specified. The Start index must be appended to this URL.
	 * @return
	 */
	public String getGotoUrl() {
		if (this.getNumberOfPages() <= 1)
			return null;

		String url = query + getUrl(0, getPageSize());
		int startIndex = url.indexOf(START.toString());
		int endIndex = url.indexOf("&", startIndex + START.toString().length());
		url = url.substring(0, startIndex) + ((endIndex != -1) ? url.substring(endIndex) : "");
		return url;
	}

	/**
	 * Returns the URL for the specified column name
	 * 
	 * @param columnName
	 * @return
	 */
	public String getColumnUrl(String columnName) {
		Column column = getTable().getColumn(columnName);
		String name = column.getSortingColumnName();

		if (name.equals(getSortingColumnName())) {
			SortingOrder order = SortingOrder.valueOf(getSortingOrder());

			if (order == ASCENDING)
				return query + getUrl(start, getPageSize(), name, DESCENDING.toString());

			return query + getUrl(start, getPageSize(), name, ASCENDING.toString());
		}

		return query + getUrl(start, getPageSize(), name, getTable().getDefaultSortingOrder());
	}

	protected String getUrl(int start, int count) {
		StringBuilder s = new StringBuilder();

		return s.append(START).append(start).append(PAGE_SIZE).append(count).append(SORT_COLUMN)
				.append(getSortingColumnName()).append(SORT_ORDER).append(getSortingOrder())
				.toString();
	}

	protected String getUrl(int start, int count, String column, String order) {
		StringBuilder s = new StringBuilder();

		return s.append(START).append(start).append(PAGE_SIZE).append(count).append(SORT_COLUMN)
				.append(column).append(SORT_ORDER).append(order).toString();
	}
}
