package edu.northwestern.bioinformatics.studycalendar.web.template;

import edu.northwestern.bioinformatics.studycalendar.dao.ArmDao;
import edu.northwestern.bioinformatics.studycalendar.domain.Arm;
import edu.northwestern.bioinformatics.studycalendar.domain.Duration;
import edu.northwestern.bioinformatics.studycalendar.domain.Study;
import edu.northwestern.bioinformatics.studycalendar.web.ControllerTools;
import edu.northwestern.bioinformatics.studycalendar.web.PscSimpleFormController;
import edu.northwestern.bioinformatics.studycalendar.utils.accesscontrol.AccessControl;
import edu.northwestern.bioinformatics.studycalendar.utils.accesscontrol.StudyCalendarProtectionGroup;
import edu.northwestern.bioinformatics.studycalendar.utils.breadcrumbs.DefaultCrumb;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.beans.factory.annotation.Required;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.HashMap;

/**
 * @author Moses Hohman
 * @author Rhett Sutphin
 */
@AccessControl(protectionGroups = StudyCalendarProtectionGroup.STUDY_COORDINATOR)
public class NewPeriodController extends PscSimpleFormController {
    private ArmDao armDao;

    public NewPeriodController() {
        setCommandClass(NewPeriodCommand.class);
        setFormView("editPeriod");
        setCrumb(new DefaultCrumb("New Period"));
    }

    @Override
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
        ControllerTools.registerDomainObjectEditor(binder, "arm", armDao);
    }

    @Override
    protected Map referenceData(HttpServletRequest request) throws Exception {
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("arm", armDao.getById(ServletRequestUtils.getIntParameter(request, "arm")));
        data.put("durationUnits", Duration.Unit.values());
        return data;
    }

    @Override
    protected ModelAndView onSubmit(Object command) throws Exception {
        return onSubmit((NewPeriodCommand) command);
    }

    private ModelAndView onSubmit(NewPeriodCommand command) throws Exception {
        command.apply();
        Study study = command.getArm().getEpoch().getPlannedCalendar().getStudy();
        return ControllerTools.redirectToCalendarTemplate(study.getId(), command.getArm().getId());
    }

    @Required
    public void setArmDao(ArmDao armDao) {
        this.armDao = armDao;
    }
}

@@ -761,16 +761,106 @@
 tCrumb;%0A
+import edu.northwestern.bioinformatics.studycalendar.utils.breadcrumbs.BreadcrumbContext;%0A
 import o
@@ -904,16 +904,16 @@
 ndView;%0A
-
 import o
@@ -1244,16 +1244,46 @@
 HashMap;
+%0Aimport java.util.Collections;
 %0A%0A/**%0A *
@@ -1654,33 +1654,14 @@
 new 
-DefaultCrumb(%22New Period%22
+Crumb(
 ));%0A
@@ -2705,24 +2705,24 @@
 o armDao) %7B%0A
-
         this
@@ -2744,11 +2744,318 @@
 o;%0A    %7D
+%0A%0A    private static class Crumb extends DefaultCrumb %7B%0A        public Crumb() %7B%0A            super(%22Add period%22);%0A        %7D%0A%0A        public Map%3CString, String%3E getParameters(BreadcrumbContext context) %7B%0A            return Collections.singletonMap(%22arm%22, context.getArm().getId().toString());%0A        %7D%0A    %7D
 %0A%7D%0A

Correct crumb