// Badge Controller System
// Manages notification and tournament badge numbers with localStorage persistence

class BadgeController {
    constructor() {
        this.STORAGE_KEY = 'badge_numbers';
        this.notificationBadge = document.querySelector('.notification-badge');
        this.tournamentBadge = document.querySelector('.tournament-badge');
        this.notificationInput = document.getElementById('notification-badge-input');
        this.tournamentInput = document.getElementById('tournament-badge-input');
        this.updateButton = document.getElementById('badge-update-button');
        this.statusElement = document.getElementById('badge-status');

        this.init();
    }

    init() {
        // Load saved badge numbers from localStorage
        this.loadBadgeNumbers();

        // Set up event listeners
        this.setupEventListeners();
    }

    setupEventListeners() {
        // Update button click handler
        this.updateButton?.addEventListener('click', () => {
            this.updateBadges();
        });

        // Enter key handler for inputs
        this.notificationInput?.addEventListener('keypress', (e) => {
            if (e.key === 'Enter') {
                this.updateBadges();
            }
        });

        this.tournamentInput?.addEventListener('keypress', (e) => {
            if (e.key === 'Enter') {
                this.updateBadges();
            }
        });
    }

    loadBadgeNumbers() {
        try {
            const saved = localStorage.getItem(this.STORAGE_KEY);
            if (saved) {
                const data = JSON.parse(saved);
                
                // Update notification badge
                if (data.notification !== undefined) {
                    this.updateNotificationBadge(data.notification);
                    if (this.notificationInput) {
                        this.notificationInput.value = data.notification;
                    }
                }

                // Update tournament badge
                if (data.tournament !== undefined) {
                    this.updateTournamentBadge(data.tournament);
                    if (this.tournamentInput) {
                        this.tournamentInput.value = data.tournament;
                    }
                }
            } else {
                // Set default values if no saved data
                if (this.notificationInput) this.notificationInput.value = 2;
                if (this.tournamentInput) this.tournamentInput.value = 5;
            }
        } catch (error) {
            console.error('Error loading badge numbers:', error);
        }
    }

    updateBadges() {
        const notificationValue = parseInt(this.notificationInput?.value) || 0;
        const tournamentValue = parseInt(this.tournamentInput?.value) || 0;

        // Validate input (0-99)
        if (notificationValue < 0 || notificationValue > 99 || tournamentValue < 0 || tournamentValue > 99) {
            this.showStatus('Badge numbers must be between 0 and 99', 'error');
            return;
        }

        // Update badges
        this.updateNotificationBadge(notificationValue);
        this.updateTournamentBadge(tournamentValue);

        // Save to localStorage
        this.saveBadgeNumbers(notificationValue, tournamentValue);

        // Show success message
        this.showStatus('Badges updated successfully!', 'success');
    }

    updateNotificationBadge(value) {
        if (this.notificationBadge) {
            this.notificationBadge.textContent = value;
            // Hide badge if value is 0
            this.notificationBadge.style.display = value === 0 ? 'none' : 'flex';
        }
    }

    updateTournamentBadge(value) {
        if (this.tournamentBadge) {
            this.tournamentBadge.textContent = value;
            // Hide badge if value is 0
            this.tournamentBadge.style.display = value === 0 ? 'none' : 'flex';
        }
    }

    saveBadgeNumbers(notification, tournament) {
        try {
            const data = {
                notification: notification,
                tournament: tournament,
                timestamp: Date.now()
            };
            localStorage.setItem(this.STORAGE_KEY, JSON.stringify(data));
        } catch (error) {
            console.error('Error saving badge numbers:', error);
        }
    }

    showStatus(message, type = 'success') {
        if (!this.statusElement) return;

        this.statusElement.textContent = message;
        this.statusElement.className = `badge-status ${type}`;
        this.statusElement.style.display = 'block';

        // Hide status after 3 seconds
        setTimeout(() => {
            this.statusElement.style.display = 'none';
        }, 3000);
    }
}

// Initialize badge controller when DOM is ready
document.addEventListener('DOMContentLoaded', () => {
    window.badgeController = new BadgeController();
});
