export class Clock {
    constructor(clockElement, greetingElement) {
        this.clockElement = clockElement;
        this.greetingElement = greetingElement;
        this.interval = null;
    }

    start() {
        this.update();
        this.interval = setInterval(() => this.update(), 1000);
    }

    stop() {
        if (this.interval) clearInterval(this.interval);
    }

    update() {
        const now = new Date();
        this.clockElement.textContent = now.toLocaleTimeString('en-US', { 
            hour12: false,
            hour: '2-digit',
            minute: '2-digit'
        });
        this.updateGreeting();
    }

    updateGreeting() {
        this.greetingElement.textContent = this.getGreeting();
    }

    getGreeting() {
        const hour = new Date().getHours();
        let greeting = '';
        
        if (hour >= 5 && hour < 12) greeting = 'Good morning';
        else if (hour >= 12 && hour < 18) greeting = 'Good afternoon';
        else if (hour >= 18 && hour < 22) greeting = 'Good evening';
        else greeting = 'Good night';

        const name = localStorage.getItem('username');
        return name ? `${greeting}, ${name}` : greeting;
    }
}
