class CustomSound
{
    constructor (scene, key, looping, muted, fadems)
    {
        this.scene = scene;
        this.key = key;
        this.sound = scene.sound.add(key, { loop: looping });
        this.muted = muted;
        this.fadems = fadems;
    }
    
    setMute(v)
    {
        this.muted = v;
    }

    play()
    {
        this.scene.tweens.killTweensOf(this.sound);

        if(this.muted)
        {
            return;
        }
        
        this.sound.setVolume(1);

        if(this.sound.isPlaying)
        {
            return;    
        }

        if(this.sound.isPaused)
        {
            this.sound.resume();
        }
        else
        {
            this.sound.play();
        }
    }

    pause(isInstant)
    {
        if(this.muted)
        {
            this.sound.pause();
            return;
        }
        
        if(this.sound.isPaused)
        {
            return;
        }

        if(isInstant != null && isInstant)
        {
            this.sound.pause();
            return;
        }
        
        this.scene.tweens.add({

            targets: this.sound,
            volume: 0,

            ease: 'Linear',
            duration: this.fadems,
            onCompleteScope: this,
            onComplete: function() 
            {
                this.sound.pause();
            },
        });
    }

    stop(isInstant)
    {
        if(this.muted)
        {
            this.sound.stop();
            return;
        }

        if(!this.sound.isPlaying)
        {
            return;    
        }

        if(isInstant != null && isInstant)
        {
            this.sound.stop();
            return;
        }

        this.scene.tweens.add({

            targets: this.sound,
            volume: 0,

            ease: 'Linear',
            duration: this.fadems,
            onCompleteScope: this,
            onComplete: function()
            {
                this.sound.stop();
            }                
        });
    }

    isPlaying()
    {
        return this.sound.isPlaying;
    }

    isTweening()
    {
        return this.scene.tweens.isTweening(this.sound);
    }
}    