import { WaveManager } from '../support/waveManager.js';
import { TowerManager } from '../support/towerManager.js';

export class Level extends Phaser.Scene {
    constructor() {
        super({ key: 'Level' });
    }

    init(data) {

    }

    preload() {
        this.load.tilemapTiledJSON('level1', 'assets/level1.json');
        this.load.image('terrain', 'assets/terrain.png');
        this.load.spritesheet('ammo', 'assets/ammo.png', {
            frameWidth: 50,
            frameHeight: 50
        });
        this.load.spritesheet('basic-enemies', 'assets/basic-enemies.png', { 
            frameWidth: 50, 
            frameHeight: 50 
        });
        this.load.spritesheet('towers', 'assets/towers.png', {
            frameHeight: 150,
            frameWidth: 150
        });
    }

    create() {
        this.levelMap = this.make.tilemap({ key: 'level1' });
        const terrainTiles = this.levelMap.addTilesetImage('terrain', 'terrain');
        this.mainLayer = this.levelMap.createLayer('main', terrainTiles)
            .setCollisionByProperty({ collides: true });
        this.platformsLayer = this.levelMap.createLayer('platforms', terrainTiles);

        // Add camera zoom functionality
        this.cameras.main.setBounds(0, 0, 2000, 2000);
        this.addControls();

        this.waveManager = new WaveManager(this, 1, 1);
        this.towerManager = new TowerManager(this);

        this.scene.launch('UIScene');
        this.UIScene = this.scene.get('UIScene');

        this.enemies = this.physics.add.group();
        this.towers = this.physics.add.group();

        this.physics.add.collider(this.enemies, this.mainLayer);
        this.physics.add.collider(this.enemies, this.platformsLayer);
    }

    gridToLocation(num, offset = 0) {
        return num * 200 + 100 + offset;
    }

    update(time, delta) {
        this.waveManager.update(time, delta);
        this.towerManager.update(time, delta);
    }

    addControls() {
        // this.input.on('wheel', (pointer, gameObjects, deltaX, deltaY) => {
        //     const zoomSpeed = 0.1;
        //     if (deltaY < 0) {
        //         // Zoom in
        //         this.cameras.main.zoom += zoomSpeed;
        //     } else if (deltaY > 0) {
        //         // Zoom out
        //         this.cameras.main.zoom -= zoomSpeed;
        //     }
            
        //     // Limit zoom range to prevent extreme zoom levels
        //     this.cameras.main.zoom = Phaser.Math.Clamp(this.cameras.main.zoom, 0.5, 2);
            
        //     // Zoom toward mouse position
        //     const worldPoint = this.input.activePointer.positionToCamera(this.cameras.main);
        //     this.cameras.main.centerOn(worldPoint.x, worldPoint.y);
        // });

        // Add camera panning functionality
        this.input.on('pointerdown', (pointer) => {
            if (pointer.button === 0) { // Left mouse button
                this.isPanning = true;
                this.panStartX = pointer.x;
                this.panStartY = pointer.y;
            }
        });

        this.input.on('pointermove', (pointer) => {
            if (this.isPanning && pointer.isDown) {
                const dx = pointer.x - this.panStartX;
                const dy = pointer.y - this.panStartY;
                
                // Pan the camera
                this.cameras.main.scrollX -= dx;
                this.cameras.main.scrollY -= dy;
                
                // Update start position for next move
                this.panStartX = pointer.x;
                this.panStartY = pointer.y;
            }
        });

        this.input.on('pointerup', (pointer) => {
            if (pointer.button === 0) { // Left mouse button
                this.isPanning = false;
            }
        });
    }
}