package edu.duke.cabig.c3pr.web.registration;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

import edu.duke.cabig.c3pr.domain.Epoch;
import edu.duke.cabig.c3pr.domain.ScheduledEpoch;
import edu.duke.cabig.c3pr.domain.StudySubject;
import edu.duke.cabig.c3pr.utils.web.ControllerTools;
import edu.duke.cabig.c3pr.web.registration.tabs.AssignArmTab;
import edu.duke.cabig.c3pr.web.registration.tabs.EligibilityCriteriaTab;
import edu.duke.cabig.c3pr.web.registration.tabs.EnrollmentDetailsTab;
import edu.duke.cabig.c3pr.web.registration.tabs.ReviewSubmitTab;
import edu.duke.cabig.c3pr.web.registration.tabs.SearchStudySubjectTab;
import edu.duke.cabig.c3pr.web.registration.tabs.StratificationTab;
import edu.duke.cabig.c3pr.web.study.StudyWrapper;
import gov.nih.nci.cabig.ctms.web.tabs.Flow;

/**
 * @author Ramakrishna
 * 
 */

public class TransferEpochRegistrationController<C extends StudySubjectWrapper> extends RegistrationController<C> {
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(TransferEpochRegistrationController.class);

	public TransferEpochRegistrationController() {
        super("Change Epoch");
    }

    @Override
    protected void intializeFlows(Flow flow) {
        flow.addTab(new EligibilityCriteriaTab());
        flow.addTab(new StratificationTab());
        flow.addTab(new AssignArmTab());
        flow.addTab(new ReviewSubmitTab());
        setFlow(flow);
    }
    
    @Override
    protected Object formBackingObject(HttpServletRequest request)
    		throws Exception {
    	// TODO Auto-generated method stub
    	StudySubjectWrapper wrapper= (StudySubjectWrapper)super.formBackingObject(request);
    	ScheduledEpoch scheduledEpoch;
    	if(WebUtils.hasSubmitParameter(request, "epoch")){
	        Integer id = Integer.parseInt(request.getParameter("epoch"));
	        Epoch epoch = epochDao.getById(id);
	        epochDao.initialize(epoch);
	        if (epoch.getTreatmentIndicator()) {
	            (epoch).getArms().size();
	            scheduledEpoch = new ScheduledEpoch();
	        }
	        else {
	            scheduledEpoch = new ScheduledEpoch();
	        }
	        scheduledEpoch.setEpoch(epoch);
	        wrapper.getStudySubject().addScheduledEpoch(scheduledEpoch);
	        registrationControllerUtils.buildCommandObject(wrapper.getStudySubject());
    	}
    	return wrapper;
    }

    @Override
    protected ModelAndView processFinish(HttpServletRequest request, HttpServletResponse response,
                    Object command, BindException errors) throws Exception {
    	StudySubjectWrapper wrapper = (StudySubjectWrapper) command;
        StudySubject studySubject = wrapper.getStudySubject();
        if(wrapper.getShouldTransfer() && !wrapper.getShouldRandomize())
        	studySubject = studySubjectRepository.transferSubject(studySubject.getIdentifiers());
        else if(wrapper.getShouldEnroll() && !wrapper.getShouldRandomize()){
        	studySubject=studySubjectRepository.enroll(studySubject.getIdentifiers());
        }else if(wrapper.getShouldRegister() ||(wrapper.getShouldEnroll() && wrapper.getShouldRandomize()) ){
        	studySubject=studySubjectRepository.register(studySubject.getIdentifiers());
        }else if(wrapper.getShouldReserve()){
        	studySubject=studySubjectRepository.reserve(studySubject.getIdentifiers());
        }else{
            studySubject=studySubjectRepository.save(studySubject);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("processFinish(HttpServletRequest, HttpServletResponse, Object, BindException) - registration service call over"); //$NON-NLS-1$
        }
        if(WebUtils.hasSubmitParameter(request, "decorator") && "noheaderDecorator".equals(request.getParameter("decorator"))){
        	 return new ModelAndView("redirect:confirm?" + ControllerTools.createParameterString(studySubject.getSystemAssignedIdentifiers().get(0))+"&decorator=" + request.getParameter("decorator"));
        }else{
        	return new ModelAndView("redirect:confirm?" + ControllerTools.createParameterString(studySubject.getSystemAssignedIdentifiers().get(0)));	
        }
    }
}
@@ -44,80 +44,8 @@
 n;%0A%0A
-import java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0A
 impo
@@ -223,54 +223,8 @@
 on;%0A
-import org.springframework.validation.Errors;%0A
 impo
@@ -701,285 +701,91 @@
 abs.
-EnrollmentDetailsTab;%0Aimport edu.duke.cabig.c3pr.web.registration.tabs.ReviewSubmitTab;%0Aimport edu.duke.cabig.c3pr.web.registration.tabs.SearchStudySubjectTab;%0Aimport edu.duke.cabig.c3pr.web.registration.tabs.StratificationTab;%0Aimport edu.duke.cabig.c3pr.web.study.StudyWrapper
+ReviewSubmitTab;%0Aimport edu.duke.cabig.c3pr.web.registration.tabs.StratificationTab
 ;%0Aim

clean up