/*:
 * @plugindesc Plugin Commands for Better Control over Music
 * v1.0
 * @author TomatoRadio
 * 
 * @help
 * =========================================================
 * PLUGIN COMMANDS
 * =========================================================
 * 
 * [BGM/BGS] SWAPTO [NAME] [VOLUME] [PITCH] [PAN]
 * Changes the Audio from one to other, while maintaining the position in the song.
 * 
 * [BGM/BGS] MOVETO [POS]
 * Sets the current Audio to that position in Samples.
 * 
 * [BGM/BGS] FADETO [VOLUME] [DURATION]
 * Fades the current Audio to the listed Volume in the duration number of frames.
 * 
 * [BGM/BGS] FADIN [NAME] [VOLUME] [DURATION] [PITCH] [PAN]
 * Fades in a new Audio with the listed duration and whatnot.
 * 
*/
{
var Imported = Imported || {};
Imported.TR_ExtMusicControls = true;

var TR = TR || {};
TR.ExtMusicControls = TR.ExtMusicControls || {};

// Adds the plugin commands
TR.ExtMusicControls.GameInterpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function (command, args) {
	switch (command.toLowerCase()) {
		case 'bgm': 
			this.handleBGM(args)
			return;
		case 'bgs':
			this.handleBGS(args)
		default:
			TR.ExtMusicControls.GameInterpreter_pluginCommand.call(this, command, args);
			return;
	}
};

Game_Interpreter.prototype.handleBGM = function(args) {
    let temp = args.shift();
    switch (temp.toLowerCase()) {
        case 'swapto':
            this.swapToBgm(args[0], args[1], args[2], args[3]);
            return;
        case 'moveto':
            this.moveToBgm(args[0]);
            return;
        case 'fadeto':
            this.fadeToBgm(args[0], args[1]);
            return;
        case 'fadein':
            this.fadeInBgm(args[0], args[1], args[2], args[3], args[4]);
            return;
        default:
            console.log('TR_ExtMusicControls: No valid BGM command inputted.')
            return;
    };
};

Game_Interpreter.prototype.handleBGS = function(args) {
    let temp = args.shift();
    switch (temp.toLowerCase()) {
        case 'swapto':
            this.swapToBgs(args[0], args[1], args[2], args[3]);
            return;
        case 'moveto':
            this.moveToBgs(args[0]);
            return;
        case 'fadeto':
            this.fadeToBgs(args[0], args[1]);
            return;
        case 'fadein':
            this.fadeInBgs(args[0], args[1], args[2], args[3], args[4]);
            return;
        default:
            console.log('TR_ExtMusicControls: No valid BGS command inputted.')
            return;
    };
};

Game_Interpreter.prototype.swapToBgm = function(name, vol, pit, pan) {
    vol = Number(vol); pit = Number(pit); pan = Number(pan);
    let old = AudioManager.saveBgm();
    AudioManager.playBgm({name: name, volume: vol, pitch: pit, pan: pan}, old.pos);
};

Game_Interpreter.prototype.moveToBgm = function(pos) {
    pos = Number(pos);
    let old = AudioManager.saveBgm();
    AudioManager.update({name: old.name, volume: old.volume, pitch: old.pitch, pan: old.pan}, pos);
};

Game_Interpreter.prototype.fadeToBgm = function(vol, dur) {
    vol = Number(vol); dur = Number(dur);
    AudioManager.fadeBgmTo(vol, dur);
};

Game_Interpreter.prototype.fadeInBgm = function(name, vol, dur, pit, pan) {
    vol = Number(vol); dur = Number(dur); pit = Number(pit); pan = Number(pan);
    AudioManager.playBgm({name: name, volume: 0, pitch: pit, pan: pan});
    AudioManager.fadeBgmTo(vol, dur);
};

Game_Interpreter.prototype.swapToBgs = function(name, vol, pit, pan) {
    vol = Number(vol); pit = Number(pit); pan = Number(pan);
    let old = AudioManager.saveBgs();
    AudioManager.playBgs({name: name, volume: vol, pitch: pit, pan: pan}, old.pos);
};

Game_Interpreter.prototype.moveToBgs = function(pos) {
    pos = Number(pos);
    let old = AudioManager.saveBgs();
    AudioManager.update({name: old.name, volume: old.volume, pitch: old.pitch, pan: old.pan}, pos);
};

Game_Interpreter.prototype.fadeToBgs = function(vol, dur) {
    vol = Number(vol); dur = Number(dur);
    AudioManager.fadeBgsTo(vol, dur);
};

Game_Interpreter.prototype.fadeInBgs = function(name, vol, dur, pit, pan) {
    vol = Number(vol); dur = Number(dur); pit = Number(pit); pan = Number(pan); pos = Number(pos);
    AudioManager.playBgs({name: name, volume: 0, pitch: pit, pan: pan});
    AudioManager.fadeBgsTo(vol, dur);
};

};