package com.doplgangr.secrecy.Jobs;

import android.content.Context;

import com.doplgangr.secrecy.Config;
import com.doplgangr.secrecy.Events.BackUpDoneEvent;
import com.doplgangr.secrecy.Events.BackingUpFileEvent;
import com.path.android.jobqueue.Job;
import com.path.android.jobqueue.Params;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import de.greenrobot.event.EventBus;

/**
 * Created by matthew on 11/2/14.
 */
public class BackupJob extends Job {
    private static final int PRIORITY = 10;   //High. Same as UI jobs
    private static final int RETRY_LIMIT = 5; //Shouldn't try too much.
    private File backupPath;    //Path of the folder to backup
    private File backupFile; //Path of backupFile
    private Context context;

    public BackupJob(Context context, File backupPath, File backupFile) {
        super(new Params(PRIORITY)
                .groupBy(backupPath.getAbsolutePath()));
        this.context = context;
        this.backupPath = backupPath;
        this.backupFile = backupFile;
        if (!backupPath.isDirectory())
            throw new IllegalArgumentException("Backup Path must be a directory");
        if (backupFile.isDirectory())
            throw new IllegalArgumentException("Backup File must be a file");
    }

    @Override
    public void onAdded() {
    }

    @Override
    public void onRun() throws Throwable {
        FileOutputStream fos = new FileOutputStream(backupFile);
        ZipOutputStream zos = new ZipOutputStream(fos);
        byte[] buffer = new byte[Config.bufferSize];

        Iterator it = FileUtils.iterateFiles(backupPath, null, true);
        while (it.hasNext()) {
            File fileToBackup = ((File) it.next());
            EventBus.getDefault().post(new BackingUpFileEvent(backupPath.getAbsolutePath(), fileToBackup.getName()));
            ZipEntry newEntry = new ZipEntry(fileToBackup.getAbsolutePath());
            zos.putNextEntry(newEntry);

            FileInputStream in =
                    new FileInputStream(fileToBackup);

            int len;
            while ((len = in.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }

            in.close();

        }
        zos.closeEntry();
        zos.close();

        EventBus.getDefault().post(new BackUpDoneEvent(backupPath, backupFile));

    }

    @Override
    protected void onCancel() {
    }

    @Override
    protected boolean shouldReRunOnThrowable(Throwable throwable) {
        //ignore everything and rerun
        throwable.printStackTrace();
        return true;
    }

    @Override
    protected int getRetryLimit() {
        return RETRY_LIMIT;
    }
}

@@ -326,24 +326,97 @@
 FileUtils;%0A%0A
+import java.io.BufferedInputStream;%0Aimport java.io.BufferedOutputStream;%0A
 import java.
@@ -1732,12 +1732,38 @@
 eam(
+new BufferedOutputStream(
 fos)
+)
 ;%0A  
@@ -2216,20 +2216,24 @@
         
-File
+Buffered
 InputStr
@@ -2261,24 +2261,48 @@
         new 
+BufferedInputStream(new 
 FileInputStr
@@ -2318,16 +2318,17 @@
 oBackup)
+)
 ;%0A%0A     

Add buffers to (hopefully) faster performance