<%include partials/header.ejs %>

<div class="container">

    <label for="flo-addr">Enter the flo address (oSjBiuTE1aFNBjaSGq6UNhU9ddpD2YXdg8)</label>
    <input type="text" id="flo-addr" class="form-control" />

    <p><button id="flo-addr-btn" class="btn btn-primary">Get Torrents List</button></p>

    <ul id="flo-res"></ul>  

    <label for="mag_text">Provide a magnetic URL below:</label>
    <textarea class="form-control" id="mag_text" rows="3"></textarea>  
    <p><button id="dwn-seed" class="btn btn-primary">View</button></p>

    <p id="dwld"></p>

    <div class="pd-10">
        <div id="mag-res"></div> 
    </div>

</div>

<%include partials/footer.ejs %>

<script>


    $(document).on('click', '#flo-addr-btn', function() {
        var floaddr = $("#flo-addr").val();
        if (floaddr.length<1) {
            alert("Please specify a FLO address.");
            return;
        }

        var job = 'flo-comment';
        
        $.ajax({
            url: '/fetch-from-blockchain',
            type: 'post',
            data: {job:job, floaddr:floaddr},
            success: function(response) {
                console.log(response);
                var t = '';
                if (response.msg.length>0) {
                    t += `<li><h5 class="text-info">${response.msg}</h5></li>`;
                    t += ``;
                }

                if (response.error==false && typeof response.data != undefined) {
                    for (let fd = 0; fd < response.data.length; fd++) {
                        const fdata = response.data[fd];
                        t += `<li><p>${fdata}</p></li>`;
                    }
                }
                $("#flo-res").html(t);
            },
            error: function() {

            }
        });
    });

    $(document).on('click', '#dwn-seed', function() {
        
        var client = new WebTorrent()

        var torrentId = document.getElementById('mag_text').value;

        if (torrentId.trim() == '') {
            alert("Please specify a magnetic url.");
            return;
        }

        client.add(torrentId, function (torrent) {
        // Torrents can contain many files. Let's use the .mp4 file
        var file = torrent.files.find(function (file) {
            console.log(file);
            if (file.name.endsWith('.mp4')) {
                return file.name.endsWith('.mp4')    
            } else if (file.name.endsWith('.webm')) {
                return file.name.endsWith('.webm')    
            } else if (file.name.endsWith('.avi')) {
                return file.name.endsWith('.avi')    
            } else if (file.name.endsWith('.pdf')) {
                return file.name.endsWith('.pdf')    
            } else if (file.name.endsWith('.mp3')) {
                return file.name.endsWith('.mp3')    
            } else if (file.name.endsWith('.jpg')) {
                return file.name.endsWith('.jpg')    
            } else if (file.name.endsWith('.png')) {
                return file.name.endsWith('.png')    
            } else if (file.name.endsWith('.gif')) {
                return file.name.endsWith('.gif')    
            } else if (file.name.endsWith('.jpeg')) {
                return file.name.endsWith('.jpeg')    
            } 
        })

        console.log('Client is downloading:', torrent.infoHash)
        
        // Display the file by adding it to the DOM. Supports video, audio, image, etc. files
        file.appendTo('#mag-res')

        file.getBlobURL(function (err, url) {
            if (err) throw err
            var a = document.createElement('a')
            a.download = file.name
            a.href = url
            a.textContent = 'Download ' + file.name
            document.getElementById("dwld").appendChild(a)
        })

        })

    });

</script>