package gov.nih.nci.cabig.caaers.web.ae;

import java.util.ArrayList;

import gov.nih.nci.cabig.caaers.domain.CtcCategory;
import gov.nih.nci.cabig.caaers.web.ae.CreateRoutineAdverseEventCommand;
import gov.nih.nci.cabig.ctms.web.tabs.Flow;
import gov.nih.nci.cabig.ctms.web.tabs.FlowFactory;

import javax.servlet.http.HttpServletRequest;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;

/**
 * @author Krikor Krumlian
 */
public class CreateRoutineAdverseEventController extends AbstractRoutineAdverseEventInputController {

	public CreateRoutineAdverseEventController() {
        super();
        setBindOnNewForm(true);
        setCommandClass(CreateRoutineAdverseEventCommand.class);
    }

    @Override
    protected FlowFactory<RoutineAdverseEventInputCommand> createFlowFactory() {
        return new RoutineFlowFactory("Document routine AEs") {
        	
        	private String instructions = "In order to document Routine AEs, you need to first select a participant and a\n" +
            "study. You may start with either one. Once you have selected one, the options\n" +
            "for the other will be automatically constrained.";
        	
            @Override
            protected void addPreBasicTabs(Flow<RoutineAdverseEventInputCommand> flow) {
                flow.addTab(new BeginTab<RoutineAdverseEventInputCommand>(instructions));
            }
        };
    }

    @Override
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
    	CreateRoutineAdverseEventCommand caec = new CreateRoutineAdverseEventCommand(assignmentDao, routineReportDao, reportDao, nowFactory);
        return caec;
    }
    
    protected void onBindAndValidate(HttpServletRequest request, Object command,BindException errors,int page)throws Exception {

    	// populate categories
    	// TODO See if you do the below with binding , the problem is the items are not being checked when go back
    	CreateRoutineAdverseEventCommand adverseEventCommand = (CreateRoutineAdverseEventCommand)command;

    	if (adverseEventCommand.getCtcCatIds() != null && page ==1 ) {
    		adverseEventCommand.setCategories(new ArrayList<CtcCategory>());
			for (String st : adverseEventCommand.getCtcCatIds()) {
				CtcCategory ctcCategory = ctcCategoryDao.getById(Integer.parseInt(st));
				adverseEventCommand.getCategories().add(ctcCategory);
			}
    	}

    }

    @Override
    protected RoutineAdverseEventInputCommand save(RoutineAdverseEventInputCommand command, Errors errors) {
        command.save();
        return null;
    }
}

@@ -39,37 +39,8 @@
 e;%0A%0A
-import java.util.ArrayList;%0A%0A
 impo
@@ -117,53 +117,25 @@
 ig.c
-aaer
+tm
 s.web.
-ae.CreateRoutineAdverseEventCommand
+tabs.Flow
 ;%0Aim
@@ -179,60 +179,44 @@
 Flow
-;%0Aimport gov.nih.nci.cabig.ctms.web.tabs.FlowFactory
+Factory;%0A%0Aimport java.util.ArrayList
 ;%0A%0Ai
@@ -1600,16 +1600,35 @@
 wFactory
+, evaluationService
 );%0A     

Fix for ManualSelect not showing for AE's created as expedited from routine flow