<%include partials/header.ejs %>

<div class="container">
    <p id='#drag'>Drop a file to make it a torrent file:</p>
    <div class="card drop-div" id="_drop"></div>  

    <div class="card">
        <p id="dw-file"></p>
        <div id="seed-result"></div>
    </div>

</div>

<%include partials/footer.ejs %>

<script>
    $(document).ready(function() {
        seeding();
    });

// You can pass in a DOM node or a selector string!
let seeding = () => {
    DragDrop('.drop-div', function (files, pos, fileList, directories) {
        console.log('Here are the dropped files', files)
        console.log('Dropped at coordinates', pos.x, pos.y)
        console.log('Here is the raw FileList object if you need it:', fileList)
        console.log('Here is the list of directories:', directories)
    
        var client = new WebTorrent()

        client.seed(files, function (torrent) {
            console.log(torrent);
            console.log('Client is seeding ' + torrent.magnetURI)

            let file_string = torrent.torrentFile.toString('hex');
    
            let t = ``;
    
            t += `Client is seeding <strong>${torrent.magnetURI}</strong>`;
    
            t += `<p>Name: ${torrent.name}</p>`;
            t += `<p>Path: ${torrent.path}</p>`;
            t += `<p><strong>peerId: ${torrent.client.peerId}</strong></p>`;
            t += `<p><strong>infoHash: ${torrent.infoHash}</strong></p>`;
            var hexdata = torrent.torrentFile.toString('hex');
            t += `<p>Torrent: ${hexdata}</p>`;
            t += `<p></p>`;
    
            // t += `<h5>Files hash</h5>`;
            // for (let h = 0; h < torrent._hashes.length; h++) {
            //     const hash = torrent._hashes[h];
            //     t += `<p>${hash}</p>`;
            // }
    
            t += `<h5>Peers</h5>`;
            for (let p = 0; p < torrent._peers.length; p++) {
                const peer = torrent._peers[p];
                t += `<p>Peer: ${peer}</p>`;
            }
    
            t += `<h5>Trackers</h5>`;
            for (let r = 0; r < torrent.discovery._announce.length; r++) {
                const tracker = torrent.discovery._announce[r];
                t += `<p>${tracker}</p>`;
            }
    
            $("#seed-result").html(t);

            document.getElementById('dw-file').innerHTML = '<a href="' + torrent.torrentFileBlobURL + '" target="_blank" download="file.torrent">[Download .torrent]</a>';

        })
    })
}

$(document).on('click', "#dwldspan", function() {
    var loc = $(this).val();
    console.log(loc);
    window.location(loc)
})

</script>