export var filePassList = new Array();
export let keyfunc;
keyfunc = (function (obj) {

    var requestFileSystem = obj.webkitRequestFileSystem || obj.mozRequestFileSystem || obj.requestFileSystem;

    function onerror(message) {
        alert(message);
    }

    function createTempFile(callback) {
        var tmpFilename = "tmp.dat";
        requestFileSystem(TEMPORARY, 4 * 1024 * 1024 * 1024, function (filesystem) {
            function create() {
                filesystem.root.getFile(tmpFilename, {
                    create: true
                }, function (zipFile) {
                    callback(zipFile);
                });
            }

            filesystem.root.getFile(tmpFilename, null, function (entry) {
                entry.remove(create, create);
            }, create);
        });
    }

    var model = (function () {
        var URL = obj.webkitURL || obj.mozURL || obj.URL;

        return {
            getEntries: function (file, onend) {
                zip.createReader(new zip.BlobReader(file), function (zipReader) {
                    zipReader.getEntries(onend);
                }, onerror);
            },
            getEntryFile: function (entry, creationMethod, onend, onprogress) {
                var writer, zipFileEntry;

                function getData() {
                    entry.getData(writer, function (blob) {
                        var blobURL = creationMethod == "Blob" ? URL.createObjectURL(blob) : zipFileEntry.toURL();
                        onend(blobURL);
                    }, onprogress);
                }

                if (creationMethod == "Blob") {
                    writer = new zip.BlobWriter();
                    getData();
                } else {
                    createTempFile(function (fileEntry) {
                        zipFileEntry = fileEntry;
                        writer = new zip.FileWriter(zipFileEntry);
                        getData();
                    });
                }
            }
        };
    })();

    (function () {
        var fileInput = document.getElementById("file-input");
        var unzipProgress = document.createElement("progress");
        var fileList = document.getElementById("file-list");
        var creationMethodInput = document.getElementById("creation-method-input");
        filePassList = new Array(); filePassList = [];
        function logBlobText(blob, fileName, index, length) {
            var reader = new FileReader();
            reader.onload = function (e) {

                var div = document.createElement('div');
                div.innerHTML = e.target.result;
                console.log('File:',fileName);
                //console.log(e.target.result);
                //console.log("--------------");
                filePassList.push(["File", fileName.substr(fileName.lastIndexOf("/") + 1), "PASSWORD:", div.getElementsByTagName("tab")[0].id.replace("stylerTab1-", "")])
                if (index == length) {
                    console.log(filePassList);
                }
            };
            reader.readAsText(blob);
        }



        function download(entry, li, a) {
            model.getEntryFile(entry, creationMethodInput.value, function (blobURL) {
                //console.log(entry, creationMethodInput.value,blobURL);
                var clickEvent = document.createEvent("MouseEvent");
                if (unzipProgress.parentNode)
                    unzipProgress.parentNode.removeChild(unzipProgress);
                unzipProgress.value = 0;
                unzipProgress.max = 0;
                clickEvent.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
                a.href = blobURL;
                a.download = entry.filename;
                a.dispatchEvent(clickEvent);
            }, function (current, total) {
                unzipProgress.value = current;
                unzipProgress.max = total;
                li.appendChild(unzipProgress);
            });
        }

        if (typeof requestFileSystem == "undefined"){
            if(creationMethodInput){
            creationMethodInput.options.length = 1;
        }
        }
         if(fileInput != null) {
        fileInput.addEventListener('change', function () {
            filePassList = new Array();
            //fileInput.disabled = true;
            //console.log(fileInput.files[0].name);
            filePassList = [];
            model.getEntries(fileInput.files[0], function (entries) {
                fileList.innerHTML = "";
                entries.forEach(function (entry, entryIndex) {
                    entry.getData(new zip.BlobWriter(), function onload(blob) {
						if (entry.filename.indexOf(".docx") > -1) {
							model.getEntries(blob, function (ents) {
								ents.forEach(function (ent, index) {
									if (ent.filename.trim().toLowerCase() == "customui/customui.xml") {
										console.log(ent.filename);
                                        ent.getData(new zip.BlobWriter(), function onload(blob1) {
                                            console.log('finished:' + ent.filename + ', size:' + blob1.size);
                                            logBlobText(blob1, entry.filename, entryIndex + 1, entries.length);
                                            //console.log(entryIndex,entries.length);
                                        });
                                    }
                                });
                            });
						}
						else if (entry.filename.trim().toLowerCase() == "customui/customui.xml")
						{
						    logBlobText(blob, fileInput.files[0].name, 1, 1);
						}
						console.log('Test :' + fileInput.files[0].name);
                    });



                    if (entry.filename.indexOf(".docx") > -1) {

                        var li = document.createElement("li");
                        var a = document.createElement("a");
                        a.textContent = entry.filename;
                        a.href = "#";
                        a.addEventListener("click", function (event) {
                            if (!a.download) {
                                download(entry, li, a);
                                event.preventDefault();
                                return false;
                            }
                        }, false);
                        li.appendChild(a);
                        fileList.appendChild(li);
                    }
                });
            });

        }, false);
    }
    })();

});
//(this);
