class WeatherSystem {
    constructor(scene) {
        this.scene = scene;
        this.currentWeather = 'clear'; // clear, rain, storm, fog
        this.weatherDuration = 0;
        this.maxWeatherDuration = 30000; // 30s per weather cycle

        // Weather effects containers
        this.rainParticles = [];
        this.overlay = null;

        this.init();
    }

    init() {
        // No local overlay - we use UI Scene
        this.changeWeather();
    }

    getOverlay() {
        const uiScene = this.scene.scene.get('UIScene');
        if (uiScene && uiScene.weatherGraphics) {
            return uiScene.weatherGraphics;
        }
        return null;
    }

    changeWeather() {
        // Clean up old weather
        this.clearWeather();

        // Random new weather
        const weathers = ['clear', 'clear', 'rain', 'fog']; // Clear more common
        this.currentWeather = weathers[Math.floor(Math.random() * weathers.length)];
        this.weatherDuration = 0;

        console.log(`🌦️ Weather changed to: ${this.currentWeather}`);

        // Apply new weather effects
        this.applyWeather();
    }

    applyWeather() {
        if (this.currentWeather === 'rain') {
            this.startRain();
            // Play rain sound
            if (this.scene.soundManager) {
                this.scene.soundManager.playRainSound();
            }
        } else if (this.currentWeather === 'fog') {
            this.applyFog();
        } else if (this.currentWeather === 'storm') {
            this.startRain(true);
            // Play rain sound (louder?)
            if (this.scene.soundManager) {
                this.scene.soundManager.playRainSound();
            }
        } else {
            // Clear weather - stop ambient sounds
            if (this.scene.soundManager) {
                this.scene.soundManager.stopRainSound();
            }
        }
    }

    startRain(heavy = false) {
        const width = this.scene.scale.width;
        const height = this.scene.scale.height;

        // Create rain drops (keep logic for drops)
        const dropCount = heavy ? 150 : 100;

        for (let i = 0; i < dropCount; i++) {
            const drop = {
                x: Math.random() * width,
                y: Math.random() * height,
                speed: heavy ? Phaser.Math.Between(400, 600) : Phaser.Math.Between(200, 400),
                length: heavy ? Phaser.Math.Between(10, 15) : Phaser.Math.Between(5, 10)
            };
            this.rainParticles.push(drop);
        }

        // No drawing here - handled in renderWeather
    }

    applyFog() {
        // No drawing here - handled in renderWeather
    }

    clearWeather() {
        this.rainParticles = [];
        // No clearing here - handled by DayNightSystem clearing the frame
    }

    update(delta) {
        this.weatherDuration += delta;

        // Change weather periodically
        if (this.weatherDuration > this.maxWeatherDuration) {
            this.changeWeather();
        }

        // Always render active weather
        if (this.currentWeather !== 'clear') {
            this.renderWeather(delta);
        }
    }

    renderWeather(delta) {
        const overlay = this.getOverlay();
        if (!overlay) return;

        const height = this.scene.scale.height;
        const width = this.scene.scale.width;

        // FOG
        if (this.currentWeather === 'fog') {
            overlay.fillStyle(0xcccccc, 0.4);
            overlay.fillRect(0, 0, width, height);
            return;
        }

        // RAIN / STORM
        if (this.currentWeather === 'rain' || this.currentWeather === 'storm') {
            // Update physics
            for (const drop of this.rainParticles) {
                drop.y += (drop.speed * delta) / 1000;
                if (drop.y > height) {
                    drop.y = -10;
                    drop.x = Math.random() * width;
                }
            }

            // Darken background for storm/rain (additive to DayNight)
            const isDark = this.currentWeather === 'storm' ? 0.3 : 0.2;
            overlay.fillStyle(0x000033, isDark);
            overlay.fillRect(0, 0, width, height);

            // Draw drops
            overlay.lineStyle(1, 0x88aaff, 0.5);
            for (const drop of this.rainParticles) {
                overlay.beginPath();
                overlay.moveTo(drop.x, drop.y);
                overlay.lineTo(drop.x - 2, drop.y + drop.length);
                overlay.strokePath();
            }
        }
    }

    getCurrentWeather() {
        return this.currentWeather;
    }

    setWeather(weather) {
        this.currentWeather = weather;
        this.weatherDuration = 0;
        this.applyWeather();
    }
}
