/*
 * Created on Aug 11, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package gov.va.med.esr.ui.ee.action;

import java.util.HashMap;
import java.util.Map;


import gov.va.med.esr.common.model.person.Person;
import javax.servlet.http.*;

import org.apache.struts.action.*;
//framework

//ui
import gov.va.med.esr.ui.conversion.UIConversionService;

/**
 * @author DNS   RAMACP
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class EligibilityEditReviewAction extends EligibilityAction {
	
	public ActionForward display(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception 
    {
          
	   	
	   	EligibilityEditReviewActionForm eligibilityForm = (form instanceof EligibilityEditReviewActionForm) ? (EligibilityEditReviewActionForm)form : null;
	   	
	   	if (eligibilityForm != null) {
		   	Person person = getSelectedPerson( request);
		   	eligibilityForm.setPerson( person );
		   	
		   	Person updatedPerson = getUpdatedPerson( request);
		   	eligibilityForm.setProposedPerson( updatedPerson );
		   	
		   	//Make it quick without config for now.
		   	UIConversionService uiConversionService = (UIConversionService)conversionService;
		   	uiConversionService.convert(person,eligibilityForm);
	   	}
	   	return (mapping.findForward("success"));
    }
	
	//Back to edit screen and keep changes
	public ActionForward revise(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
			throws Exception 
	{		
        return mapping.findForward("edit");
    }
	
	//Back to edit screen and discard changes, start over
	public ActionForward discardChanges(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
			throws Exception 
	{
		setUpdatedPerson(request, null);
        return mapping.findForward("discardChanges");
    }
	
	public ActionForward submit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws Exception 
	{

	   	// Get data from session to pass to a service for processing the submit
		
		//TODO change this to get updated person from the request (which will be set by the edit page)
		Person person = getUpdatedPerson(request);
		if (getEnrollmentService().isServicePeriodNeeded(person))
			return mapping.findForward("servicePeriod");

		// Call service to store
        Person updatedPerson = getEnrollmentService().updateEligibilityData(person);
		if(updatedPerson != null)
		{
			setUpdatedPerson(request, null);
			updateSelectedPerson(request, updatedPerson);
		}
		return mapping.findForward("view");
	}
	
	public ActionForward cancel(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
			throws Exception 
	{
		setUpdatedPerson(request, null);

        return mapping.findForward("view");
    }
	/**
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 */
	public void afterPropertiesSet() {
       
//		if( getEnrollmentService() == null || getFacilityService() == null) {
//           throw new ServiceConfigurationException( "Missing required enrollmentCalculateService/facilityService");
//		}
	}  	
	/**
     * handle one form have multiple submit button.
     * 
     * @see org.apache.struts.actions.LookupDispatchAction#getKeyMethodMap()
     * @return
     */
    protected Map getKeyMethodMap() {
        Map map = new HashMap();
        map.put("button.revise", "revise");
        map.put("button.submit", "submit");
        map.put("button.cancel", "cancel");
        map.put("button.discardChanges", "discardChanges");
		map.put("button.reviewImpact","display");
       return map;
    }
}