<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
<script src="../../editing.js"></script>
</head>
<body>
<div id="dropzone" style="width: 200px; height: 200px; background-color: grey;"></div>
<script>
description("Basic test coverage for fileSystemDirectoryReader.readEntries()");
jsTestIsAsync = true;

function getEntriesAsPromise(dirEntry) {
    return new Promise((resolve, reject) => {
        let result = [];
        let reader = dirEntry.createReader();
        let doBatch = () => {
            reader.readEntries(entries => {
            if (entries.length > 0) {
                entries.forEach(e => result.push(e));
                doBatch();
            } else {
                resolve(result);
            }
        }, reject);
    };
    doBatch();
  });
}

function findEntryByName(entries, name)
{
    for (let childEntry of entries) {
        if (childEntry.name == name)
            return childEntry
    }
    return null;
}

var dropzone = document.getElementById('dropzone');
dropzone.ondrop = function(e) {
    e.preventDefault();
    dataTransfer = e.dataTransfer;

    shouldBe("dataTransfer.items.length", "2");

    entry1 = dataTransfer.items[0].webkitGetAsEntry();
    entry2 = dataTransfer.items[1].webkitGetAsEntry();

    shouldBeTrue("entry1.isFile");
    shouldBeEqualToString("entry1.name", "test.txt");
    shouldBeEqualToString("entry1.fullPath", "/test.txt");
    getEntriesAsPromise(entry1.filesystem.root).then(function(entries) {
        entryFound = findEntryByName(entries, "test.txt");
        shouldBeTrue("entryFound.isFile");
        shouldBeEqualToString("entryFound.name", "test.txt");
        shouldBeEqualToString("entryFound.fullPath", "/test.txt");

        shouldBeTrue("entry2.isDirectory");
        shouldBeEqualToString("entry2.name", "testFiles");
        shouldBeEqualToString("entry2.fullPath", "/testFiles");
        getEntriesAsPromise(entry2.filesystem.root).then(function(entries) {
            entryFound = findEntryByName(entries, "testFiles");
            shouldBeTrue("entryFound.isDirectory");
            shouldBeEqualToString("entryFound.name", "testFiles");
            shouldBeEqualToString("entryFound.fullPath", "/testFiles");

            finishJSTest();
        }, function(e) {
            testFailed("readEntries failed: " + e);
            finishJSTest();
        });
    }, function(e) {
        testFailed("readEntries failed: " + e);
        finishJSTest();
    });
};

dropzone.ondragover = function(ev) {
    ev.preventDefault();
}

onload = function() {
    dragFilesOntoElement(dropzone, ['../../../fast/forms/resources/test.txt', '../../../fast/forms/file/entries-api/resources/testFiles']);
}
</script>
</body>
</html>
