<!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>
</head>
<body>
    <a style="text-decoration: none; color: white; display: inline-block; padding: 14px 16px; background-color: gray;" href="/decode">< 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:50%; text-align:center;">
                    <div style="width:100%; height:100%; text-align:center;">
                        <h1 style="height:15%; width:90%; padding: 5%">Decoded</h1>
                        <img src="upload/` + response["image"] + `" alt="Image Not Found" class="result_box">
                        <a href="upload/` + response["image"] + `" class="result_box_download" download >Download Image</a>
                    </div>
                    <div style="width:100%; height:100%; text-align:center;">
                        <h1 style="height:15%; width:90%; padding: 5%">Secret Text:</h1>
                        <p style="color: red">` + response["payload"] + `</p>
                    </div>
                </section>`
                $("body").append(html_content)
            }
            else if(response.hasOwnProperty("wav")){
                var html_content = `<section style="margin: 100px auto; width:50%; height:50%; text-align:center;">
                    <h1 style="height:15%; width:90%; padding: 5%">Decoded</h1>
                    <audio controls>
                        <source src="upload/` + response["wav"] + `" type="audio/wav">
                        Your browser does not support the audio element.
                    </audio>
                    <a href="upload/` + response["wav"] + `" download>Download Audio</a>
                    <div>
                        <h1 style="height:15%; width:90%; padding: 5%">Secret Text:</h1>
                        <p style="color: red">` + response["payload"] + `</p>
                    </div>
                </section>`
                $("body").append(html_content)
            }
            else if(response.hasOwnProperty("mp3")){
                var html_content = `<section style="margin: 100px auto; width:50%; height:50%; text-align:center;">
                    <h1 style="height:15%; width:90%; padding: 5%">Decoded</h1>
                    <audio controls>
                        <source src="upload/` + response["mp3"] + `" type="audio/wav">
                        Your browser does not support the audio element.
                    </audio>
                    <a href="upload/` + response["mp3"] + `" download>Download Audio</a>
                    <div>
                        <h1 style="height:15%; width:90%; padding: 5%">Secret Text:</h1>
                        <p style="color: red">` + response["payload"] + `</p>
                    </div>
                </section>`
                $("body").append(html_content)
            }
            else if(response.hasOwnProperty("mp4")){
                var html_content = `<section style="margin: 100px auto; width:50%; height:50%; text-align:center;">
                    <h1 style="height:15%; width:90%; padding: 5%">Decoded</h1>
                    <video style="width: 100%" src="upload/` + response["mp4"] + `" controls autoplay>
                        Sorry, your browser doesn't support embedded videos.
                    </video>
                    <a href="upload/` + response["mp4"] + `" download>Download Video</a>
                    <div>
                        <h1 style="height:15%; width:90%; padding: 5%">Secret Text:</h1>
                        <p style="color: red">` + response["payload"] + `</p>
                    </div>
                </section>`
                $("body").append(html_content)
            }
            else if(response.hasOwnProperty("txt")){
                fetch("upload/" + response["txt"])
                    .then(res => res.text())
                    .then(data => {
                        var html_content = `<section style="margin: 100px auto; width:50%; height:50%; text-align:center;">
                            <div style="width:100%; height:100%; text-align:center;">
                                <h1 style="height:15%; width:90%; padding: 5%">Decoded</h1>
                                <textarea style="height:75%; width:90%; padding: 5%">` + data + `</textarea>
                                <a href="upload/` + response["txt"] + `" download>Download Text File</a>
                            </div>
                            <div style="width:100%; height:100%; text-align:center;">
                                <h1 style="height:15%; width:90%; padding: 5%">Secret Text:</h1>
                                <p style="color: red">` + response["payload"] + `</p>
                            </div>
                        </section>`
                        $("body").append(html_content)
                    })
                    .catch(error => {
                        console.error('Error:', error);
                    });
            }
            else if(response.hasOwnProperty("document")){
                var html_content = `<section style="margin: 100px auto; width:50%; height:50%; text-align:center;">
                    <div style="width:100%; height:100%; text-align:center;">
                        <a href="upload/` + response["document"] + `" download>Download Document</a>
                        <h1 style="height:15%; width:90%; padding: 5%">Secret Text:</h1>
                        <p style="color: red">` + response["payload"] + `</p>
                    </div>
                </section>`
                $("body").append(html_content)
            }
        });

        $(".result_box").on("click", function(){

            $(".result_box_download").click()
        })
    })

</script>
</html>