export class CycleManager {
    constructor(scene) {
        this.scene = scene;
        this.currentCycle = 'day';
        this.cycleTimer = 0;
        this.cycleDuration = 120; // 3 minutes
        this.cycles = ['day', 'evening', 'night', 'morning'];
        
        // Cycle tint values
        this.tintValues = {
            'day': 0xffffff,      // White (no tint)
            'evening': 0xffb085,   // Orange tint
            'night': 0x222244,     // Dark blue tint
            'morning': 0xffffaa   // Light yellow tint
        };

        // Cycle tint values
        this.playerTintValues = {
            'day': 0xffffff,      // White (no tint)
            'evening': 0xffccaa,   // Orange tint
            'night': 0x8888ff,     // Dark blue tint
            'morning': 0xffffdd   // Light yellow tint
        };
        
        this.cycleText = null;
    }

    init() {
        // Create cycle display text in upper right corner
        this.cycleText = this.scene.add.text(
            40, 
            40, 
            'Day', 
            { fontSize: '24px', fill: '#ffffff' }
        ).setShadow(3,3, '#333', 5).setScrollFactor(0);
    }

    update(delta) {
        // Handle cycle timing
        this.cycleTimer += delta / 1000; // Convert ms to seconds

        if (this.cycleTimer >= this.cycleDuration) {
            this.nextCycle();
            this.cycleTimer = 0;
        }
    }

    nextCycle() {
        const currentIndex = this.cycles.indexOf(this.currentCycle);
        const nextIndex = (currentIndex + 1) % this.cycles.length;
        
        this.currentCycle = this.cycles[nextIndex];
        
        // Update display text
        if (this.cycleText) {
            this.cycleText.setText(this.currentCycle.charAt(0).toUpperCase() + this.currentCycle.slice(1));
        }
        
        // Apply tint to the terrain layer
        this.applyTint(this.scene.mainLayer);
    }

    applyTint(layer) {
        if (layer && this.tintValues[this.currentCycle]) {
            layer.setTint(this.tintValues[this.currentCycle]);
            this.scene.player.setTint(this.playerTintValues[this.currentCycle]);
            this.scene.objects.setTint(this.playerTintValues[this.currentCycle]);
        }
    }

    getCurrentCycle() {
        return this.currentCycle;
    }
}