/*
 * Created on Sep 15, 2011
 * 
 * (C) Copyright Ericsson Television Inc.
 */

package com.tandbergtv.workflow.web.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Handler to manage the page size for a pagination form. If the request contains a page size,
 * writes the page size to a cookie. If the request does not contain a page size, overrides the page
 * size value in the populated action form with the value from the cookie (if it exists).
 * 
 * @author Vijay Silva
 */
public class PageSizeHandler {

	/** The default page size parameter name */
	public static final String PAGE_SIZE_PARAMETER = "pageSize";

	/* The page size parameter name */
	private final String parameterName;

	/* The page size cookie name */
	private final String cookieName;

	/**
	 * Constructor, uses the default parameter name
	 * 
	 * @param cookieName The cookie name
	 */
	public PageSizeHandler(String cookieName) {
		this(PAGE_SIZE_PARAMETER, cookieName);
	}

	/**
	 * Constructor
	 * 
	 * @param parameterName The parameter name for the page size input parameter
	 * @param cookieName The name of the cookie
	 */
	public PageSizeHandler(String parameterName, String cookieName) {
		this.parameterName = parameterName;
		this.cookieName = cookieName;
	}

	/**
	 * Read the page size from the request or get the default value from the cookie if no valid
	 * value is in the request. If the request has a valid valid, write this value to the cookie.
	 * 
	 * @param request The request
	 * @param response The response
	 * @return The page size from the request, or from the cookie if the request does not have a
	 * valid value. Return -1 if both the request and the cookie do not have a valid value.
	 */
	public int getPageSize(HttpServletRequest request, HttpServletResponse response) {
		String value = request.getParameter(this.parameterName);
		int pageSize = getPageSize(value);

		if (pageSize <= 0) {
			/* Need to use the 'default' value in the cookie */
			pageSize = readPageSizeFromCookie(this.cookieName, request);
		} else {
			/* Need to write out this value as a new 'default' value in the cookie */
			Cookie cookie = new Cookie(this.cookieName, Integer.toString(pageSize));
			cookie.setMaxAge(Integer.MAX_VALUE);
			response.addCookie(cookie);
		}

		return pageSize;
	}

	/*
	 * Get the page size integer value given a string value for the page size
	 */
	private int getPageSize(String value) {
		int pageSize = -1;
		if (value == null)
			return pageSize;

		value = value.trim();
		if (!value.isEmpty()) {
			try {
				pageSize = Integer.parseInt(value);
				if (pageSize <= 0)
					pageSize = -1;
			} catch (Exception ignore) {
			}
		}

		return pageSize;
	}

	/*
	 * Read the page size value from the cookie
	 */
	private int readPageSizeFromCookie(String cookieName, HttpServletRequest request) {
		String value = null;
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookieName.equals(cookie.getName())) {
					value = cookie.getValue();
					break;
				}
			}
		}

		return getPageSize(value);
	}

	/**
	 * Get the page size parameter name
	 * 
	 * @return The parameter name
	 */
	public String getPageSizeParameterName() {
		return this.parameterName;
	}

	/**
	 * Get the cookie name used for this form
	 * 
	 * @return The cookie name for the form
	 */
	public String getCookieName() {
		return this.cookieName;
	}
}
