/**
 * Licensed under the MICE s.r.l.
 * End User License Agreement
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.micegroup.it/voila/license.html
 */
package it.mice.voila.runtime.springmvc.form;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.multipart.MultipartFile;

import it.mice.voila.runtime.springmvc.bean.PageStatus;
import it.mice.voila.runtime.springmvc.bean.TabInfoBean;

/**
 * <b>Descrizione:</b><BR>
 * Rappresenta la classe base delle classi Form.
 */
public abstract class JfFormBean implements Serializable {

	private static final int EXPORT_DEFAULT_MAX_ROWS = 65535;
	/**
	 */
	private static final long serialVersionUID = -123844818389396694L;
	/**
	 * Commons Logging instance.
	 */
	protected static org.apache.commons.logging.Log log = LogFactory
			.getLog(JfFormBean.class);

	private static final int SEARCH_RESULT_PAGE_SIZE = 30;
	private static final int POPUP_PAGE_SIZE = 15;
	private static final int SEARCH_RESULT_DEFAULT_PAGE_SIZE = 6000;
	private String expandSearchFrame = "no";
	// Stato della pagina
	// "READ" = sola lettura;
	// "EDIT" = in modifica;
	private PageStatus pageStatus;
	private String listViewMode = "list";
	private String listViewModeFor = null;
	private String listViewModeForSuffix = "";
	private Map<String, Object> criteriaMap = new HashMap<String, Object>();
	private String[] popupReturnParameters = new String[] {};
	private String[] popupNoClearFilterParameters = new String[] {};
	private boolean popupJscript = false;

    private int tabIndex = 0;

	private String fullTextSearchFilter = null;

	private MultipartFile iconImage;
	private boolean iconImageDeleteFlag;
	private MultipartFile bannerImage;
	private boolean bannerImageDeleteFlag;
	private MultipartFile attachment;

	public String getListViewMode() {
		return listViewMode;
	}

	public void setListViewMode(String listMode) {
		this.listViewMode = listMode;
	}

	public String getListViewModeFor() {
		return listViewModeFor;
	}

	public String getListViewModeForSuffix() {
		return listViewModeForSuffix;
	}

	public String getFullTextSearchFilter() {
		return fullTextSearchFilter;
	}

	public void setFullTextSearchFilter(String fullTextSearchFilter) {
		this.fullTextSearchFilter = fullTextSearchFilter;
	}

	public void setListViewModeFor(String listViewModeFor) {
		String[] parts = listViewModeFor.split(":");
		this.listViewModeFor = parts[0];
		if (parts.length > 1) {
			this.listViewModeForSuffix = parts[1];
		} else {
			this.listViewModeForSuffix = "";
		}
	}

	public String getListViewModeForList() {
		return getListViewModeFor() + "List";
	}

	public String getListViewModeForObjectKey() {
		return getListViewModeFor() + getListViewModeForSuffix() + "ObjectKey";
	}

	public boolean isListMode() {
		return "list".equals(getListViewMode());
	}

	public boolean isCalendarMode() {
		return "calendar".equals(getListViewMode());
	}

	public boolean isMapMode() {
		return "map".equals(getListViewMode());
	}

	public boolean isKanbanMode() {
		return "kanban".equals(getListViewMode());
	}

	public boolean isChartMode() {
		return "chart".equals(getListViewMode());
	}

	public MultipartFile getIconImage() {
		return iconImage;
	}

	public void setIconImage(MultipartFile iconImage) {
		this.iconImage = iconImage;
	}

	public MultipartFile getBannerImage() {
		return bannerImage;
	}

	public void setBannerImage(MultipartFile bannerImage) {
		this.bannerImage = bannerImage;
	}

	public boolean isIconImageDeleteFlag() {
		return iconImageDeleteFlag;
	}

	public void setIconImageDeleteFlag(boolean iconImageDeleteFlag) {
		this.iconImageDeleteFlag = iconImageDeleteFlag;
	}

	public boolean isBannerImageDeleteFlag() {
		return bannerImageDeleteFlag;
	}

	public void setBannerImageDeleteFlag(boolean bannerImageDeleteFlag) {
		this.bannerImageDeleteFlag = bannerImageDeleteFlag;
	}

	public MultipartFile getAttachment() {
		return attachment;
	}

	public void setAttachment(MultipartFile attachment) {
		this.attachment = attachment;
	}

	public String getExpandSearchFrame() {
		return expandSearchFrame;
	}

	public void setExpandSearchFrame(String flag) {
		expandSearchFrame = flag;
	}

	public int getSearchResultPageSize() {
		if (isListMode()) {
			return getSearchResultPageSizePaged();
		}
		log.warn("Default list row limit is " + SEARCH_RESULT_DEFAULT_PAGE_SIZE + ". To change this please override the method getSearchResultPageSize() in your form.");
		return getSearchResultPageSizeNotPaged();
	}

