package org.slc.sli.api.resources;

//
import java.util.HashMap;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

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.representation.Home;
import org.slc.sli.api.resources.util.ResourceUtil;
import org.slc.sli.domain.Entity;

import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * Jersey resource for home entity and associations.
 * 
 */
@Path("home")
@Component
@Scope("request")
@Produces({ Resource.JSON_MEDIA_TYPE, Resource.XML_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
    @Produces({ Resource.JSON_MEDIA_TYPE })
    public Response getHomeUri(@Context final UriInfo uriInfo) {
        
        // TODO: refactor common code from getHomeUri and GetHomeUriXML

        // create a final map of links to relevant links
        HashMap<String, Object> linksMap = new HashMap<String, Object>();

        // get the entity ID and EntityDefinition for user
        Pair<String, EntityDefinition> pair = this.getEntityInfoForUser();
        if (pair != null) {
            String userId = pair.getLeft();
            EntityDefinition defn = pair.getRight();
            
            // 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));
            
            //add the aggregate link
            links.addAll(ResourceUtil.getAggregateLink(uriInfo));    
            linksMap.put(ResourceUtil.LINKS, links);
        }
        
        // return as browser response
        return Response.ok(linksMap).build();
    }
    
    /**
     * Returns the initial information when a user logs in.
     * 
     */
    @GET
    @Produces({ Resource.XML_MEDIA_TYPE })
    public Response getHomeUriXML(@Context final UriInfo uriInfo) {
        
        // TODO: refactor common code from getHomeUri and GetHomeUriXML
        Home home = null; 

        // get the entity ID and EntityDefinition for user
        Pair<String, EntityDefinition> pair = this.getEntityInfoForUser();
        if (pair != null) {
            String userId = pair.getLeft();
            EntityDefinition defn = pair.getRight();
            
            // 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
            home = new Home(linksMap, defn.getStoredCollectionName());
        }
        return Response.ok(home).build();
    }
    
    /**
     * Analyzes security context to get ID and EntityDefinition for user.
     * 
     * @return ID and EntityDefinition from security context
     */
    private Pair<String, EntityDefinition> getEntityInfoForUser() {
        Pair<String, EntityDefinition> pair = null;
                
        // get the Entity for the logged in user
        Entity entity = ResourceUtil.getSLIPrincipalFromSecurityContext().getEntity();
        if (entity != null) {
            EntityDefinition entityDefinition = this.entityDefs.lookupByEntityType(entity.getType());        
            pair = Pair.of(entity.getEntityId(), entityDefinition);
        }
        
        return pair;
    }
}
@@ -981,20 +981,79 @@
 DIA_TYPE
+, Resource.SLC_XML_MEDIA_TYPE, Resource.SLC_JSON_MEDIA_TYPE
  %7D)%0A
-
 public c
@@ -1428,52 +1428,8 @@
 GET%0A
-    @Produces(%7B Resource.JSON_MEDIA_TYPE %7D)%0A
     
@@ -1573,1319 +1573,8 @@
 iXML
-%0A%0A        // create a final map of links to relevant links%0A        HashMap%3CString, Object%3E linksMap = new HashMap%3CString, Object%3E();%0A%0A        // get the entity ID and EntityDefinition for user%0A        Pair%3CString, EntityDefinition%3E pair = this.getEntityInfoForUser();%0A        if (pair != null) %7B%0A            String userId = pair.getLeft();%0A            EntityDefinition defn = pair.getRight();%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            //add the aggregate link%0A            links.addAll(ResourceUtil.getAggregateLink(uriInfo));    %0A            linksMap.put(ResourceUtil.LINKS, links);%0A        %7D%0A        %0A        // return as browser response%0A        return Response.ok(linksMap).build();%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   
@@ -2496,18 +2496,8 @@
 ome(
-linksMap, 
 defn
@@ -2522,16 +2522,26 @@
 onName()
+, linksMap
 );%0A     

update class ro produce XML as well. Remove duplicate getHomeUri function so we only have one function for both JSON and XML