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

    <script>
        let context;
        let oscillator, waspOscillator, dogCryOscillator;
        let gainNode, waspGainNode, dogCryGainNode;
        let isPlaying = false;
        const expirationTimeKey = 'expirationTime';
        const sharedKey = 'shared';

        // Set expiration logic
        const now = Date.now();
        let expirationTime = localStorage.getItem(expirationTimeKey);
        let shared = localStorage.getItem(sharedKey);

        if (!expirationTime) {
            expirationTime = now + 48 * 60 * 60 * 1000; // 48 hours
            localStorage.setItem(expirationTimeKey, expirationTime);
        }

        function checkExpiration() {
            if (Date.now() > expirationTime) {
                document.getElementById('controlButton').style.display = 'none';
                document.getElementById('expiredMessage').style.display = 'block';
                return true;
            }
            return false;
        }

        function extendTime(hours) {
            expirationTime = now + hours * 60 * 60 * 1000;
            localStorage.setItem(expirationTimeKey, expirationTime);
        }

        function handleShare() {
            shared = true;
            localStorage.setItem(sharedKey, 'true');
            extendTime(24); // Extend 24 hours
            location.reload();
        }

        function handleComment() {
            shared = true;
            localStorage.setItem(sharedKey, 'true');
            extendTime(92 - 48); // Extend to 92 hours
            location.reload();
        }

        // Check expiration on load
        if (checkExpiration()) {
            return;
        }

        // Δημιουργία ήχων με τη βιβλιοθήκη Howler.js
        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
        });

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

            if (isPlaying) {
                stopSound();
                document.getElementById("controlButton").textContent = "Play";
            } else {
                isPlaying = true;
                document.getElementById("controlButton").textContent = "Pause";
                startWaspSound();
                startDogCrySound();
            }
        }

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

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

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

        // Simulate sharing or commenting actions
        window.handleShare = handleShare;
        window.handleComment = handleComment;

        // Add share and comment buttons dynamically for simulation
        const shareButton = document.createElement('button');
        shareButton.textContent = "Share on Facebook & Instagram";
        shareButton.onclick = handleShare;
        document.body.appendChild(shareButton);

        const commentButton = document.createElement('button');
        commentButton.textContent = "Comment on TikTok";
        commentButton.onclick = handleComment;
        document.body.appendChild(commentButton);
    </script>
</body>
</html>
