package org.slc.sli.ingestion.handler;

import java.io.File;
import java.io.IOException;

import org.slc.sli.ingestion.landingzone.IngestionFileEntry;
import org.slc.sli.ingestion.landingzone.LocalFileSystemLandingZone;
import org.slc.sli.ingestion.validation.ErrorReport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import org.slc.sli.ingestion.hdfs.HDFSUtil;

/**
 *
 * @author ccheng
 *
 */
@Component
public class MatchDiffHandler extends AbstractIngestionHandler<IngestionFileEntry, IngestionFileEntry> {

    private static final Logger LOG = LoggerFactory.getLogger(MatchDiffHandler.class);

    @Autowired
    LocalFileSystemLandingZone lz;

    @Override
    IngestionFileEntry doHandling(IngestionFileEntry fileEntry, ErrorReport errorReport) {
        try {
            // load new and current state files to HDFS
            loadFiles(fileEntry, errorReport);

            // compute diff between new and current files
            computeDelta(fileEntry, errorReport);

        } catch (IOException e) {
            LOG.error("Failed to load files to HDFS.");
            errorReport.fatal("Failed to load files to HDFS.", MatchDiffHandler.class);
        }

        return fileEntry;
    }

    void loadFiles(IngestionFileEntry fileEntry, ErrorReport errorReport) throws IOException {

        // load new files from user
        File newRecordFile = fileEntry.getNeutralRecordFile();
        // TODO: switch to HDFS as follows:
        // HDFSUtil.addFile(newRecordFile, "/");
        // temporarily using local landing zone
        lz.loadFile(newRecordFile);

        // load current state files
        File currentRecordFile = getCurrentRecordFile(fileEntry, errorReport);
        // TODO: switch to HDFS as follows:
        // HDFSUtil.addFile(currentRecordFile, "/");
        // temporarily using local landing zone
        lz.loadFile(currentRecordFile);

    }

    void computeDelta(IngestionFileEntry fileEntry, ErrorReport errorReport) {

        // TODO: compute delta between new and current state records
        File deltaFile = null;

        // write delta to deltaNeutralRecordFile
        fileEntry.setDeltaNeutralRecordFile(deltaFile);

    }

    File getCurrentRecordFile(IngestionFileEntry fileEntry, ErrorReport errorReport) throws IOException {

        // TODO: fetch current state records
        // temporarily return empty file
        File file = File.createTempFile("currentRecord_", ".tmp");
        file.deleteOnExit();
        return file;
    }
}

@@ -769,16 +769,85 @@
 ne lz;%0A%0A
+    private File newRecordFile;%0A    private File currentRecordFile;%0A%0A
     @Ove
@@ -852,16 +852,16 @@
 verride%0A
-
     Inge
@@ -1532,21 +1532,16 @@
         
-File 
 newRecor
@@ -1804,21 +1804,16 @@
         
-File 
 currentR
@@ -2139,16 +2139,35 @@
 rReport)
+ throws IOException
  %7B%0A%0A    
@@ -2263,16 +2263,88 @@
  null;%0A%0A
+        deltaFile = computeDeltaImp(newRecordFile, currentRecordFile);%0A%0A
         
@@ -2448,24 +2448,32 @@
 %0A    %7D%0A%0A    
+private 
 File getCurr
@@ -2741,24 +2741,24 @@
 teOnExit();%0A
-
         retu
@@ -2771,11 +2771,238 @@
 e;%0A    %7D
+%0A%0A    private File computeDeltaImp(File newRecordFile, File currentRecordFile) %7B%0A%0A        // TODO: right now the currentRecordFile is empty, so the delta file is the same as the newRecordFile%0A        return newRecordFile;%0A    %7D
 %0A%7D%0A

add psudo delta computation code as a placeholder