	protected int getSearchResultPageSizePaged() {
		return SEARCH_RESULT_PAGE_SIZE;
	}

	protected int getSearchResultPageSizeNotPaged() {
		return SEARCH_RESULT_DEFAULT_PAGE_SIZE;
	}

	public int getPopupPageSize() {
		return POPUP_PAGE_SIZE;
	}

	public String[] getPopupReturnParametersArray() {
		return popupReturnParameters;
	}

	public void setPopupReturnParameters(String popupReturnParameters) {
		if (popupReturnParameters != null) {
			this.popupReturnParameters = popupReturnParameters.split("\\,");
		}
	}

	public String[] getPopupNoClearFilterParametersArray() {
		return popupNoClearFilterParameters;
	}

	public void setPopupNoClearFilterParameters(
			String popupNoClearFilterParameters) {
		if (popupNoClearFilterParameters != null) {
			this.popupNoClearFilterParameters = popupNoClearFilterParameters
					.split("\\,");
		}
	}

	public boolean isPopupJscript() {
		return popupJscript;
	}

	public void setPopupJscript(boolean popupJscript) {
		this.popupJscript = popupJscript;
	}

	/**
	 * Returns the pageStatus.
	 *
	 * @return String
	 */
	public PageStatus getPageStatus() {
		return pageStatus;
	}

	/**
	 * Sets the pageStatus.
	 *
	 * @param pageStatus
	 *            The pageStatus to set
	 */
	public void setPageStatus(PageStatus pageStatus) {
		this.pageStatus = pageStatus;
	}

	public Map<String, Object> getCriteriaMap() {
		return criteriaMap;
	}

	public void setCriteriaMap(Map<String, Object> criteriaMap) {
		this.criteriaMap = criteriaMap;
	}

	public Byte getCriteriaValue(String propertyKey) {
		return (Byte) getCriteriaMap().get(propertyKey);
	}

	public void setCriteriaValue(String propertyKey, Byte value) {
		getCriteriaMap().put(propertyKey, value);
	}

	public String getOrderByValue(String propertyKey) {
		return (String) getCriteriaMap().get(propertyKey);
	}

	public void setOrderByValue(String propertyKey, String value) {
		getCriteriaMap().put(propertyKey, value);
	}

	public int getTabIndex() {
		return tabIndex;
	}

	public void setTabIndex(int tabIndex) {
		this.tabIndex = tabIndex;
	}

	/**
	 * Restituisce l'elenco dei TabInfoBean caricati.
	 * @return l'elenco dei TabInfoBean caricati.
	 */
	public Collection<TabInfoBean> getTabInfoBeanList() {
		return null;
	}

	public Collection<TabInfoBean> getVisibleTabInfoBeanList() {
		Collection<TabInfoBean> result = new ArrayList<TabInfoBean>();
		Iterator<TabInfoBean> i = getTabInfoBeanList().iterator();
		while (i.hasNext()) {
			TabInfoBean tabInfoBean = i.next();
			if (!hideTab(tabInfoBean)) {
				result.add(tabInfoBean);
			}
		}
		return result;
	}

	protected boolean hideTab(TabInfoBean tabInfoBean) {
		return tabInfoBean.hideFor(getPageStatus());
	}

	public String getCurrentTabName() {
		List<TabInfoBean> list = (List<TabInfoBean>)getVisibleTabInfoBeanList();
		return list.get(getTabIndex()).getTabName();
	}

	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

	public boolean isLastWizardPage() {
		List<TabInfoBean> list = (List<TabInfoBean>)getVisibleTabInfoBeanList();
		if (list.size() == 0) {
			return true;
		}
		return getTabIndex() == list.size();
	}

	public boolean isFirstWizardPage() {
		List<TabInfoBean> list = (List<TabInfoBean>)getVisibleTabInfoBeanList();
		if (list.size() == 0) {
			return true;
		}
		return getTabIndex() == 0;
	}

	public boolean getHasNextWizardPage() {
		return !isLastWizardPage();
	}

	public boolean getHasPrevWizardPage() {
		return !isFirstWizardPage();
	}

	public void nextPage() {
		if (!isLastWizardPage()) {
			setTabIndex(getTabIndex() + 1);
		}
	}

	public void prevPage() {
		if (!isFirstWizardPage()) {
			setTabIndex(getTabIndex() - 1);
		}
	}

	/**
	 * Return the max list rows to export. Default to 65535 rows.
	 * Please override to best fit your specific needs.
	 * @return max list rows to export.
	 */
	public int getListExportMaxRows() {
		return EXPORT_DEFAULT_MAX_ROWS;
	}

	public boolean isFullTextSearchRequested() {
		return !StringUtils.isEmpty(getFullTextSearchFilter());
	}

	/**
	 * Does this form support full text search? By default no forms support them.
	 * @return false, always, please override this method to return true and signal support for full text search.
	 */
	public boolean isSupportFullTextSearch() {
		return false;
	}
}
