// Game Scene - Glavna igralna scena
class GameScene extends Phaser.Scene {
    constructor() {
        super({ key: 'GameScene' });
        this.terrainSystem = null;
        this.terrainContainer = null;
        this.player = null;
        this.npcs = []; // Array za NPCje
    }

    create() {
        console.log('🎮 GameScene: Initialized!');
        window.gameState.currentScene = 'GameScene';

        const width = this.cameras.main.width;
        const height = this.cameras.main.height;

        // Setup kamere
        this.cameras.main.setBackgroundColor('#1a1a2e');

        // Initialize Isometric Utils
        this.iso = new IsometricUtils();

        // Inicializiraj terrain sistem - 100x100 mapa
        console.log('🌍 Initializing terrain...');
        try {
            this.terrainSystem = new TerrainSystem(this, 100, 100);
            this.terrainSystem.generate();

            // Terrain offset
            this.terrainOffsetX = width / 2;
            this.terrainOffsetY = 100;

            // Initialization for culling
            this.terrainSystem.init(this.terrainOffsetX, this.terrainOffsetY);

            // Initial force update to render active tiles before first frame
            this.terrainSystem.updateCulling(this.cameras.main);

            // FAZA 14: Spawn Ruin (Town Project) at fixed location near player
            console.log('🏚️ Spawning Ruin...');
            this.terrainSystem.placeStructure(55, 55, 'ruin');
        } catch (e) {
            console.error("Terrain system failed:", e);
        }

        // Dodaj igralca
        console.log('👤 Initializing player...');
        this.player = new Player(this, 50, 50, this.terrainOffsetX, this.terrainOffsetY);

        // Dodaj 3 NPCje
        console.log('🧟 Initializing NPCs...');
        // Dodaj 3 NPCje (Mixed)
        console.log('🧟 Initializing NPCs...');
        const npcTypes = ['zombie', 'villager', 'merchant'];
        for (let i = 0; i < 3; i++) {
            const randomX = Phaser.Math.Between(40, 60); // Closer to center
            const randomY = Phaser.Math.Between(40, 60);
            const npc = new NPC(this, randomX, randomY, this.terrainOffsetX, this.terrainOffsetY, npcTypes[i]);
            this.npcs.push(npc);
        }

        // Dodaj 10 dodatnih Zombijev!
        for (let i = 0; i < 10; i++) {
            const randomX = Phaser.Math.Between(10, 90);
            const randomY = Phaser.Math.Between(10, 90);
            const zombie = new NPC(this, randomX, randomY, this.terrainOffsetX, this.terrainOffsetY, 'zombie');
            this.npcs.push(zombie);
        }

        // Kamera sledi igralcu z izboljšanimi nastavitvami
        this.cameras.main.startFollow(this.player.sprite, true, 1.0, 1.0); // Instant follow (was 0.1)

        // Nastavi deadzone (100px border)
        this.cameras.main.setDeadzone(100, 100);

        // Round pixels za crisp pixel art
        this.cameras.main.roundPixels = true;

        // Zoom out za boljši pogled (85% zoom)
        this.cameras.main.setZoom(0.85);

        // Parallax oblaki
        this.createClouds();

        // Kamera kontrole
        this.setupCamera();

        // Initialize Time & Stats
        console.log('⏳ Initializing Time & Stats...');
        this.timeSystem = new TimeSystem(this);
        this.timeSystem.create();

        this.statsSystem = new StatsSystem(this);
        this.inventorySystem = new InventorySystem(this);
        this.interactionSystem = new InteractionSystem(this);
        this.farmingSystem = new FarmingSystem(this);
        this.buildingSystem = new BuildingSystem(this);

        // Initialize Weather System
        console.log('🌦️ Initializing Weather System...');
        this.weatherSystem = new WeatherSystem(this);

        // Initialize Day/Night Cycle
        console.log('🌅 Initializing Day/Night System...');
        this.dayNightSystem = new DayNightSystem(this, this.timeSystem);

        // Initialize Sound Manager
        console.log('🎵 Initializing Sound Manager...');
        this.soundManager = new SoundManager(this);

        // Initialize Parallax System
        console.log('🌄 Initializing Parallax System...');
        this.parallaxSystem = new ParallaxSystem(this);

        // Launch UI Scene
        console.log('🖥️ Launching UI Scene...');
        this.scene.launch('UIScene');

        // Initialize Save System
        this.saveSystem = new SaveSystem(this);

        // Auto-load if available (optional, for now manual)
        // this.saveSystem.loadGame(); 

        console.log('✅ GameScene ready - FAZA 17!');
    }

