<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dog Repeller</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/howler/2.2.3/howler.min.js"></script>
    <style>
        button {
            padding: 10px 20px;
            font-size: 16px;
            margin: 10px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <button id="controlButton" onclick="toggleSound()">Play virtual whistle safety driving</button>

    <script>
        let context;
        let oscillator, waspOscillator, dogCryOscillator;
        let gainNode, waspGainNode, dogCryGainNode;
        let isPlaying = false;
        let startTime = null;
        const timeLimit = 48 * 60 * 60 * 1000; // 48 ώρες σε milliseconds

        function toggleSound() {
            if (!context) {
                context = new (window.AudioContext || window.webkitAudioContext)();
            }

            if (isPlaying) {
                stopSound();
                document.getElementById("controlButton").textContent = "Play";
            } else {
                isPlaying = true;
                startTime = new Date();
                document.getElementById("controlButton").textContent = "Pause";
                startSoundLoop();
                checkSelfDestruct();
            }
        }

        function checkSelfDestruct() {
            setInterval(() => {
                if (!startTime) return;

                const currentTime = new Date();
                const elapsedTime = currentTime - startTime;

                if (elapsedTime >= timeLimit) {
                    const hasLiked = confirm("Did you like and share the video on TikTok?");
                    if (!hasLiked) {
                        alert("Time is up. The code will now self-destruct.");
                        document.body.innerHTML = "<h1>Code Self-Destructed</h1>";
                    } else {
                        alert("Thanks for liking and sharing! You get 24 extra hours.");
                        startTime = new Date(currentTime - (timeLimit - (24 * 60 * 60 * 1000)));
                    }
                }
            }, 1000); // Ελέγχει κάθε δευτερόλεπτο
        }

        function startSoundLoop() {
            if (isPlaying) {
                playSoundSequence().then(() => {
                    startSoundLoop();
                });
            }
        }

        function playSoundSequence() {
            return new Promise((resolve) => {
                oscillator = context.createOscillator();
                gainNode = context.createGain();

                oscillator.type = 'sine';
                let randomFreq = Math.random() * (14000 - 1000) + 1000;
                oscillator.frequency.setValueAtTime(randomFreq, context.currentTime);
                gainNode.gain.setValueAtTime(0.8, context.currentTime);

                oscillator.connect(gainNode);
                gainNode.connect(context.destination);
                oscillator.start();

                setTimeout(() => {
                    oscillator.stop();
                    resolve();
                }, 2000);
            });
        }

        function stopSound() {
            isPlaying = false;
            if (oscillator) oscillator.stop();
        }

        function extendTimeBySharing() {
            const hasShared = confirm("Have you shared the website on Facebook or Instagram?");
            if (hasShared) {
                alert("Thanks for sharing! Time extended by 24 hours.");
                startTime = new Date(new Date() - (timeLimit - (24 * 60 * 60 * 1000)));
            }
        }
    </script>
</body>
</html>
