import {downloadFile} from "./download"
import {uploadFile} from "./upload"
import JSZip from "jszip"
import JSZipUtils from "jszip-utils"

/** Creates a zip file.
 * @function zipFileCreator
 * @param {list} textFiles A list of files in plain text format.
 * @param {list} httpFiles A list fo files that have to be downloaded from the internet before being included.
 * @param {string} zipFileName The name of the zip file to be created
 * @param {string} [mimeType=application/zip] The mimetype of the file that is to be created.
 * @param {list} includeZips A list of zip files to be merged into the output zip file.
 * @param {boolean} [upload=false] Whether to upload rather than downloading the Zip file once finished.
 * @param {object} editor An editor instance (only for upload=true).
 */

export let zipFileCreator = function(textFiles, httpFiles, zipFileName,
    mimeType, includeZips, upload, editor) {
    let zipFs = new JSZip(),
        zipDir

    if (mimeType) {
        zipFs.file('mimetype', mimeType, {compression: 'STORE'})
    } else {
        mimeType = 'application/zip'
    }


    let createZip = function() {
        for (let i = 0; i < textFiles.length; i++) {
            zipFs.file(textFiles[i].filename, textFiles[i].contents, {compression: 'DEFLATE'})
        }

        for (let i = 0; i < httpFiles.length; i++) {
            JSZipUtils.getBinaryContent(httpFiles[
                i].url, function(err, contents) {
                    zipFs.file(httpFiles[i].filename, contents, {binary: true, compression: 'DEFLATE'})
                })
        }

        zipFs.generateAsync({type:"blob"})
            .then(function(blob) {
                if (upload) {
                    uploadFile(zipFileName, blob, editor)
                } else {
                    downloadFile(zipFileName, blob)
                }
        })

    }

    if (includeZips) {
        let i = 0
        let includeZipLoop = function() {
            if (i === includeZips.length) {
                createZip()
            } else {
                if (includeZips[i].directory === '') {
                    zipDir = zipFs
                } else {
                    zipDir = zipFs.folder(includeZips[i].directory)
                }
                JSZipUtils.getBinaryContent(includeZips[i].url, function(err, contents) {
                    zipDir.loadAsync(contents).then(function (importedZip) {
                        i++
                        includeZipLoop()
                    })
                })
            }

        }
        includeZipLoop()
    } else {
        createZip()
    }
}

@@ -1280,32 +1280,50 @@
 TE'%7D)%0A        %7D%0A
+        let p = %5B%5D
 %0A        for (le
@@ -1352,32 +1352,115 @@
 .length; i++) %7B%0A
+            p.push(new window.Promise(%0A                function(resolve) %7B%0A        
             JSZi
@@ -1493,33 +1493,16 @@
 tpFiles%5B
-%0A                
 i%5D.url, 
@@ -1519,32 +1519,36 @@
 rr, contents) %7B%0A
+    
                 
@@ -1627,32 +1627,70 @@
 on: 'DEFLATE'%7D)%0A
+                        resolve()%0A    
                 
@@ -1700,27 +1700,123 @@
         
+ 
-%7D%0A%0A
+       %7D%0A            ))%0A%0A        %7D%0A        window.Promise.all(p).then(%0A            function()%7B%0A   
         
 zipFs.ge
@@ -1803,24 +1803,29 @@
 %0A           
+     
 zipFs.genera
@@ -1859,16 +1859,24 @@
         
+      
+  
 .then(fu
@@ -1902,24 +1902,32 @@
             
+        
 if (upload) 
@@ -1948,16 +1948,24 @@
         
+ 
+       
 uploadFi
@@ -2002,32 +2002,40 @@
                 
+        
 %7D else %7B%0A       
@@ -2047,16 +2047,24 @@
         
+      
+  
 download
@@ -2107,21 +2107,61 @@
     
-%7D%0A        %7D)%0A
+        %7D%0A                %7D)%0A            %7D%0A%0A        )
 %0A   

switch to jszip, enable image saving