import Phaser from 'phaser'

export default class VFX extends Phaser.Group {
    constructor(game, parent, name) {
        super(game, parent, name);

        this.createImpactParticles();
        this.createIBloodParticles();
        // this.createWalkingParticles();
        this.createHintPool();

        this.game.myEvents.onWallCollision.add(this.spawnImpact, this);
        this.game.myEvents.onGirlDrop.add(this.spawnDrop, this);
        this.game.myEvents.onGirlPick.add(this.hideHint, this);

        this.isGore = false;
        this.game.myEvents.onStartGore.add(() => {
            this.enableGore();
        });
    }

    createImpactParticles() {
        this.impactPool = [];
        for (let i = 0; i < 10; i++) {

            // let a = this.game.add.graphics();
            // a.beginFill(0x000000);
            // a.drawCircle(0, 0, 20);
            // a.endFill();

            const prt = this.game.add.sprite(0, 0, 'golpe');
            prt.scale.set(0.4);
            prt.anchor.set(0.5);
            prt.visible = false;
            this.add(prt);

            this.impactPool.push(prt);
        }
    }

    createIBloodParticles() {
        this.bloodPool = [];
        for (let i = 0; i < 10; i++) {

            // let a = this.game.add.graphics();
            // a.beginFill(0x000000);
            // a.drawCircle(0, 0, 20);
            // a.endFill();

            const prt = this.game.add.sprite(0, 0, 'blood');
            prt.scale.set(0.4);
            prt.anchor.set(0.5);
            prt.visible = false;
            this.add(prt);

            this.bloodPool.push(prt);
        }
    }

    createHintPool() {
        this.hintPool = [];
        this.hintGrp = this.game.add.group();
        this.add(this.hintGrp);

        for (let i = 0; i < 3; i++) {
            let a = this.game.add.graphics();
            a.lineStyle(8, 0x000000);
            // a.beginFill(0x000000);
            a.drawEllipse(0, 0, 100, 100);
            // a.endFill();
            
            const prt = this.game.add.sprite(a.x, a.y, a.generateTexture());
            a.destroy();
            prt.x += 15;
            prt.y += 10;
            prt.anchor.set(0.5);
            this.hintGrp.add(prt);

            this.hintPool.push(prt);
            this.tweenHint(prt);
        }
        this.hintGrp.visible = false;
    }

    spawnImpact(isSafe, player) {
        const localPos = this.toLocal(player, player.parent);
        if (!this.isGore) {
            this.impactPool.forEach(prt => {
                prt.visible = true;
                prt.alpha = 1;
                prt.position.copyFrom(localPos);
                prt.x += Math.random() * 20;
                prt.y += Math.random() * 20;
    
                this.tweenImpact(prt)
            });
        } else {
            this.bloodPool.forEach(prt => {
                prt.visible = true;
                prt.alpha = 1;
                prt.position.copyFrom(localPos);
                prt.x += Math.random() * 20;
                prt.y += Math.random() * 20;
    
                this.tweenImpact(prt)
            });
        }
    }

    enableGore() {
        this.isGore = true;
    }

    spawnDrop(player) {
        const localPos = this.toLocal(player, player.parent);
        this.hintGrp.position.copyFrom(localPos);
        this.hintGrp.visible = true;
    }

    hideHint() {
        this.hintGrp.visible = false;
    }

    tweenHint(prt) {
        prt.scale.set(0.5);
        this.game.add.tween(prt).to( {alpha: 0 }, 1500, Phaser.Easing.Quadratic.Out, true).loop(true);
        this.game.add.tween(prt.scale).to( { x: 1.1, y: 1.1 }, 1500, Phaser.Easing.Quadratic.Out, true).loop(true);
    }

    tweenImpact(prt) {
        const rndX = Math.random() < 0.5 ? 1 : -1;
        const rndY = Math.random() < 0.5 ? 1 : -1;
        this.game.add.tween(prt).to( {x: `${Math.random() * 75 * rndX}`, y: `${Math.random() * 75 * rndY}` }, 300, Phaser.Easing.Quadratic.Out, true);
        this.game.add.tween(prt).to( {alpha: 0 }, 500, Phaser.Easing.Quadratic.Out, true)
            .onComplete.add(() => {
                prt.visible = false;
            });
    }
}
