package org.slc.sli.api.resources.v1.view.impl;

import org.slc.sli.api.config.ResourceNames;
import org.slc.sli.api.representation.EntityBody;
import org.slc.sli.api.resources.v1.ParameterConstants;
import org.slc.sli.api.resources.v1.PathConstants;
import org.slc.sli.api.resources.v1.view.OptionalFieldAppender;
import org.slc.sli.api.resources.v1.view.OptionalFieldAppenderHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * Provides data about students and gradebook entries to construct the custom
 * views returned by the api
 * @author srupasinghe
 */
@Component
public class StudentGradebookOptionalFieldAppender implements OptionalFieldAppender {

    @Autowired
    private OptionalFieldAppenderHelper optionalFieldAppenderHelper;


    @Override
    public List<EntityBody> applyOptionalField(List<EntityBody> entities) {

        //get the section Ids
        List<String> sectionIds = new ArrayList<String>(optionalFieldAppenderHelper.getSectionIds(entities));

        //get the list of student section grade book entries for all sections
        List<EntityBody> studentSectionGradebookEntries = optionalFieldAppenderHelper.queryEntities(ResourceNames.STUDENT_SECTION_GRADEBOOK_ENTRIES,
                ParameterConstants.SECTION_ID, sectionIds);

        //get all gradebook entry ids
        List<String> gradebookEntryIds = optionalFieldAppenderHelper.getIdList(studentSectionGradebookEntries, ParameterConstants.GRADEBOOK_ENTRY_ID);

        //get all gradebook entries for the sections
        List<EntityBody> gradebookEntries = optionalFieldAppenderHelper.queryEntities(ResourceNames.GRADEBOOK_ENTRIES,
                "_id", gradebookEntryIds);

        for (EntityBody student : entities) {
            //get the student gradebook entries for the given student
            List<EntityBody> studentSectionGradebookEntriesForStudent = optionalFieldAppenderHelper.getEntitySubList(studentSectionGradebookEntries,
                    ParameterConstants.STUDENT_ID, (String) student.get("id"));

            for (EntityBody studentSectionGradebookEntry : studentSectionGradebookEntriesForStudent) {
                //get the gradebook entry for the student gradebook entry
                EntityBody gradebookEntry = optionalFieldAppenderHelper.getEntityFromList(gradebookEntries, "id",
                        (String) studentSectionGradebookEntry.get(ParameterConstants.GRADEBOOK_ENTRY_ID));

                if (gradebookEntry != null) {
                    studentSectionGradebookEntry.put(PathConstants.GRADEBOOK_ENTRIES, gradebookEntry);
                }
            }

            //add the body to the student
            student.put(PathConstants.STUDENT_SECTION_GRADEBOOK_ENTRIES, studentSectionGradebookEntriesForStudent);
        }

        return entities;
    }

}

@@ -1194,32 +1194,114 @@
 List%3CEntityBody%3E
+ studentSectionGradebookEntries;%0A        if (sectionIds.size() != 0) %7B%0A           
  studentSectionG
@@ -1417,32 +1417,36 @@
                 
+    
 ParameterConstan
@@ -1468,24 +1468,367 @@
 sectionIds);
+%0A        %7D else %7B%0A            List%3CString%3E studentIds = new ArrayList%3CString%3E(optionalFieldAppenderHelper.getIdList(entities, %22id%22));%0A            studentSectionGradebookEntries = optionalFieldAppenderHelper.queryEntities(ResourceNames.STUDENT_SECTION_GRADEBOOK_ENTRIES,%0A                    ParameterConstants.STUDENT_ID, studentIds);%0A        %7D
 %0A%0A        //

Update gradebook appender to handle students view.