<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <title>RESULT</title>
    <style>
        *{
            padding: 0px;
            margin: 0px;
        }

        html, body{
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body>
    <a style="text-decoration: none; color: white; display: inline-block; padding: 14px 16px; background-color: gray;" href="/encode">< Back</a>
</body>
<script>

    $(document).ready(function(){

        $.get('/get_session', function(response) {
            console.log(response)
            if(response.hasOwnProperty("image")){
                var html_content = `<section style="margin: 100px auto; width:50%; height:55%; display: flex; justify-content: space-between;">
                    <div style="border: 1px solid black; width:100%; height:100%; text-align:center;">
                        <h1 style="height:15%; width:90%; padding: 5%">Original</h1>
                        <img src="upload/` + response["image"] + `" alt="Image Not Found" class="result_box">
                        <br>
                        <a href="upload/` + response["image"] + `" download rel="nofollow" class="result_box_download">Download Image</a>
                    </div>
                    <div style="border: 1px solid black; width:100%; height:100%; text-align:center;">
                        <h1 style="height:15%; width:90%; padding: 5%">Encoded</h1>
                        <img src="upload/` + response["image2"] + `" alt="Image Not Found" class="result_box">
                        <br>
                        <a href="upload/` + response["image2"] + `" download rel="nofollow" class="result_box_download">Download Image</a>
                    </div>

                </section>`
                $("body").append(html_content)
            }
            else if(response.hasOwnProperty("wav")){
                var html_content = `<section style="margin: 100px auto; width:50%; height:50%; display: flex; justify-content: space-between;">
                    <div style="border: 1px solid black; width:100%; height:100%; text-align:center;">
                    <h1 style="height:15%; width:90%; padding: 5%">Original</h1>
                    <audio controls>
                        <source src="upload/` + response["wav"] + `" type="audio/wav">
                        Your browser does not support the audio element.
                    </audio>
                    <br>
                    <a href="upload/` + response["wav"] + `" download rel="nofollow">Download Audio</a>
                </div>
                <div style="border: 1px solid black; width:100%; height:100%; text-align:center;">
                    <h1 style="height:15%; width:90%; padding: 5%">Encoded</h1>
                    <audio controls>
                        <source src="upload/` + response["wav2"] + `" type="audio/wav">
                        Your browser does not support the audio element.
                    </audio>
                    <br>
                    <a href="upload/` + response["wav2"] + `" download rel="nofollow">Download Audio</a>
                </div>
                </section>`
                $("body").append(html_content)
            }
            else if(response.hasOwnProperty("mp3")){
                var html_content = `<section style="margin: 100px auto; width:50%; height:50%; display: flex; justify-content: space-between;">
                    <div style="border: 1px solid black; width:100%; height:100%; text-align:center;">
                    <h1 style="height:15%; width:90%; padding: 5%">Original</h1>
                    <audio controls>
                        <source src="upload/` + response["mp3"] + `" type="audio/wav">
                        Your browser does not support the audio element.
                    </audio>
                    <br>
                    <a href="upload/` + response["mp3"] + `" download rel="nofollow">Download Audio</a>
                </div>
                <div style="border: 1px solid black; width:100%; height:100%; text-align:center;">
                    <h1 style="height:15%; width:90%; padding: 5%">Encoded</h1>
                    <audio controls>
                        <source src="upload/` + response["mp3_2"] + `" type="audio/wav">
                        Your browser does not support the audio element.
                    </audio>
                    <br>
                    <a href="upload/` + response["mp3_2"] + `" download rel="nofollow">Download Audio</a>
                </div>
                </section>`
                $("body").append(html_content)
            }
            else if(response.hasOwnProperty("mp4")){
                var html_content = `<section style="margin: 100px auto; width:50%; height:50%; display: flex; justify-content: space-between;">
                    <div style="border: 1px solid black; width:100%; height:100%; text-align:center;">
                    <h1 style="height:15%; width:90%; padding: 5%">Original</h1>
                    <video style="width: 100%" src="upload/` + response["mp4"] + `" controls autoplay>
                        Sorry, your browser doesn't support embedded videos.
                    </video>
                    <br>
                    <a href="upload/` + response["mp4"] + `" download rel="nofollow">Download Video</a>
                </div>
                <div style="border: 1px solid black; width:100%; height:100%; text-align:center;">
                    <h1 style="height:15%; width:90%; padding: 5%">Encoded</h1>
                    <video style="width: 100%" src="upload/` + response["mp4_2"] + `" controls autoplay>
                        Sorry, your browser doesn't support embedded videos.
                    </video>
                    <br>
                    <a href="upload/` + response["mp4_2"] + `" download rel="nofollow">Download Video</a>
                </div>
                </section>`
                $("body").append(html_content)
            }
            else if(response.hasOwnProperty("document")){

                var html_content = `<section style="margin: 100px auto; width:50%; height:50%; display: flex; justify-content: space-between;">
                    <div style="border: 1px solid black; width:100%; height:100%; text-align:center;">
                    <h1 style="height:15%; width:90%; padding: 5%">Original</h1>
                    <br>
                    <a href="upload/` + response["document"] + `" download rel="nofollow">Download Document</a>
                </div>
                <div style="border: 1px solid black; width:100%; height:100%; text-align:center;">
                    <h1 style="height:15%; width:90%; padding: 5%">Encoded</h1>
                    <br>
                    <a href="upload/` + response["document_2"] + `" download rel="nofollow">Download Document</a>
                </div>
                </section>`
                $("body").append(html_content)
            }
            else if(response.hasOwnProperty("txt")){
                
                fetch("upload/" + response["txt"])
                    .then(res => res.text())
                    .then(data => {
                        fetch("upload/" + response["txt2"])
                        .then(res2 => res2.text())
                        .then(data2 => {
                            var html_content = `<section style="margin: 100px auto; width:50%; height:50%; display: flex; justify-content: space-between;">
                                <div style="border: 1px solid black; width:100%; height:100%; text-align:center;">
                                    <h1 style="height:15%; width:90%; padding: 5%">Original</h1>
                                    <textarea style="height:77%; width:100%">` + data + `</textarea>
                                </div>
                                <div style="border: 1px solid black; width:100%; height:100%; text-align:center;">
                                    <h1 style="height:15%; width:90%; padding: 5%">Encoded</h1>
                                    <textarea style="height:77%; width:100%">` + data2 + `</textarea>
                                </div>

                            </section>`
                            $("body").append(html_content)
                        })
                        .catch(error2 => {
                            console.error('Error:', error2);
                        });
                    })
                    .catch(error => {
                        console.error('Error:', error);
                    });
            }
        });

        $("body").on("click", ".result_box", function(e){
            e.preventDefault()
            window.location.href = $(this).parent().find("a").attr("href")
        })
    })

</script>
</html>