<!DOCTYPE html>
<html>
<head>
    <title>Ήχος Βατράχου με Εύρος Συχνοτήτων (15.5 kHz - 19 kHz)</title>
    <style>
        #pauseButton {
            display: none; /* Απόκρυψη του κουμπιού Pause αρχικά */
        }
        #expiredMessage {
            display: none; /* Απόκρυψη μηνύματος λήξης αρχικά */
            color: red;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <h1>frog you (ultrasounds)</h1>
    <button id="playButton" onclick="playFrogSound()">Play</button>
    <button id="pauseButton" onclick="pauseFrogSound()">Pause</button>
    <p id="expiredMessage">The application has expired.</p>

    <script>
        let audioCtx;
        let oscillator;
        let gainNode;
        let isPlaying = false;
        let interval;
        const startTime = new Date().getTime(); // Χρόνος εκκίνησης
        const expirationTime = 48 * 60 * 60 * 1000; // 48 ώρες σε milliseconds
        let shared = false;

        function playFrogSound() {
            const currentTime = new Date().getTime();
            if (currentTime - startTime > expirationTime) {
                handleExpiration();
                return;
            }

            if (isPlaying) return; // Αν ήδη παίζει, δεν κάνουμε τίποτα
            isPlaying = true;

            // Εναλλαγή ορατότητας κουμπιών
            document.getElementById("playButton").style.display = "none";
            document.getElementById("pauseButton").style.display = "inline";

            audioCtx = new (window.AudioContext || window.webkitAudioContext)();

            // Λειτουργία αναπαραγωγής με επαναλαμβανόμενες διάρκειες και παύσεις
            interval = setInterval(() => {
                oscillator = audioCtx.createOscillator();
                gainNode = audioCtx.createGain();

                // Ρύθμιση του ήχου
                oscillator.type = 'sine'; // Κυματομορφή 'sine' για φυσικό ήχο
                // Τυχαία συχνότητα μεταξύ 15.5 kHz και 19 kHz
                oscillator.frequency.setValueAtTime(15500 + Math.random() * (19000 - 15500), audioCtx.currentTime);

                // Ρύθμιση του gain στο ελάχιστο για χαμηλά decibel
                gainNode.gain.setValueAtTime(0.01, audioCtx.currentTime); // Ελάχιστη ένταση

                oscillator.connect(gainNode);
                gainNode.connect(audioCtx.destination);

                // Εκκίνηση του ήχου
                oscillator.start();
                setTimeout(() => {
                    oscillator.stop(); // Διακοπή του ήχου μετά από 0.4 δευτερόλεπτα
                }, 400);
            }, 1400); // Επαναλαμβάνεται κάθε 1.4 δευτερόλεπτα (0.4 δευτερόλεπτα ήχος + 1 δευτερόλεπτο παύση)
        }

        function pauseFrogSound() {
            if (!isPlaying) return; // Αν δεν παίζει, δεν κάνουμε τίποτα
            isPlaying = false;

            // Εναλλαγή ορατότητας κουμπιών
            document.getElementById("playButton").style.display = "inline";
            document.getElementById("pauseButton").style.display = "none";

            clearInterval(interval); // Διακοπή της επανάληψης
            audioCtx.close(); // Κλείσιμο του AudioContext
            audioCtx = null;
        }

        function handleExpiration() {
            document.getElementById("playButton").style.display = "none";
            document.getElementById("pauseButton").style.display = "none";
            document.getElementById("expiredMessage").style.display = "block";
        }

        function detectDevice() {
            const userAgent = navigator.userAgent;
            if (/Mobi|Android|iPhone/i.test(userAgent)) {
                return 'mobile';
            }
            return 'desktop';
        }

        function checkDevice() {
            const deviceType = detectDevice();
            if (deviceType === 'desktop') {
                document.body.innerHTML = '<p>(When downloaded on a desktop, this program self-destructs completely)</p>';
            }
        }

        // Έλεγχος για share σε Facebook/Instagram
        function extendExpiration() {
            if (!shared) {
                shared = true;
                expirationTime += 24 * 60 * 60 * 1000; // Προσθήκη 24 ωρών
            }
        }

        checkDevice();
    </script>
</body>
</html>
