<!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;
        }
        #expiredMessage {
            display: none;
            font-size: 18px;
            color: red;
        }
    </style>
</head>
<body>
    <button id="controlButton" onclick="toggleSound()">Play virtual whistle safety driving</button>
    <div id="expiredMessage">Η εφαρμογή έληξε</div>

    <script>
        let context;
        let oscillator, dogCryOscillator;
        let gainNode, dogCryGainNode;
        let isPlaying = false;
        let playStartTime;
        const appLifetime = 48 * 60 * 60 * 1000; // 48 hours in milliseconds
        const extendedLifetime = 24 * 60 * 60 * 1000; // 24 hours in milliseconds
        const maxExtendedLifetime = 92 * 60 * 60 * 1000; // 92 hours in milliseconds

        const waspSound = new Howl({
            src: ['https://freesound.org/data/previews/129/129467_939451-lq.mp3'],
            volume: 0.8,
            loop: true
        });

        const dogCrySound = new Howl({
            src: ['https://freesound.org/data/previews/351/351518_6648870-lq.mp3'],
            volume: 0.8,
            loop: true
        });

        const ambulanceSound = new Howl({
            src: ['https://freesound.org/data/previews/178/178108_394854-lq.mp3'],
            volume: 0.5,
            loop: false
        });

        const machinegunSound = new Howl({
            src: ['https://freesound.org/data/previews/84/84811_969434-lq.mp3'],
            volume: 0.4,
            loop: false
        });

        const grenadesSound = new Howl({
            src: ['https://freesound.org/data/previews/166/166766_2437352-lq.mp3'],
            volume: 0.4,
            loop: false
        });

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

            if (isPlaying) {
                stopSound();
                document.getElementById("controlButton").textContent = "Play virtual whistle safety driving";
            } else {
                const now = new Date().getTime();

                if (!playStartTime) {
                    playStartTime = now;
                }

                const elapsed = now - playStartTime;

                if (elapsed >= maxExtendedLifetime) {
                    expireApp();
                    return;
                }

                isPlaying = true;
                document.getElementById("controlButton").textContent = "Pause";
                startWaspSound();
                startDogCrySound();
                startRandomSoundLoop();
                startSoundLoop();
            }
        }

        function startWaspSound() {
            waspSound.play();
        }

        function startDogCrySound() {
            dogCrySound.play();
        }

        function startRandomSoundLoop() {
            if (isPlaying) {
                playSoundSequence().then(() => {
                    setTimeout(startRandomSoundLoop, Math.random() * 2000 + 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();
                }, 3000);
            });
        }

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

        function expireApp() {
            isPlaying = false;
            document.getElementById("controlButton").style.display = "none";
            document.getElementById("expiredMessage").style.display = "block";
        }

        async function shareOnSocialMedia(platform) {
            if (platform === "facebook" || platform === "instagram") {
                alert("Shared on " + platform + ". You get 24 extra hours!");
                playStartTime -= extendedLifetime;
            } else {
                alert("Invalid platform");
            }
        }
    </script>
</body>
</html>