    setupCamera() {
        const cam = this.cameras.main;

        // Zoom kontrole (Mouse Wheel)
        this.input.on('wheel', (pointer, gameObjects, deltaX, deltaY, deltaZ) => {
            const zoomSpeed = 0.001;
            const newZoom = Phaser.Math.Clamp(
                cam.zoom - deltaY * zoomSpeed,
                0.3,
                2.0
            );
            cam.setZoom(newZoom);
        });

        // Q/E za zoom
        this.zoomKeys = this.input.keyboard.addKeys({
            zoomIn: Phaser.Input.Keyboard.KeyCodes.Q,
            zoomOut: Phaser.Input.Keyboard.KeyCodes.E
        });

        // Save/Load Keys
        this.input.keyboard.on('keydown-F8', () => {
            // Save
            if (this.saveSystem) {
                this.saveSystem.saveGame();
                console.log('💾 Game Saved! (F8)');
            }
        });

        this.input.keyboard.on('keydown-F9', () => {
            // Load
            if (this.saveSystem) {
                this.saveSystem.loadGame();
                console.log('📂 Game Loaded! (F9)');
            }
        });

        // Build Mode Keys
        this.input.keyboard.on('keydown-B', () => {
            if (this.buildingSystem) this.buildingSystem.toggleBuildMode();
        });

        this.input.keyboard.on('keydown-ONE', () => {
            if (this.buildingSystem && this.buildingSystem.isBuildMode) this.buildingSystem.selectBuilding('fence');
            else if (this.scene.get('UIScene')) this.scene.get('UIScene').selectSlot(0);
        });
        this.input.keyboard.on('keydown-TWO', () => {
            if (this.buildingSystem && this.buildingSystem.isBuildMode) this.buildingSystem.selectBuilding('wall');
            else if (this.scene.get('UIScene')) this.scene.get('UIScene').selectSlot(1);
        });
        this.input.keyboard.on('keydown-THREE', () => {
            if (this.buildingSystem && this.buildingSystem.isBuildMode) this.buildingSystem.selectBuilding('house');
            else if (this.scene.get('UIScene')) this.scene.get('UIScene').selectSlot(2);
        });

        // Soft Reset (F4) - Force Reload Page
        this.input.keyboard.on('keydown-F4', () => {
            console.log('🔄 Soft Reset Initiated (Force Reload)...');
            window.location.reload();
        });

        // Mute Toggle (M key)
        this.input.keyboard.on('keydown-M', () => {
            if (this.soundManager) {
                this.soundManager.toggleMute();
            }
        });

    }

    update(time, delta) {
        // Update Systems
        if (this.timeSystem) this.timeSystem.update(delta);
        if (this.statsSystem) this.statsSystem.update(delta);
        if (this.interactionSystem) this.interactionSystem.update(delta);
        if (this.farmingSystem) this.farmingSystem.update(delta);
        if (this.dayNightSystem) this.dayNightSystem.update();
        if (this.weatherSystem) this.weatherSystem.update(delta);

        // Update Parallax (foreground grass fading)
        if (this.parallaxSystem && this.player) {
            const playerPos = this.player.getPosition();
            const screenPos = this.iso.toScreen(playerPos.x, playerPos.y);
            this.parallaxSystem.update(
                screenPos.x + this.terrainOffsetX,
                screenPos.y + this.terrainOffsetY
            );
        }

        // Update player
        if (this.player) {
            this.player.update(delta);
        }

        // Update NPCs
        for (const npc of this.npcs) {
            npc.update(delta);
        }

        // Update Terrain Culling
        if (this.terrainSystem) {
            this.terrainSystem.updateCulling(this.cameras.main);
        }

        // Update clouds
        if (this.clouds) {
            for (const cloud of this.clouds) {
                cloud.sprite.x += cloud.speed * (delta / 1000);
                if (cloud.sprite.x > this.terrainOffsetX + 2000) { // Reset far right
                    cloud.sprite.x = this.terrainOffsetX - 2000;
                    cloud.sprite.y = Phaser.Math.Between(0, 1000);
                }
            }
        }

        // Send debug info to UI Scene
        if (this.player) {
            const playerPos = this.player.getPosition();
            const cam = this.cameras.main;
            const visibleTiles = this.terrainSystem ? this.terrainSystem.visibleTiles.size : 0;

            const uiScene = this.scene.get('UIScene');
            if (uiScene && uiScene.debugText) {
                const activeCrops = this.terrainSystem && this.terrainSystem.cropsMap ? this.terrainSystem.cropsMap.size : 0;
                const dropsCount = this.interactionSystem && this.interactionSystem.drops ? this.interactionSystem.drops.length : 0;

                uiScene.debugText.setText(
                    `FAZA 11 - Building\n` +
                    `[F5] Save | [F9] Load | [B] Build Mode\n` +
                    `Time: ${this.timeSystem ? this.timeSystem.gameTime.toFixed(1) : '?'}h\n` +
                    `Active Crops: ${activeCrops}\n` +
                    `Loot Drops: ${dropsCount}\n` +
                    `Player: (${playerPos.x}, ${playerPos.y})`
                );
            }
        }
    }

    createClouds() {
        if (!this.textures.exists('cloud')) TextureGenerator.createCloudSprite(this, 'cloud');

        this.clouds = [];
        console.log('☁️ Creating parallax clouds...');
        for (let i = 0; i < 8; i++) {
            const x = Phaser.Math.Between(-1000, 3000);
            const y = Phaser.Math.Between(-500, 1500);

            const cloud = this.add.sprite(x, y, 'cloud');
            cloud.setAlpha(0.4);
            cloud.setScrollFactor(0.2); // Parallax effect
            cloud.setDepth(2000); // Nad vsem
            cloud.setScale(Phaser.Math.FloatBetween(2, 4)); // Veliki oblaki

            this.clouds.push({ sprite: cloud, speed: Phaser.Math.FloatBetween(10, 30) });
        }
    }
}
