export class ObjectManager {
    constructor(scene) {
        this.scene = scene;
        this.objects = [];
    }

    init() {
        // Parse main layer for object properties and create objects
        this.parseLayerForObjects();
    }

    parseLayerForObjects() {
        const map = this.scene.mainLayer.tilemap;
        const layerData = this.scene.mainLayer.layer;

        // Iterate through all tiles in the layer
        for (let y = 0; y < layerData.height; y++) {
            for (let x = 0; x < layerData.width; x++) {
                const tile = this.scene.mainLayer.getTileAt(x, y);
                
                if (tile && tile.properties) {
                    // Check for object-related properties
                    this.createObjectFromTile(tile, x*100+50, y*100+50);
                }
            }
        }
    }

    createObjectFromTile(tile, x, y) {
        // Example: Look for a "type" property to determine what object to create
        const objectType = tile.properties;

        Object.keys(objectType).forEach(key => {
            const rand = Phaser.Math.Between(1,300);
            
            if (key === 'palmTree' && rand <= objectType[key]) {
                const palmTree = this.scene.physics.add.sprite(x, y, 'objects', 0);
                this.scene.objects.add(palmTree);
                palmTree.setImmovable(true).setSize(60,100);
            } else if (key === 'forestTree' && rand <= objectType[key]) {
                const forestRand = Phaser.Math.Between(1,4);
                const forestTree = this.scene.physics.add.sprite(x, y, 'objects', forestRand);
                this.scene.objects.add(forestTree);
                forestTree.setImmovable(true).setSize(50,100);
            } else if (key === 'boulder' && rand <= objectType[key]) {
                const boulder = this.scene.physics.add.sprite(x, y, 'objects', 20);
                this.scene.objects.add(boulder);
                boulder.setImmovable(true);
            }
        });
        
    }

    createEnemy(x, y, properties) {
        // Placeholder for enemy creation logic
        console.log(`Creating enemy at (${x}, ${y}) with properties:`, properties);
        
        // Example implementation:
        // const enemy = this.scene.physics.add.sprite(
        //     x * tileWidth + tileWidth / 2,
        //     y * tileHeight + tileHeight / 2,
        //     'enemy-sprite'
        // );
        // this.objects.push(enemy);
    }

    createCollectible(x, y, properties) {
        // Placeholder for collectible creation logic
        console.log(`Creating collectible at (${x}, ${y}) with properties:`, properties);
        
        // Example implementation:
        // const collectible = this.scene.physics.add.sprite(
        //     x * tileWidth + tileWidth / 2,
        //     y * tileHeight + tileHeight / 2,
        //     'collectible-sprite'
        // );
        // this.objects.push(collectible);
    }

    createPlatform(x, y, properties) {
        // Placeholder for platform creation logic
        console.log(`Creating platform at (${x}, ${y}) with properties:`, properties);
        
        // Example implementation:
        // const platform = this.scene.physics.add.sprite(
        //     x * tileWidth + tileWidth / 2,
        //     y * tileHeight + tileHeight / 2,
        //     'platform-sprite'
        // );
        // platform.setImmovable(true);
        // this.objects.push(platform);
    }

    update(delta) {
        // Update all created objects
        this.objects.forEach(object => {
            if (object.update) {
                object.update(delta);
            }
        });
    }
}