package org.sagebionetworks.bridge.play.controllers;

import java.util.Collections;
import java.util.List;

import org.sagebionetworks.bridge.json.BridgeObjectMapper;
import org.sagebionetworks.bridge.models.ClientInfo;
import org.sagebionetworks.bridge.models.ResourceList;
import org.sagebionetworks.bridge.models.accounts.UserSession;
import org.sagebionetworks.bridge.models.schedules.Schedule;
import org.sagebionetworks.bridge.models.schedules.SchedulePlan;
import org.sagebionetworks.bridge.models.schedules.ScheduleType;
import org.sagebionetworks.bridge.models.studies.StudyIdentifier;
import org.sagebionetworks.bridge.services.SchedulePlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import play.mvc.Result;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Lists;

@Controller
public class ScheduleController extends BaseController {

    private SchedulePlanService schedulePlanService;
    
    @Autowired
    public void setSchedulePlanService(SchedulePlanService schedulePlanService) {
        this.schedulePlanService = schedulePlanService;
    }
    
    @Deprecated
    public Result getSchedulesV1() throws Exception {
        getAuthenticatedAndConsentedSession();
        return okResult(Collections.emptyList());
    }
    
    @Deprecated
    public Result getSchedulesV3() throws Exception {
        UserSession session = getAuthenticatedAndConsentedSession();
        StudyIdentifier studyId = session.getStudyIdentifier();
        ClientInfo clientInfo = getClientInfoFromUserAgentHeader();
        
        List<SchedulePlan> plans = schedulePlanService.getSchedulePlans(clientInfo, studyId);
        List<Schedule> schedules = Lists.newArrayListWithCapacity(plans.size());
        for (SchedulePlan plan : plans) {
            Schedule schedule = plan.getStrategy().getScheduleForUser(session.getStudyIdentifier(), plan, session.getUser());
            schedules.add(schedule);
        }
        
        JsonNode node = BridgeObjectMapper.get().valueToTree(new ResourceList<Schedule>(schedules));
        ArrayNode items = (ArrayNode)node.get("items");
        for (int i=0; i < items.size(); i++) {
            // If it's marked persistent it's also a once-time schedule. Hack this to make it a recurring schedule.
            if (schedules.get(i).getPersistent()) {
                ((ObjectNode)items.get(i)).put("scheduleType", ScheduleType.RECURRING.name().toLowerCase());
            }
        }
        return ok(node);
    }
    
    public Result getSchedules() throws Exception {
        UserSession session = getAuthenticatedAndConsentedSession();
        StudyIdentifier studyId = session.getStudyIdentifier();
        ClientInfo clientInfo = getClientInfoFromUserAgentHeader();
        
        List<SchedulePlan> plans = schedulePlanService.getSchedulePlans(clientInfo, studyId);
        List<Schedule> schedules = Lists.newArrayListWithCapacity(plans.size());
        for (SchedulePlan plan : plans) {
            Schedule schedule = plan.getStrategy().getScheduleForUser(session.getStudyIdentifier(), plan, session.getUser());
            schedules.add(schedule);
        }
        return okResult(schedules);
    }
}

@@ -2364,75 +2364,42 @@
  If 
-it's marked persistent it's also a once-time schedule. Hack this to
+the schedule has this cron string,
  mak
@@ -2414,16 +2414,30 @@
 ecurring
+, %22persistent%22
  schedul
@@ -2437,17 +2437,16 @@
 schedule
-.
 %0A       
@@ -2453,16 +2453,42 @@
      if 
+(%220 0 12 1/1 * ? *%22.equals
 (schedul
@@ -2504,19 +2504,21 @@
 .get
-Persistent(
+CronTrigger()
 )) %7B
@@ -2623,24 +2623,92 @@
 werCase());%0A
+                ((ObjectNode)items.get(i)).put(%22persistent%22, true);%0A
             

Change to the emergency fix