package controllers;

import org.sagebionetworks.bridge.exceptions.EntityNotFoundException;
import org.sagebionetworks.bridge.models.UserSession;
import org.sagebionetworks.bridge.models.UserSessionInfo;
import org.sagebionetworks.bridge.models.studies.Study;

import play.libs.Json;
import play.mvc.Result;

@org.springframework.stereotype.Controller
public class ApplicationController extends BaseController {

    private static final UserSession EMPTY_USER_SESSION = new UserSession();

    public Result redirectToApp() {
        return redirect("/app/");
    }

    public Result loadApp() throws Exception {
        UserSession session = getSessionIfItExists();
        if (session == null) {
            session = new UserSession();
        }
        UserSessionInfo info = new UserSessionInfo(session);
        Study study = studyService.getStudyByHostname(getHostname());
        return ok(views.html.index.render(Json.toJson(info).toString(), study.getName()));
    }

    public Result redirectToPublicApp() {
        return redirect("/");
    }

    public Result loadPublicApp() throws Exception {
        UserSessionInfo info = new UserSessionInfo(new UserSession());
        
        // There's probably a non-crappy way of doing this in Play, but I couldn't find it.
        Study study = studyService.getStudyByHostname(getHostname());
        if (study == null) {
            throw new EntityNotFoundException(Study.class, "Cannot determine study from the host name: " + getHostname());
        } else if ("pd".equals(study.getIdentifier()) || "neurod".equals(study.getIdentifier()) || "parkinson".equals(study.getIdentifier())) {
            return ok(views.html.neurod.render(Json.toJson(info).toString()));    
        } else if ("api".equals(study.getIdentifier())) {
            return ok(views.html.api.render(Json.toJson(info).toString()));
        }
        String apiHost = "api" + bridgeConfig.getStudyHostnamePostfix();
        
        return ok(views.html.nosite.render(study.getName(), apiHost));
    }
    
    public Result loadConsent(String sessionToken) throws Exception {
        UserSession session = null;
        if (sessionToken != null) {
            session = cacheProvider.getUserSession(sessionToken);    
        }
        if (session == null) {
            session = EMPTY_USER_SESSION;
        }
        UserSessionInfo info = new UserSessionInfo(session);
        return ok(views.html.consent.render(Json.toJson(info).toString()));
    }

    public Result redirectToConsent() {
        return redirect("/consent/");
    }
}

@@ -1200,295 +1200,204 @@
  // 
-There's probably a non-crappy way of doing this in Play, but I couldn't find it.%0A        Study study = studyService.getStudyByHostname(getHostname());%0A        if (study == null) %7B%0A            throw new EntityNotFoundException(Study.class, %22Cannot determine study from the h
+We need to default to a study or the integration tests in Play will fail %0A        // (localhost:3333 won't match anything).%0A        try %7B%0A            Study study = studyService.getStudyByH
 ost
- 
 name
-: %22 + 
+(
 getH
@@ -1412,30 +1412,27 @@
 ));%0A        
-%7D else
+   
  if (%22pd%22.eq
@@ -1553,32 +1553,36 @@
 ) %7B%0A            
+    
 return ok(views.
@@ -1648,133 +1648,58 @@
     
-%7D else if (%22api%22.equals(study.getIdentifier())) %7B%0A            return ok(views.html.api.render(Json.toJson(info).toString()));
+    %7D%0A        %7D catch(EntityNotFoundException e) %7B
 %0A   
@@ -1707,82 +1707,36 @@
     
-%7D%0A
     
-    String apiHost = %22api%22 + bridgeConfig.getStudyHostnamePostfix();
+// Go with the API study
 %0A   
@@ -1732,32 +1732,33 @@
 I study%0A        
+%7D
 %0A        return 
@@ -1775,22 +1775,19 @@
 tml.
-nosite
+api
 .render(
 stud
@@ -1786,32 +1786,36 @@
 der(
-study.getName(), apiHost
+Json.toJson(info).toString()
 ));%0A

Build fix: cannot have no study when using localhost