import { DiceSFX } from '../DiceSFX.js';
import * as THREE from '../libs/three.module.js';
import { Proton } from '../libs/three-modules/three.proton.js';

export class PlayAnimationParticleSpiral extends DiceSFX {
    static id = "PlayAnimationParticleSpiral";
    static name = "DICESONICE.PlayAnimationParticleSpiral";
    static sprite = null;
    static dot = "data:image/png;base64,[encoded data, 6924 characters]";
    static sound = "modules/dice-so-nice/sfx/sounds/doublespiral.mp3";

    /**@override init */
    static async init() {
        let map = new THREE.TextureLoader().load(PlayAnimationParticleSpiral.dot);
        let material = new THREE.SpriteMaterial({
            map: map,
            color: 0xff0000,
            blending: THREE.CustomBlending,
            blendSrc: THREE.SrcAlphaFactor,
            blendDst: THREE.OneFactor,
            blendEquation: THREE.AddEquation,
            fog: true
        });
        PlayAnimationParticleSpiral.sprite = new THREE.Sprite(material);
        game.audio.pending.push(function(){
            AudioHelper.preloadSound(PlayAnimationParticleSpiral.sound);
        }.bind(this));
    }

    /**@override play */
    async play() {
        this.proton = new Proton();
        this.R = 70;
        this.tha = 0;
        this.emitter1 = this.createEmitter(this.R, this.R, '#4F1500', '#0029FF');
        this.emitter2 = this.createEmitter(-this.R, -this.R, '#004CFE', '#6600FF');
        this.proton.addEmitter(this.emitter1);
        this.proton.addEmitter(this.emitter2);
        this.proton.addRender(new Proton.SpriteRender(this.box.scene));

        AudioHelper.play({
            src: PlayAnimationParticleSpiral.sound,
            volume: this.box.volume
		}, false);
        this.renderReady = true;
        //Proton.Debug.drawZone(proton,scene,zone2);
        //Proton.Debug.drawEmitter(this.proton,this.box.scene,this.emitter);
    }

    createEmitter(x, y, color1, color2) {
        var emitter = new Proton.Emitter();
        emitter.rate = new Proton.Rate(new Proton.Span(5, 7), new Proton.Span(.01, .02));
        emitter.addInitialize(new Proton.Mass(1));
        emitter.addInitialize(new Proton.Life(1));
        emitter.addInitialize(new Proton.Body(PlayAnimationParticleSpiral.sprite));
        emitter.addInitialize(new Proton.Radius(30));
        emitter.addInitialize(new Proton.V(200, new Proton.Vector3D(0, 0, -1), 0));


        emitter.addBehaviour(new Proton.Alpha(1, 0));
        emitter.addBehaviour(new Proton.Color(color1, color2));
        emitter.addBehaviour(new Proton.Scale(1.5, 0.1));
        //emitter.addBehaviour(new Proton.CrossZone(new Proton.ScreenZone(this.box.camera, this.box.renderer), 'dead'));

        emitter.addBehaviour(new Proton.Force(0, 0, 17));
        emitter.p.x = this.dicemesh.parent.position.x + x;
        emitter.p.y = this.dicemesh.parent.position.y + y;
        emitter.p.z = 50;
        emitter.emit(0.7,true);
        return emitter;
    }

    animateEmitter() {
        this.tha += .2;
        this.emitter1.p.x = this.dicemesh.parent.position.x + this.R * Math.cos(this.tha);
        this.emitter1.p.y = this.dicemesh.parent.position.y + this.R * Math.sin(this.tha);

        this.emitter2.p.x = this.dicemesh.parent.position.x - this.R * Math.cos(this.tha);
        this.emitter2.p.y = this.dicemesh.parent.position.y - this.R * Math.sin(this.tha);
    }

    render() {
        if(!this.renderReady)
            return;
        this.animateEmitter();
        this.proton.update();
        if(this.proton.emitters.length == 0){
            this.destroy();
        }
    }

    destroy(){
        if(this.emitter1){
            this.emitter1.stopEmit();
            this.emitter1.removeAllParticles();
        } 
            
        if(this.emitter2){
            this.emitter2.stopEmit();
            this.emitter2.removeAllParticles();
        }
        this.proton.update();
        this.proton.destroy();
        this.destroyed = true;
    }
}