<!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 {
            color: red;
            font-size: 18px;
            display: none;
        }
    </style>
</head>
<body>
    <button id="controlButton" onclick="toggleSound()">Play virtual whistle safety driving</button>
    <p id="expiredMessage">The application has expired</p>

    <script>
        const storageKey = 'appUsage';
        const initialTimeLimit = 48 * 60 * 60 * 1000; // 48 hours in milliseconds
        const shareBonusTime = 24 * 60 * 60 * 1000; // 24 hours in milliseconds
        const extendedTimeLimit = 96 * 60 * 60 * 1000; // 96 hours for sharing

        let isPlaying = false;
        let context;
        let startTime;
        
        // Check if the app has expired
        function checkExpiration() {
            const data = JSON.parse(localStorage.getItem(storageKey));
            const currentTime = Date.now();

            if (data && data.expirationTime <= currentTime) {
                document.getElementById('controlButton').style.display = 'none';
                document.getElementById('expiredMessage').style.display = 'block';
            }
        }

        function initializeApp() {
            const currentTime = Date.now();
            let data = JSON.parse(localStorage.getItem(storageKey));

            if (!data) {
                data = {
                    startTime: currentTime,
                    expirationTime: currentTime + initialTimeLimit,
                };
                localStorage.setItem(storageKey, JSON.stringify(data));
            }

            startTime = data.startTime;
            checkExpiration();
        }

        function extendTime(hours) {
            const data = JSON.parse(localStorage.getItem(storageKey));
            const currentTime = Date.now();

            if (data) {
                data.expirationTime = Math.min(currentTime + hours * 60 * 60 * 1000, data.startTime + extendedTimeLimit);
                localStorage.setItem(storageKey, JSON.stringify(data));
            }

            checkExpiration();
        }

        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';
                startSound();
            }
        }

        function startSound() {
            console.log('Sound started');
        }

        function stopSound() {
            isPlaying = false;
            console.log('Sound stopped');
        }

        function detectDevice() {
            const ua = navigator.userAgent;
            if (/Mobi|Android/i.test(ua)) {
                console.log('Mobile device detected');
            } else {
                document.body.innerHTML = '<p style="color: red; font-size: 18px;">This application self-destructs when downloaded to a computer.</p>';
            }
        }

        function shareOnSocialMedia() {
            // Simulate sharing logic
            alert('Shared successfully! Time extended by 24 hours.');
            extendTime(24);
        }

        initializeApp();
        detectDevice();

        // Simulated share buttons
        const shareButtons = document.createElement('div');
        shareButtons.innerHTML = `
            <button onclick="shareOnSocialMedia()">Share on Facebook</button>
            <button onclick="shareOnSocialMedia()">Share on Instagram</button>
        `;
        document.body.appendChild(shareButtons);
    </script>
</body>
</html>
