/**
 * TitleSearchAction.java
 * Created May 20, 2008
 * Copyright (c) TANDBERG Television 2007-2008
 */
package com.tandbergtv.watchpoint.pmm.web.title;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.tandbergtv.watchpoint.pmm.entities.Schedule;
import com.tandbergtv.watchpoint.pmm.schedule.ISchedulePersistenceService;
import com.tandbergtv.watchpoint.pmm.title.search.TitleSearchKey;
import com.tandbergtv.workflow.web.page.Field;
import com.tandbergtv.workflow.web.page.Page;
import com.tandbergtv.watchpoint.pmm.web.title.search.TitleSearchPageBuilder;
import com.tandbergtv.workflow.core.service.ServiceRegistry;

/**
 * This action is responsible for painting the search page
 * 
 * @author Sahil Verma
 * @author Vijay Silva
 */
public class TitleSearchAction extends DispatchAction {

	/* The Search Field name for the title specification */
	private static final String TITLE_SPEC_FIELD_NAME = TitleSearchKey.TITLE_SPEC.toString();

	/**
	 * Displays the title search page
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 */
	@Deprecated
	public ActionForward showSearchPage(ActionMapping mapping, ActionForm actionform,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		/*TitleSearchForm form = (TitleSearchForm) actionform;

		 Get the specification name to use for building the search page 
		String scheduleSpecification = null;
		Schedule schedule = this.getSchedule(form);
		if (schedule != null) {
			if (schedule.getTitles()!= null && schedule.getTitles().size() > 0) {
				scheduleSpecification = schedule.getTitles().iterator().next().getSpecification();
			}
		}

		 Determine the specification to use when building the search page 
		String specificationName = scheduleSpecification;
		if (specificationName == null)
			specificationName = this.getSpecificationName(form);

		 Need to build the Title Search Page based on the Title Search Type and Specification 
		TitleSearchPageBuilder builder = TitleSearchPageBuilder.newInstance();
		Page searchPage = builder.buildSearchPage(form.getSearchType(), specificationName);
		Field specificationField = searchPage.getFieldsMap().get(TITLE_SPEC_FIELD_NAME);

		 Remove the specification field from the search page 
		searchPage.getFields().remove(specificationField);
		searchPage.getFieldsMap().remove(TITLE_SPEC_FIELD_NAME);

		 If the specification is defined by the schedule, remove other specification choices 
		if (scheduleSpecification != null) {
			specificationField.getValuesMap().clear();
			specificationField.getValuesMap().put(specificationName, specificationName);
		}

		 Add specification field separately to the form 
		form.setSpecification(specificationName);
		form.setSpecificationField(specificationField);
		form.setSearchPage(searchPage);

		return mapping.findForward("search");*/
		return null;
	}

	/* Get the Schedule for which titles are being searched for */
	private Schedule getSchedule(TitleSearchForm form) throws Exception {
		String scheduleId = form.getScheduleId();

		/* Get the schedule, if possible */
		if (scheduleId == null || scheduleId.trim().length() == 0)
			return null;
		
		ServiceRegistry registry = ServiceRegistry.getDefault();
		ISchedulePersistenceService service = registry.lookup(ISchedulePersistenceService.class);
		return service.get(Long.parseLong(scheduleId));
	}

	/* Build the Search Page for title searches */
	private String getSpecificationName(TitleSearchForm form) throws Exception {
		/* Read the name from the form */
		String name = form.getSpecification();

		/* If name not present, check the default name in the basic search page */
		if (name == null || name.trim().length() == 0) {
			TitleSearchPageBuilder builder = TitleSearchPageBuilder.newInstance();
			Page searchPage = builder.buildDefaultSearchPage(form.getSearchType());
			if (searchPage.getFieldsMap() != null) {
				Field specificationField = searchPage.getFieldsMap().get(TITLE_SPEC_FIELD_NAME);
				Map<String, String> allNames = specificationField.getValuesMap();
				if (allNames != null && allNames.size() > 0) {
					name = allNames.entrySet().iterator().next().getKey();
				}
			}
		}

		/* Avoid null value for the specification name, since it causes problems with the form */
		if (name == null)
			name = "";

		return name;
	}
}
