package org.slc.sli.ingestion.transformation;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.codehaus.jackson.map.ObjectMapper;
import org.milyn.Smooks;
import org.milyn.payload.JavaResult;
import org.milyn.payload.StringSource;
import org.springframework.stereotype.Component;

import org.slc.sli.ingestion.NeutralRecord;
import org.slc.sli.ingestion.validation.ErrorReport;

/**
 * EdFi to SLI transformer based on Smooks
 *
 * @author okrook
 *
 */
@Component
public class SmooksEdFi2SLITransformer extends EdFi2SLITransformer {
    private static final ObjectMapper MAPPER = new ObjectMapper();

    private Map<String, Smooks> smooksConfigs;

    
    @SuppressWarnings("unchecked")
    @Override
    public List<SimpleEntity> transform(NeutralRecord item, ErrorReport errorReport) {

        JavaResult result = new JavaResult();
        Smooks smooks = smooksConfigs.get(item.getRecordType());

        List<SimpleEntity> sliEntities;

        try {
            StringSource source = new StringSource(MAPPER.writeValueAsString(item));

            smooks.filterSource(source, result);

            sliEntities = getEntityListResult(result);
            //sliEntities = result.getBean(List.class);
        } catch (IOException e) {
            sliEntities = Collections.emptyList();
        }

        return sliEntities;
    }
    
   /**
    * Traverse the results map to get the entity list
    */
    @SuppressWarnings("unchecked")
    private List<SimpleEntity> getEntityListResult(JavaResult result) {
        List<SimpleEntity> entityList = new ArrayList<SimpleEntity>();
        for (Entry<String, Object> resEntry : result.getResultMap().entrySet()) {
            if (resEntry.getValue() instanceof List) {
                List<?> list = (List<?>) resEntry.getValue();
                if (list.size() != 0 && list.get(0) instanceof SimpleEntity) {
                    entityList = (List<SimpleEntity>) list;
                    break;
                }
            }
        }
        return entityList;
    }

    public Map<String, Smooks> getSmooksConfigs() {
        return smooksConfigs;
    }

    public void setSmooksConfigs(Map<String, Smooks> smooksConfigs) {
        this.smooksConfigs = smooksConfigs;
    }

}

@@ -92,24 +92,49 @@
 .ArrayList;%0A
+import java.util.Arrays;%0A
 import java.
@@ -151,16 +151,16 @@
 ctions;%0A
-
 import j
@@ -795,48 +795,8 @@
 s;%0A%0A
-    %0A    @SuppressWarnings(%22unchecked%22)%0A
     
@@ -1001,24 +1001,491 @@
 rdType());%0A%0A
+        // if no smooks configured for this, then just convert the neutral record to a SimpleEntity%0A        // directly.%0A        if (smooks == null) %7B%0A            String type = item.getRecordType().replaceAll(%22_transformed%22, %22%22);%0A            Map%3CString, Object%3E body = item.getAttributes();%0A            SimpleEntity entity = new SimpleEntity();%0A            entity.setType(type);%0A            entity.setBody(body);%0A%0A            return Arrays.asList(entity);%0A        %7D%0A%0A
         List
@@ -1732,16 +1732,17 @@
       //
+ 
 sliEntit
@@ -1909,20 +1909,18 @@
   %7D%0A
-    
 %0A
+ 
    /**%0A
+ 
     
@@ -1969,16 +1969,17 @@
 ty list%0A
+ 
     */%0A 

allow pass through EdFi2SliTransformer without a second smooks config