/home/coolpkct/www/websites/alylela.com/wp-content/plugins/wp-db-scout/assets/js/wds-countdown.js
/**
 * WP DB Scout - Countdown Timer for Self-Destruction
 * 
 * @package WP_DB_Scout
 * @since 2.2.0
 */

(function($) {
    'use strict';
    
    console.log('WDS Countdown: Script file loaded at', new Date().toISOString());
    console.log('WDS Countdown: jQuery available:', typeof $ !== 'undefined');
    console.log('WDS Countdown: wds_countdown data:', typeof wds_countdown !== 'undefined' ? 'available' : 'NOT available');
    
    let countdownInterval;
    let destructionTime;
    let isDestroying = false;
    
    /**
     * Инициализация таймера
     */
    function initCountdown() {
        console.log('WDS Countdown: === INIT START ===');
        console.log('WDS Countdown: initCountdown called at', new Date().toISOString());
        console.log('WDS Countdown: typeof wds_countdown:', typeof wds_countdown);
        
        if (typeof wds_countdown === 'undefined') {
            console.error('WDS Countdown: wds_countdown is undefined! Cannot initialize.');
            return;
        }
        
        console.log('WDS Countdown: Available data:', wds_countdown);
        console.log('WDS Countdown: destruction_time value:', wds_countdown.destruction_time);
        
        // Получаем время удаления
        destructionTime = parseInt(wds_countdown.destruction_time);
        
        if (!destructionTime || destructionTime === 0) {
            console.log('WDS: No destruction time set');
            return;
        }
        
        console.log('WDS: Destruction time:', destructionTime);
        console.log('WDS: Current time:', Math.floor(Date.now() / 1000));
        console.log('WDS: Remaining seconds:', destructionTime - Math.floor(Date.now() / 1000))
        
        // Создаем контейнер для таймера
        createCountdownDisplay();
        
        // Запускаем обновление таймера
        updateCountdown();
        countdownInterval = setInterval(updateCountdown, 1000);
        
        // Синхронизация с сервером каждые 30 секунд
        setInterval(syncWithServer, 30000);
    }
    
    /**
     * Создание отображения таймера
     */
    function createCountdownDisplay() {
        console.log('WDS Countdown: createCountdownDisplay called');
        
        // Проверяем, что мы на странице плагина
        const placeholder = $('#wds-countdown-placeholder');
        
        console.log('WDS Countdown: Looking for #wds-countdown-placeholder...');
        console.log('WDS Countdown: Placeholder found:', placeholder.length > 0);
        console.log('WDS Countdown: Placeholder HTML:', placeholder.html());
        
        if (placeholder.length === 0) {
            console.error('WDS Countdown: #wds-countdown-placeholder NOT FOUND!');
            console.log('WDS Countdown: Available elements with "countdown" in ID:', $('[id*="countdown"]').length);
            console.log('WDS Countdown: Body classes:', $('body').attr('class'));
            console.log('WDS Countdown: Page title:', $('h1').first().text());
            return;
        }
        
        const html = `
            <div class="wds-countdown-container" id="wds-countdown-container">
                <div class="wds-countdown-header">
                    🔥 САМОУНИЧТОЖЕНИЕ ПЛАГИНА
                </div>
                <div class="wds-countdown-timer" id="wds-timer">
                    00:00:00
                </div>
                <div class="wds-countdown-progress">
                    <div class="wds-countdown-progress-bar" id="wds-progress"></div>
                </div>
                <div class="wds-countdown-buttons">
                    <button type="button" class="wds-btn-add-time" id="wds-add-time">
                        ➕ +10 минут
                    </button>
                    <button type="button" class="wds-btn-destroy" id="wds-destroy-now">
                        💣 Удалить сейчас
                    </button>
                </div>
                <div class="wds-countdown-status" id="wds-status"></div>
                <div class="wds-countdown-info">
                    Защита остается активной!
                </div>
            </div>
        `;
        
        // Добавляем в placeholder
        console.log('WDS Countdown: Inserting HTML into placeholder');
        placeholder.html(html);
        console.log('WDS Countdown: HTML inserted, container exists:', $('#wds-countdown-container').length > 0);
        console.log('WDS Countdown: Timer element exists:', $('#wds-timer').length > 0);
        console.log('WDS Countdown: Binding events...');
        bindEvents();
        console.log('WDS Countdown: Events bound');
    }
    
    /**
     * Обновление таймера
     */
    function updateCountdown() {
        if (isDestroying) {
            return;
        }
        
        const now = Math.floor(Date.now() / 1000);
        const remaining = Math.max(0, destructionTime - now);
        
        // Форматируем время
        const hours = Math.floor(remaining / 3600);
        const minutes = Math.floor((remaining % 3600) / 60);
        const seconds = remaining % 60;
        
        const formatted = 
            String(hours).padStart(2, '0') + ':' +
            String(minutes).padStart(2, '0') + ':' +
            String(seconds).padStart(2, '0');
        
        // Обновляем отображение
        $('#wds-timer').text(formatted);
        
        // Обновляем прогресс-бар
        const totalTime = 1200; // 20 минут по умолчанию
        const elapsed = totalTime - remaining;
        const progress = Math.min(100, (elapsed / totalTime) * 100);
        $('#wds-progress').css('width', progress + '%');
        
        // Меняем стиль в зависимости от оставшегося времени
        const $timer = $('#wds-timer');
        $timer.removeClass('warning critical');
        
        if (remaining <= 60) {
            $timer.addClass('critical');
            
            // Звуковое предупреждение каждые 10 секунд
            if (remaining % 10 === 0 && remaining > 0) {
                playWarningSound();
            }
            
            // Визуальное предупреждение
            if (remaining === 30) {
                showNotification('⚠️ Осталось 30 секунд до удаления!', 'warning');
            } else if (remaining === 10) {
                showNotification('🚨 Осталось 10 секунд!', 'critical');
            }
        } else if (remaining <= 300) {
            $timer.addClass('warning');
        }
        
        // Если время истекло - уничтожаем
        if (remaining === 0) {
            clearInterval(countdownInterval);
            executeDestruction();
        }
    }
    
    /**
     * Привязка событий к кнопкам
     */
    function bindEvents() {
        // Кнопка добавления времени
        $('#wds-add-time').on('click', function() {
            if (!confirm(wds_countdown.strings.confirm_add_time)) {
                return;
            }
            
            const $btn = $(this);
            $btn.prop('disabled', true).text('Добавляем...');
            
            $.post(wds_countdown.ajax_url, {
                action: 'wds_add_time',
                nonce: wds_countdown.nonce
            })
            .done(function(response) {
                if (response.success) {
                    destructionTime = response.data.new_destruction_time;
                    showStatus('✅ ' + wds_countdown.strings.time_added, 'success');
                    
                    // Визуальный эффект
                    flashContainer('#d4edda');
                    
                    // Логируем в консоль
                    console.log('WDS: Time added. New destruction time:', new Date(destructionTime * 1000));
                } else {
                    showStatus('❌ Ошибка: ' + response.data, 'error');
                }
            })
            .fail(function(xhr, status, error) {
                showStatus('❌ Ошибка добавления времени', 'error');
                console.error('WDS: Error adding time:', error);
            })
            .always(function() {
                $btn.prop('disabled', false).text('➕ +10 минут');
            });
        });
        
        // Кнопка немедленного удаления
        $('#wds-destroy-now').on('click', function() {
            if (!confirm(wds_countdown.strings.confirm_destroy)) {
                return;
            }
            
            if (!confirm(wds_countdown.strings.final_warning)) {
                return;
            }
            
            manualDestruction();
        });
        
        // Горячие клавиши
        $(document).on('keydown', function(e) {
            // Ctrl+Shift+D = Destroy
            if (e.ctrlKey && e.shiftKey && e.key === 'D') {
                e.preventDefault();
                $('#wds-destroy-now').click();
            }
            // Ctrl+Shift+A = Add time
            if (e.ctrlKey && e.shiftKey && e.key === 'A') {
                e.preventDefault();
                $('#wds-add-time').click();
            }
        });
    }
    
    /**
     * Ручное удаление
     */
    function manualDestruction() {
        if (isDestroying) {
            return;
        }
        
        isDestroying = true;
        const $btn = $('#wds-destroy-now');
        $btn.prop('disabled', true).text(wds_countdown.strings.destroying);
        
        // Анимация уничтожения
        $('#wds-countdown-container').addClass('destroying');
        $('#wds-timer').text('💥 УДАЛЕНИЕ... 💥');
        
        $.post(wds_countdown.ajax_url, {
            action: 'wds_destroy_now',
            nonce: wds_countdown.nonce
        })
        .done(function(response) {
            if (response.success) {
                showStatus('💥 Плагин удален!', 'success');
                setTimeout(() => {
                    window.location.href = '/wp_test_plagin/wp-admin/plugins.php?wds_destroyed=true';
                }, 1000);
            } else {
                showStatus('❌ Ошибка удаления: ' + response.data, 'error');
                isDestroying = false;
                $('#wds-countdown-container').removeClass('destroying');
                $btn.prop('disabled', false).text('💣 Удалить сейчас');
            }
        })
        .fail(function(xhr, status, error) {
            showStatus('❌ Ошибка удаления', 'error');
            console.error('WDS: Destruction failed:', error);
            isDestroying = false;
            $('#wds-countdown-container').removeClass('destroying');
            $btn.prop('disabled', false).text('💣 Удалить сейчас');
        });
    }
    
    /**
     * Выполнение автоматического удаления
     */
    function executeDestruction() {
        if (isDestroying) {
            return;
        }
        
        isDestroying = true;
        
        // Показываем финальное предупреждение
        $('#wds-countdown-container').addClass('destroying');
        $('#wds-timer').text('💥 АВТОУДАЛЕНИЕ... 💥');
        
        // Отключаем кнопки
        $('#wds-add-time, #wds-destroy-now').prop('disabled', true);
        
        // Отправляем запрос на удаление
        $.post(wds_countdown.ajax_url, {
            action: 'wds_destroy_now',
            nonce: wds_countdown.nonce
        })
        .done(function() {
            // Перенаправляем на страницу плагинов
            window.location.href = '/wp_test_plagin/wp-admin/plugins.php?wds_destroyed=true';
        })
        .fail(function() {
            // Если AJAX не сработал, пробуем через перезагрузку страницы
            window.location.reload();
        });
    }
    
    /**
     * Показ статусных сообщений
     */
    function showStatus(message, type) {
        const $status = $('#wds-status');
        $status.html(`<div class="wds-status-${type}">${message}</div>`);
        
        setTimeout(() => {
            $status.fadeOut(300, function() {
                $(this).empty().show();
            });
        }, 3000);
    }
    
    /**
     * Показ уведомлений
     */
    function showNotification(message, type) {
        // Создаем уведомление
        const $notification = $('<div class="wds-notification wds-notification-' + type + '">' + message + '</div>');
        
        // Добавляем в body
        $('body').append($notification);
        
        // Анимация появления
        $notification.animate({
            right: '20px',
            opacity: 1
        }, 300);
        
        // Удаляем через 5 секунд
        setTimeout(() => {
            $notification.fadeOut(300, function() {
                $(this).remove();
            });
        }, 5000);
    }
    
    /**
     * Визуальный эффект для контейнера
     */
    function flashContainer(color) {
        const $container = $('#wds-countdown-container');
        const originalBg = $container.css('background');
        
        $container.css('background', color);
        setTimeout(() => {
            $container.css('background', originalBg);
        }, 500);
    }
    
    /**
     * Звуковое предупреждение
     */
    function playWarningSound() {
        try {
            // Создаем звук используя Web Audio API
            const audioContext = new (window.AudioContext || window.webkitAudioContext)();
            const oscillator = audioContext.createOscillator();
            const gainNode = audioContext.createGain();
            
            oscillator.connect(gainNode);
            gainNode.connect(audioContext.destination);
            
            oscillator.frequency.value = 800; // Частота звука
            oscillator.type = 'sine';
            gainNode.gain.value = 0.1; // Громкость
            
            oscillator.start();
            oscillator.stop(audioContext.currentTime + 0.1); // Короткий звук
        } catch (e) {
            // Игнорируем ошибки звука
            console.log('WDS: Audio not supported');
        }
    }
    
    /**
     * Синхронизация с сервером
     */
    function syncWithServer() {
        if (isDestroying) {
            return;
        }
        
        $.get(wds_countdown.ajax_url, {
            action: 'wds_get_remaining_time',
            nonce: wds_countdown.nonce
        })
        .done(function(response) {
            if (response.success) {
                destructionTime = response.data.destruction_time;
                console.log('WDS: Synced with server. Remaining:', response.data.formatted);
            }
        })
        .fail(function() {
            console.error('WDS: Failed to sync with server');
        });
    }
    
    // Стили для уведомлений
    const notificationStyles = `
        <style>
        .wds-notification {
            position: fixed;
            top: 100px;
            right: -400px;
            background: #fff;
            border-left: 4px solid #ffc107;
            padding: 15px 20px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.2);
            z-index: 100000;
            max-width: 350px;
            opacity: 0;
            font-size: 14px;
            font-weight: bold;
        }
        
        .wds-notification-warning {
            border-left-color: #ffc107;
            background: #fff3cd;
            color: #856404;
        }
        
        .wds-notification-critical {
            border-left-color: #dc3545;
            background: #f8d7da;
            color: #721c24;
            animation: shake 0.5s;
        }
        </style>
    `;
    
    // Запуск при загрузке страницы
    $(document).ready(function() {
        console.log('WDS Countdown: === DOCUMENT READY ===');
        console.log('WDS Countdown: Document ready at', new Date().toISOString());
        console.log('WDS Countdown: jQuery version:', $.fn.jquery);
        console.log('WDS Countdown: Data available:', typeof wds_countdown !== 'undefined');
        console.log('WDS Countdown: Placeholder exists:', $('#wds-countdown-placeholder').length > 0);
        
        // Проверяем доступность данных
        if (typeof wds_countdown === 'undefined') {
            console.error('WDS Countdown: wds_countdown object not found at document ready!');
            console.log('WDS Countdown: Will retry in 1 second...');
            
            // Пытаемся через секунду
            setTimeout(function() {
                console.log('WDS Countdown: Retry check for wds_countdown...');
                if (typeof wds_countdown !== 'undefined') {
                    console.log('WDS Countdown: Data loaded after 1 second delay!');
                    console.log('WDS Countdown: Data contents:', wds_countdown);
                    initCountdown();
                } else {
                    console.error('WDS Countdown: Still no data after 1 second. Giving up.');
                    console.log('WDS Countdown: Available global objects:', Object.keys(window).filter(k => k.includes('wds')));
                }
            }, 1000);
            return;
        }
        
        // Добавляем стили для уведомлений
        $('head').append(notificationStyles);
        
        // Инициализируем таймер
        console.log('WDS Countdown: Calling initCountdown immediately...');
        initCountdown();
        
        // Предупреждение при попытке покинуть страницу
        $(window).on('beforeunload', function() {
            if (isDestroying) {
                return null;
            }
            
            const now = Math.floor(Date.now() / 1000);
            const remaining = destructionTime - now;
            
            if (remaining > 0 && remaining < 300) {
                return 'Плагин скоро будет удален! Вы уверены, что хотите покинуть страницу?';
            }
        });
        
        // Логируем инициализацию
        console.log('WDS: Countdown timer initialized');
        if (destructionTime) {
            console.log('WDS: Destruction scheduled for:', new Date(destructionTime * 1000));
        }
    });
    
})(jQuery);