export class InventoryManager {
    constructor(scene) {
        this.scene = scene;
        this.isOpen = false;
        this.inventory = {};
        this.backpackIcon = null;
        this.inventoryUI = null;
        this.closeButton = null;
    }

    init() {
        const cam = this.scene.cameras.main;
        this.backpackIcon = this.scene.physics.add.image(cam.worldView.width-150, cam.worldView.height-150, 'backpack')
            .setOrigin(0.5)
            .setScale(.5)
            .setScrollFactor(0)
            .setDepth(99);
        
        // Create inventory UI (hidden initially)
        this.createInventoryUI();
        
        // Add click event to toggle inventory
        this.backpackIcon.setInteractive();
        this.backpackIcon.on('pointerdown', () => {
            console.log('hit');
            this.toggleInventory();
        });
    }

    createInventoryUI() {
        const scene = this.scene;
        
        // Create inventory background
        this.inventoryUI = scene.add.rectangle(
            scene.game.config.width / 2,
            scene.game.config.height / 2,
            400,
            300,
            0x2F4F4F
        ).setOrigin(0.5).setAlpha(0.9).setScrollFactor(0).setDepth(150);
        
        // Create close button
        this.closeButton = scene.add.rectangle(
            scene.game.config.width / 2 + 180,
            scene.game.config.height / 2 - 130,
            30,
            30,
            0xFF4444
        ).setOrigin(0.5).setScrollFactor(0).setDepth(150);
        
        // Add X to close button
        this.closeButtonText = scene.add.text(
            scene.game.config.width / 2 + 180,
            scene.game.config.height / 2 - 130,
            'X',
            { fontSize: '24px', fill: '#FFFFFF' }
        ).setOrigin(0.5).setScrollFactor(0).setDepth(150);
        
        // Hide inventory UI initially
        this.inventoryUI.setVisible(false);
        this.closeButton.setVisible(false);
        this.closeButtonText.setVisible(false);
        
        // Add click event to close button
        this.closeButton.setInteractive();
        this.closeButton.on('pointerdown', () => {
            this.toggleInventory();
        });
    }

    toggleInventory() {
        this.isOpen = !this.isOpen;
        
        if (this.isOpen) {
            this.scene.player.stop();
            this.scene.player.canMove = false;
            // Show inventory UI and close button
            this.inventoryUI.setVisible(true);
            this.closeButton.setVisible(true);
            this.closeButtonText.setVisible(true);
        } else {
            // Hide inventory UI and close button
            this.inventoryUI.setVisible(false);
            this.closeButton.setVisible(false);
            this.closeButtonText.setVisible(false);
            this.scene.time.delayedCall(200, () => {
                this.scene.player.canMove = true;
            });
        }
    }

    addItem(item) {
        const type = item.props.type;
        const amount = item.props.amount;
        if (type in this.inventory) {
            this.inventory[type] += amount;
        } else {
            this.inventory[type] = amount;
        }
        this.scene.tweens.add({
            targets: this.backpackIcon,
            angle: 360,
            delay: 500,
            duration: 500
        });
        this.scene.tweens.add({
            targets: this.backpackIcon,
            scale: 0.8,
            delay: 500,
            yoyo: true,
            ease: 'Bounce',
            duration: 500
        });
    }

    removeItem(item) {
        const index = this.inventory.indexOf(item);
        if (index > -1) {
            this.inventory.splice(index, 1);
        }
    }

    update(delta) {
        // Inventory update logic can go here
    }
}