<!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">
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="fa/css/all.min.css">
    <meta property="og:title" content="THE Bin" />
    <meta property="og:type" content="website" />
    <meta property="og:url" content="http://bin.got-hacked.rip/" />
    <meta property="og:description" content="An open-source, uncensored bin." />
    <meta name="theme-color" content="#5D67FF">
    <title>New Paste</title>
    <script>
        let currentencryptionstatus = true;
        function alert(text) {
            if(document.getElementsByClassName('alert').length > 0) {
                Object.keys(document.getElementsByClassName('alert')).forEach(item => {
                    if(item != 'length') {
                        document.body.removeChild(document.getElementsByClassName('alert')[item]);
                    }
                })
            }
            var alert = document.createElement("div");
            alert.className = "alert";
            alert.innerHTML = text;
            document.body.appendChild(alert);
            setTimeout(function() {
                document.body.removeChild(alert);
            }, 3000);
        }
        function savePaste() {
            document.getElementById('save-button').style = 'display: none';
            let xhr = new XMLHttpRequest();
            xhr.open("POST", "/write");

            xhr.setRequestHeader("Accept", "application/json");
            xhr.setRequestHeader("Content-Type", "application/json");

            let text = document.getElementById('editor').value;
            let name = document.getElementById('name').value;

            let data = JSON.stringify({"text": text, "name": name, "status": currentencryptionstatus});

            xhr.send(data);

            xhr.onload = () => {
                let rq = xhr.responseText;
                if (xhr.status == 200) {
                    alert("Paste saved! Redirecting...");
                    let jsonRQ = JSON.parse(rq);
                    if(jsonRQ.key == undefined || jsonRQ.filename == undefined || jsonRQ.iv == undefined) {
                        alert("Internal error has occured, we could not get required properties from request correctly!");
                        return;
                    }
                    let id = jsonRQ.filename;
                    let key = jsonRQ.key;
                    let iv = jsonRQ.iv;
                    if(key == "" || iv == []) {
                        setTimeout(() => {window.location.href = "/paste/" + id;}, 1000);
                    } else {
                        setTimeout(() => {window.location.href = "/paste/" + id + "?e=" + key + "&iv=" + iv;}, 1000);
                    }
                } else {
                    document.getElementById('save-button').style = 'display: block';
                }
            };
        }
        function goToHome() {
            window.location.href = "/";
        }
        function toggleEncryption() {
            if(document.getElementById("encryption-button").className == "material-button-toggle-on") {
                document.getElementById("encryption-button").className = "material-button-toggle";
                document.getElementById("encryption-button").getElementsByTagName("i")[0].className = "fa-solid fa-unlock";
                document.getElementById("encryption-button").getElementsByTagName("span")[0].innerText = "Link Encryption Disabled";
                document.getElementById("encryption-button").style = "color: #FF5F89";
                currentencryptionstatus = false;
                alert("Encryption turned off! Use this only for public pastes.");
            } else {
                document.getElementById("encryption-button").className = "material-button-toggle-on";
                document.getElementById("encryption-button").getElementsByTagName("i")[0].className = "fa-solid fa-lock";
                document.getElementById("encryption-button").getElementsByTagName("span")[0].innerText = "Link Encryption Enabled";
                document.getElementById("encryption-button").style = "";
                currentencryptionstatus = true;
                alert("Encryption turned on! Your paste is now secure.");
            }
        }
    </script>
</head>
<body style="overflow-y:hidden">
    <div class="material-editor-box">
        <div class="material-editor-topbar">
            <div class="material-editor-name-div"><i class="fa-solid fa-file"></i><input class="material-editor-name" type="text" id="name" placeholder="Here goes the paste name!" autocomplete="off" spellcheck="false" /></div>
            <button class="material-button-toggle" onclick="goToHome()"><i class="fa-solid fa-house"></i>Go back to Home</button>
            <button class="material-button-toggle" onclick="savePaste()" id="save-button"><i class="fa-solid fa-floppy-disk"></i>Save</button>
            <button class="material-button-toggle-on" id="encryption-button" onclick="toggleEncryption()"><i class="fa-solid fa-lock"></i><span>Link Encryption Enabled</span></button>
        </div>
        <div class="material-editor-textbox">
            <textarea placeholder="Enter some text to get started!" id="editor" spellcheck="false" autofocus="true" autocomplete="off"></textarea>
        </div>
    </div>
</body>
</html>