<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/howler/2.2.3/howler.min.js"></script> <!-- Βιβλιοθήκη Howler.js για διαχείριση ήχων -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css"> <!-- Εικονίδια FontAwesome -->
    <style>
        #pauseButton {
            display: none; /* Απόκρυψη κουμπιού pause από προεπιλογή */
        }

        #facebookShare {
            display: none; /* Απόκρυψη εικονιδίου Facebook από προεπιλογή */
            margin-left: 10px; /* Περιθώριο για ευθυγράμμιση */
        }

        #buttonContainer {
            display: flex; /* Τα κουμπιά ευθυγραμμίζονται σε σειρά */
            align-items: center; /* Κάθετη κεντροποίηση */
        }

        #facebookShare i {
            color: #3b5998; /* Μπλε χρώμα για το εικονίδιο Facebook */
            font-size: 24px; /* Μέγεθος εικονιδίου */
        }
    </style>
    <script>
        let soundTimeout; // Χρονόμετρο για επανάληψη ήχων
        let oscillator; // Αντικείμενο για τον ταλαντωτή ήχου
        let audioContext; // Αντικείμενο διαχείρισης ήχου
        let gainNode; // Ενίσχυση ήχου
        let isPlaying = false; // Σημαία που ελέγχει αν παίζουν οι ήχοι
        let hasPlayedScream = false; // Σημαία για το αν έχει παιχτεί ο πρώτος ήχος (κραυγή)

        // Δημιουργία ήχων μέσω της Howler.js
        const thunder = new Howl({ src: ['sounds/thunder.mp3'], loop: false, html5: true });
        const scream = new Howl({ src: ['sounds/scream.mp3'], loop: false, html5: true });
        const drums = new Howl({ src: ['sounds/drums.mp3'], loop: false, html5: true });
        const glassBreak = new Howl({ src: ['sounds/glass_break.mp3'], loop: false, html5: true });
        const catScream = new Howl({ src: ['sounds/cat_scream.mp3'], duration: 4, html5: true });
        const clapping = new Howl({ src: ['sounds/clapping.mp3'], loop: false, html5: true });

        const sounds = [thunder, scream, drums, glassBreak, clapping]; // Πίνακας για τυχαία αναπαραγωγή ήχων

        // Αναπαραγωγή πρώτου ήχου (κραυγή γάτας)
        function playCatScream() {
            catScream.play();
        }

        // Αναπαραγωγή τυχαίου ήχου από τον πίνακα
        function playRandomSound() {
            const randomIndex = Math.floor(Math.random() * sounds.length);
            sounds[randomIndex].play();
        }

        // Έλεγχος της έκδοσης WAAPI
        function checkWAAPIVersion() {
            try {
                audioContext = new (window.AudioContext || window.webkitAudioContext)();
                return 2; // Νεότερη έκδοση API
            } catch (e) {
                console.log("AudioContext not supported, falling back to older version.");
                return 1; // Παλαιότερη έκδοση API
            }
        }

        // Ξεκινά ταλαντωτής (έκδοση 2)
        function startOscillatorVersion2() {
            oscillator = audioContext.createOscillator();
            gainNode = audioContext.createGain();

            oscillator.connect(gainNode);
            gainNode.connect(audioContext.destination);
            oscillator.type = "sine";

            oscillator.frequency.setValueAtTime(1000, audioContext.currentTime);
            oscillator.frequency.linearRampToValueAtTime(4000, audioContext.currentTime + 1.5);
            oscillator.frequency.linearRampToValueAtTime(1000, audioContext.currentTime + 3);

            gainNode.gain.setValueAtTime(10, audioContext.currentTime);
            oscillator.start(audioContext.currentTime);
            oscillator.stop(audioContext.currentTime + 3);
        }

        // Ξεκινά ταλαντωτής (έκδοση 1)
        function startOscillatorVersion1() {
            oscillator = audioContext.createOscillator();
            gainNode = audioContext.createGain();

            oscillator.connect(gainNode);
            gainNode.connect(audioContext.destination);
            oscillator.type = "sine";

            oscillator.frequency.setValueAtTime(1000, audioContext.currentTime);
            gainNode.gain.setValueAtTime(5, audioContext.currentTime);
            oscillator.start(audioContext.currentTime);
            oscillator.stop(audioContext.currentTime + 3);
        }

        // Έναρξη ταλαντωτή
        function startOscillator() {
            const waapiVersion = checkWAAPIVersion();
            if (waapiVersion === 2) {
                startOscillatorVersion2();
            } else {
                startOscillatorVersion1();
            }
        }

        // Διαδικασία έναρξης ήχων
        function startProcess() {
            if (!isPlaying) return;

            startOscillator();

            if (!hasPlayedScream) {
                playCatScream();
                hasPlayedScream = true;
                setTimeout(() => {
                    startProcess();
                }, 4000); // Αναμονή για 4 δευτερόλεπτα
            } else {
                playRandomSound();

                soundTimeout = setTimeout(() => {
                    startProcess();
                }, 3100); // Αναμονή για 3.1 δευτερόλεπτα
            }
        }

        // Έναρξη όλων των ήχων
        function startSounds() {
            if (!isPlaying) {
                isPlaying = true;
                document.getElementById("playButton").style.display = "none";
                document.getElementById("pauseButton").style.display = "inline";

                if (audioContext && audioContext.state === 'suspended') {
                    audioContext.resume().then(() => {
                        console.log("AudioContext resumed");
                    });
                }

                startProcess();
            }
        }

        // Διακοπή όλων των ήχων
        function stopSounds() {
            if (isPlaying) {
                isPlaying = false;
                document.getElementById("pauseButton").style.display = "none";
                document.getElementById("playButton").style.display = "inline";
                clearTimeout(soundTimeout);

                if (oscillator) {
                    oscillator.stop();
                    oscillator = null;
                }

                sounds.forEach(sound => sound.stop());
                catScream.stop();

                document.getElementById("facebookShare").style.display = "inline";
            }
        }

        // Κοινοποίηση στο Facebook
        function shareOnFacebook() {
            const url = 'https://www.facebook.com/sharer/sharer.php?u=https://www.sonic-protector.com';
            window.open(url, '_blank');
        }

        // Ενημέρωση για ανανέωση συνδρομής μετά από 27 ημέρες
        function notifySubscriptionRenewal() {
            const currentDate = new Date();
            const renewalDate = new Date(currentDate.getTime() + 27 * 24 * 60 * 60 * 1000); // Σε 27 ημέρες

            const reminder = setTimeout(() => {
                alert("Σε 3 ημέρες πρέπει να ανανεώσετε τη συνδρομή σας!");
            }, 27 * 24 * 60 * 60 * 1000 - 3 * 24 * 60 * 60 * 1000); // Μήνυμα στις 27 - 3 = 24 ημέρες

            return { renewalDate, reminder };
        }

        // Αυτοκαταστροφή του κώδικα μετά από 30 ημέρες
        function selfDestructAfter30Days() {
            setTimeout(() => {
                document.body.innerHTML = ""; // Διαγραφή του περιεχομένου της σελίδας
                alert("Ο πηγαίος κώδικας διαγράφηκε.");
            }, 30 * 24 * 60 * 60 * 1000); // Σε 30 ημέρες
        }

        // Κλήση λειτουργιών κατά την εκκίνηση
        notifySubscriptionRenewal();
        selfDestructAfter30Days();

    </script>
</head>
<body>
    <div id="buttonContainer">
        <button id="playButton" onclick="startSounds()">Super Play</button>
        <button id="pauseButton" onclick="stopSounds()">Pause</button>
        <span id="facebookShare" onclick="shareOnFacebook()">
            <i class="fab fa-facebook"></i>
        </span>
    </div>
</body>
</html>
