package org.slc.sli.api.resources;

//
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

//
import org.slc.sli.api.config.EntityDefinition;
import org.slc.sli.api.config.EntityDefinitionStore;
import org.slc.sli.api.representation.EmbeddedLink;
import org.slc.sli.api.resources.util.ResourceUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

/**
 * Jersey resource for home entity and associations.
 * 
 */
@Path("home")
@Component
@Scope("request")
@Produces({ Resource.JSON_MEDIA_TYPE })
public class HomeResource {
    
    private static final Logger LOG = 
            LoggerFactory.getLogger(HomeResource.class);
    
    final EntityDefinitionStore entityDefs;
    
    @Autowired
    HomeResource(EntityDefinitionStore entityDefs) {
        this.entityDefs = entityDefs;
    }
    
    /**
     * Returns the initial information when a user logs in.
     * 
     */
    @GET
    public Response getHomeUri(@Context final UriInfo uriInfo) {
        
        // use login data to resolve what type of user and ID of user
        Map<String, String> map = this.resolveUserDataFromSecurityContext();
        
        // remove user's type from map
        // TODO: if userType and id are still being used after  
        //       authentication updates, then use static strings in code
        String userType = map.remove("userType");
        String userId = map.remove("id");
        
        //look up known associations to user's type
        EntityDefinition defn = this.entityDefs.lookupByResourceName(userType);
        
        //prepare a list of links with the self link
        List<EmbeddedLink> links = ResourceUtil.getSelfLink(uriInfo, userId, defn);
        
        //add links for all of the entity's associations for this ID
        links.addAll(ResourceUtil.getAssociationsLinks(this.entityDefs, defn, userId, uriInfo));
        
        //create a final map of links to relevant links
        HashMap<String, Object> linksMap = new HashMap<String, Object>();
        linksMap.put(ResourceUtil.LINKS, links);
        
        //return as browser response
        return Response.ok(linksMap).build();
    }
    
   
    /**
     * Analyzes security context to determine exposure type and ID of user.
     * 
     * @return deduced ID from available security context
     */
    // call with student id
    private Map<String, String> resolveUserDataFromSecurityContext() {
        // map user fields to values
        Map<String, String> map = new HashMap<String, String>();
        
        // lookup security/login information
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        
        // TODO: extract userType and ID (in some way) from authentication value
        map.put("userType", "students");
        map.put("id", "714c1304-8a04-4e23-b043-4ad80eb60992");
        //map.put("userType", "teachers");
        //map.put("id", "fa45033c-5517-b14b-1d39-c9442ba95782");
        
        return map;
    }
}
@@ -386,16 +386,60 @@
 nStore;%0A
+import org.slc.sli.api.representation.Home;%0A
 import o
@@ -482,16 +482,16 @@
 edLink;%0A
-
 import o
@@ -1097,16 +1097,41 @@
 DIA_TYPE
+, Resource.XML_MEDIA_TYPE
  %7D)%0Apubl
@@ -1519,16 +1519,60 @@
    @GET%0A
+    @Produces(%7B Resource.JSON_MEDIA_TYPE %7D)%0A
     publ
@@ -1627,16 +1627,89 @@
 iInfo) %7B
+%0A%0A        // TODO: refactor common code from getHomeUri and GetHomeUriXML
 %0A       
@@ -2883,21 +2883,1541 @@
 ;%0A    %7D%0A
-
     %0A
+    /**%0A     * Returns the initial information when a user logs in.%0A     * %0A     */%0A    @GET%0A    @Produces(%7B Resource.XML_MEDIA_TYPE %7D)%0A    public Response getHomeUriXML(@Context final UriInfo uriInfo) %7B%0A%0A        // TODO: refactor common code from getHomeUri and GetHomeUriXML%0A%0A        // use login data to resolve what type of user and ID of user%0A        Map%3CString, String%3E map = this.resolveUserDataFromSecurityContext();%0A        %0A        // remove user's type from map%0A        // TODO: if userType and id are still being used after  %0A        //       authentication updates, then use static strings in code%0A        String userType = map.remove(%22userType%22);%0A        String userId = map.remove(%22id%22);%0A        %0A        //look up known associations to user's type%0A        EntityDefinition defn = this.entityDefs.lookupByResourceName(userType);%0A        %0A        //prepare a list of links with the self link%0A        List%3CEmbeddedLink%3E links = ResourceUtil.getSelfLink(uriInfo, userId, defn);%0A        %0A        //add links for all of the entity's associations for this ID%0A        links.addAll(ResourceUtil.getAssociationsLinks(this.entityDefs, defn, userId, uriInfo));%0A        %0A        //create a final map of links to relevant links%0A        HashMap%3CString, Object%3E linksMap = new HashMap%3CString, Object%3E();%0A        linksMap.put(ResourceUtil.LINKS, links);%0A        %0A        //return as browser response%0A        Home home = new Home (linksMap, defn.getStoredCollectionName());%0A        return Response.ok(home).build();%0A    %7D%0A
    %0A    

Add Get function to return XML