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;
        this.garbage = false;
        this.onPause = false;
        this.health = 1;
        this.takingDamage = false;
        this.boss = false;

        // Create animations
        this.anims.create({
            key: 'walk99',
            frames: this.anims.generateFrameNumbers(texture, { 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() {
        if (this.onPause === true) {
            return;
        }
        // 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.touching.up || this.body.y <= this.topBoundary) {
                this.body.setVelocityY(this.speed);
            } else if (this.body.touching.down || this.body.y >= this.bottomBoundary) {
                this.body.setVelocityY(-this.speed);
            }
        }

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

    // 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() {
        if (this.takingDamage === true) {
            return;
        }

        this.takingDamage = true;

        // Set takingDamage back to false after 2000ms
        this.scene.time.delayedCall(500, () => {
            this.takingDamage = false;
        });

        if (this.health === 1) {
            if (this.boss === true) {
                //BOSS DEFEAT
                this.scene.game.globalState.addScore(100);
                this.scene.interface.showScore();
                this.shoots = false;
                this.garbage = false;
                this.onPatrol = false;
                this.anims.stop('walk99');
                this.onPause = true;
                this.setTexture('boss-tiles', 2);
                this.scene.tweens.add({
                    targets: this,
                    scale: 1.5,
                    ease: 'Power1',
                    yoyo: true,
                    repeat: 2,
                    duration: 200,
                    onComplete: () => {
                        this.setTexture('boss-tiles', 3);
                        this.scene.tweens.add({
                            targets: this,
                            ease: 'Power1',
                            scale: 1.1,
                            duration: 200,
                            onComplete: () => {
                                this.setTexture('boss-tiles', 4);
                                this.scene.tweens.add({
                                    targets: this,
                                    ease: 'Power1',
                                    scale: 0,
                                    duration: 2000,
                                    delay: 2000,
                                    onComplete: () => {
                                        this.destroy();
                                    }
                                });
                            }
                        });
                    }               
                });
            } else {
                this.scene.game.globalState.addScore(10);
                this.scene.interface.showScore();
                //REGULAR ENEMY DEFEAT
                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();
                    }
                });
            }
        } else {
            this.body.setVelocity(0, 0);
            this.health --;
            if (this.health === 2) {
                this.shoots = true;
                this.bottleReload = 1200;
                this.speed += 200;
            }
            if (this.health === 1) {
                this.bottleReload = 800;
                this.speed += 200;
                this.bottleReloadVariance = 1000;
            }
            if (this.boss === true) {
                this.anims.stop('walk99');
                this.setTexture('boss-tiles', 2);
            }
        }
    }

    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.sound.play('swoosh');
        this.scene.tweens.add({
            targets: bottle,
            angle: 720,
            duration: 2000,
            ease: 'Power1',
            onComplete: () => {
                if (this.scene) {
                    this.scene.sound.play('glass-break');
                }
                bottle.destroy();
            }
        });
    }

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

        // Fire three garbage projectiles with different angles
        const baseAngle = Phaser.Math.Angle.Between(this.x, this.y, player.x, player.y);

        // Create and fire garbage at original angle
        this.createGarbageProjectile(baseAngle);

        // Create and fire garbage at 0.5 radians less than the base angle
        this.createGarbageProjectile(baseAngle - 0.5);

        // Create and fire garbage at 0.5 radians more than the base angle
        this.createGarbageProjectile(baseAngle + 0.5);
        this.scene.sound.play('crumple');
    }

    createGarbageProjectile(angle) {
        let variance = Math.floor(Math.random() * 4);
        const garbage = this.scene.physics.add.sprite(this.x, this.y, '99boy-tiles', 6+variance);
        garbage.setSize(50, 50);
        this.scene.bottles.add(garbage);
        garbage.setVelocity(Math.cos(angle) * this.bottleSpeed, Math.sin(angle) * this.bottleSpeed);
        this.scene.physics.world.enable(garbage);
        this.scene.tweens.add({
            targets: garbage,
            angle: 720,
            duration: 2000,
            ease: 'Power1',
            onComplete: () => {
                garbage.destroy();
            }
        });
    }

    prepShot() {
        this.scene.time.delayedCall(500, () => {
            if(!this.body) {
                return;
            }
            this.onPause = true;
            const originalVelocity = this.body.velocity.clone();
            this.body.setVelocity(0, 0);

            this.scene.time.delayedCall(500, () => {
                if(!this.body) {
                    return;
                }
                this.body.setVelocity(originalVelocity.x, originalVelocity.y);
                this.onPause = false;
            });
        });
        this.scene.tweens.add({
            targets: this,
            tint: 0xaaaaff,
            duration: 1000,
            repeat: 0,
            yoyo: true,
            ease: 'Power1'
        });
    }

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

        const camera = this.scene.cameras.main;

        if (this.shoots) {
            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.prepShot();
                this.scene.time.delayedCall(1000, () => {
                    if(!this.body) {
                        return;
                    }
                    this.fireBullet();
                });
                }
            }
        }

        if (this.garbage) {
            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.prepShot();
                this.scene.time.delayedCall(1000, () => {
                    if(!this.body) {
                        return;
                    }
                    this.fireGarbage();
                });                
                }
            }
        }
    }
}