export class NNBoy extends Phaser.GameObjects.Sprite {
    constructor(scene, x, y, texture, frame) {
        super(scene, x, y, texture, frame);

        // Add the enemy to the scene
        scene.add.existing(this);

        // Enable physics for the enemy (if using Arcade Physics)
        scene.physics.world.enable(this);

        this.speed = 100;
        this.isFollowing = false;
        this.onPatrol = false;
        this.leftBoundary = 0;
        this.rightBoundary = 0;
        this.topBoundary = 0;
        this.bottomBoundary = 0;

        this.shoots = false;
        this.shootTimer = 0;
        this.bottleSpeed = 400;
        this.bottleReloadVariance = 5000;
        this.bottleReloadCalc = 3000;
        this.bottleReload = 0;
        this.bulletsGroup = scene.add.group();
        this.dropHeartOneIn = 3;

        // Create animations
        this.anims.create({
            key: 'walk99',
            frames: this.anims.generateFrameNumbers('99boy-tiles', { start: frame, end: frame+1 }),
            frameRate: 5,
            repeat: -1
        });
    }

    setPatrolX(leftBoundary, rightBoundary) {
        this.onPatrol = true;
        this.leftBoundary = leftBoundary;
        this.rightBoundary = rightBoundary;
    }

    setPatrolY(topBoundary, bottomBoundary) {
        this.onPatrol = true;
        this.topBoundary = topBoundary;
        this.bottomBoundary = bottomBoundary;
    }

    setSpeed(spd) {
        this.speed = spd;
    }

    // Example: Basic patrolling movement
    patrol() {
        // Horizontal boundaries
        if (this.leftBoundary !== 0) {
            if (this.body.blocked.left || this.body.x <= this.leftBoundary) {
                this.flipX = true;
                this.body.setVelocityX(this.speed);
            } else if (this.body.blocked.right || this.body.x >= this.rightBoundary) {
                this.flipX = false;
                this.body.setVelocityX(-this.speed);
            }
        }

        // Vertical boundaries
        if (this.topBoundary !== 0) {
            if (this.body.blocked.up || this.body.y <= this.topBoundary) {
                this.body.setVelocityY(this.speed);
            } else if (this.body.blocked.down || this.body.y >= this.bottomBoundary) {
                this.body.setVelocityY(-this.speed);
            }
        }

        // Prevent falling off the platform
        if (this.body.velocity.x === 0 && this.body.velocity.y === 0) {
            this.body.setVelocityX(this.speed);
            this.flipX = true;
        }
    }

    // Example: Following the player
    follow(player) {
        if (Phaser.Math.Distance.Between(this.x, this.y, player.x, player.y) < 200) {
            this.isFollowing = true;
            this.scene.physics.moveToObject(this, player, this.speed);
        } else {
            this.isFollowing = false;
            this.body.setVelocity(0);
        }
    }

    // Example: Enemy taking damage
    takeDamage() {
        let dropHeart = Math.floor(Math.random() * this.dropHeartOneIn);
        if (dropHeart === 1) {
            let heart = this.scene.physics.add.image(this.x, this.y, 'heart-full').setScale(.5);
            this.scene.hearts.add(heart);
            this.scene.tweens.add({
                targets: heart,
                scale: 0.8,
                duration: 1000,
                ease: 'Power1',
                yoyo: true,
                repeat: -1
            });
        }
        // Create a tween that scales the enemy's body on Y-axis to 10
        let variance = Math.floor(Math.random() * 2);
        this.scene.sound.play('99boy-defeat-'+variance);
        this.scene.tweens.add({
            targets: this,
            scaleY: .2,
            duration: 200, // Duration of the animation in milliseconds
            ease: 'Power1',
            onComplete: () => {
                // Destroy the enemy after the animation completes
                this.destroy();
            }
        });
    }

    fireBullet() {
        // Get the player from the scene
        const player = this.scene.player;

        // Fire the bottle
        let variance = Math.floor(Math.random() * 3);
        const bottle = this.scene.physics.add.sprite(this.x, this.y, '99boy-tiles', 2+variance);
        bottle.setSize(50, 50);
        this.scene.bottles.add(bottle);
        const angle = Phaser.Math.Angle.Between(this.x, this.y, player.x, player.y);
        bottle.setVelocity(Math.cos(angle) * this.bottleSpeed, Math.sin(angle) * this.bottleSpeed);
        this.scene.physics.world.enable(bottle);
        this.scene.tweens.add({
            targets: bottle,
            angle: 720,
            duration: 2000,
            ease: 'Power1',
            onComplete: () => {
                bottle.destroy();
            }
        });
    }


    // 
    update(time, delta) {
        if (this.onPatrol === true) {
            this.patrol();
        }
        this.anims.play('walk99', true);

        if (this.shoots) {
            

                // Check if the enemy is visible to the camera
                const camera = this.scene.cameras.main;
                if (camera.worldView.contains(this.x, this.y)) {
                    this.shootTimer += delta;
                    if (this.shootTimer >= this.bottleReloadCalc) {
                        this.shootTimer = 0;
                    let variance = Math.floor(Math.random() * this.bottleReloadVariance);
                    this.bottleReloadCalc = this.bottleReload + variance;
                    this.fireBullet();
                    }
                }
            
        }
    }
}