//=============================================================================
// Bobstah Plugins
// BOB_BattleCommandList.js
// Version: 1.1
//=============================================================================

var Imported = Imported || {};
Imported.BOB_BattleCommandList = true;

var Bobstah = Bobstah || {};
Bobstah.BattleCmds = Bobstah.BattleCmds || {};

//=============================================================================
 /*:
 * @plugindesc Allows further customization of battle command menus by class
 * and actor.
 * @author Bobstah (Bobstah @ rpgmakerweb.com forums or /rpgmg/)
 *
 * @help
 * ============================================================================
 * Introduction
 * ============================================================================
 *
 * Allows the modification of battle commands on an actor and/or class basis.
 * This also allows you to add skills and items directly onto the command list.
 * Some command options are restricted to class. See Command Defintions for
 * more information.
 *
 * If no battle commands are set at the class level, it will use the actor's
 * command list instead. If the actor or class has no battle commands set, the
 * default list will be used.
 *
 * Note: This plugin will be updated more extensively later.
 *
 * ============================================================================
 * Recommended Usage
 * ============================================================================
 *
 * Set only actor-specific commands (maybe a skill or skilltype, etc) on each
 * actor. Set the general attack, skill, item, etc commands on each class,
 * then insert ActorCmd where you want the actor's commands to show up.
 *
 * ============================================================================
 * Notetags
 * ============================================================================
 *
 * Class
 *   <Battle Commands>
 *   Attack
 *   Skills
 *   Skill:ID
 *   Item:ID
 *   ActorCmd
 *   Items
 *   Guard
 *   </Battle Commands>
 *
 * Actor
 *   <Battle Commands>
 *   Attack
 *   Skills
 *   Skill:ID
 *   Item:ID
 *   Items
 *   Guard
 *   </Battle Commands>
 * ============================================================================
 * Command Defintions
 * ============================================================================
 *
 * Class & Actor
 *   Attack - Use the default attack command
 *   Skills - Show the default skill list
 *   Skill:ID - Show a specific skill ID, if the actor knows it and can use it.
 *   Item:ID - Show a specific item, if it is in the party inventory
 *   Items - Show the default item command
 *   Guard - Show the default Guard command.
 * 
 * Class
 *   ActorCmd - Show the list of actor commands.
 */
//=============================================================================


//=============================================================================
// DataManager
//=============================================================================
Bobstah.BattleCmds.DataManager_isDatabaseLoaded = DataManager.isDatabaseLoaded;
DataManager.isDatabaseLoaded = function() {
    if (!Bobstah.BattleCmds.DataManager_isDatabaseLoaded.call(this)) return false;
	DataManager.processBobstahBattleCmdNotes($dataActors);
    DataManager.processBobstahBattleCmdNotes($dataClasses);
	return true;
};

DataManager.processBobstahBattleCmdNotes = function(group) {
	var cmdlist = /<Battle Commands>[\s]+([\s\S]*?)[\r|\n]+<\/Battle Commands>/i;
	
	for (var n = 1; n < group.length; n++) {
		var obj = group[n];

		obj.battleCommands = [];
		if (obj.note.match(cmdlist)) {
			var notedata = RegExp.$1.split(/[\r\n]+/);
			for (var i = 0; i < notedata.length; i++) {
				obj.battleCommands.push(notedata[i].replace(/^\s+|\s+$/g, '').toLowerCase());
			}
		}
	}
};

//=============================================================================
// Game_Actor
//=============================================================================
Game_Actor.prototype.battleCommandList = function() {
	var classCommands = $dataClasses[this._classId].battleCommands;
	var actorCmd = null;
	for(var i = 0; i < classCommands.length; i++) {
		cmd = classCommands[i];
		if (cmd.match(/ActorCmd/i)) {
			actorCmd = i;
		}
	}
	if (actorCmd != null) {
		classCommands.splice(actorCmd,1);
		var battleCommands = $dataActors[this._actorId].battleCommands.reverse();
		if (battleCommands.length > 0) {
			for(var i = 0; i < battleCommands.length; i++) {
				classCommands.splice(actorCmd,0,battleCommands[i]);
			}
		}
	} else if (classCommands.length === 0) {
		classCommands = $dataActors[this._actorId].battleCommands;
	}
	return classCommands;
}

//=============================================================================
// Window_ActorCommand
//=============================================================================
Bobstah.BattleCmds.WindowActorCommand_makeCommandList = Window_ActorCommand.prototype.makeCommandList;
Window_ActorCommand.prototype.makeCommandList = function() {
    if (this._actor) {
        var battleCommands = this._actor.battleCommandList();
		if (battleCommands.length != 0) {
			for(var i = 0; i < battleCommands.length; i++) {
				var cmd = battleCommands[i];
				switch (cmd)
				{
					case "attack":
						this.addAttackCommand();
					break;
					
					case "skills":
						this.addSkillCommands();
					break;
					
					case "guard":
						this.addGuardCommand();
					break;
					
					case "items":
						this.addItemCommand();
					break;
					
					default:
						if (cmd.match(/skill:[\s]*([0-9]+)/i))
						{
							this.addCustomSkillCommand(RegExp.$1);
							break;
						} else if (cmd.match(/item:[\s]*([0-9]+)/i))
						{
							this.addCustomItemCommand(RegExp.$1);
							break;
						}
					break;
				}
			}
		} else {
			Bobstah.BattleCmds.WindowActorCommand_makeCommandList.call(this);
		}
    }
};

Window_ActorCommand.prototype.addCustomSkillCommand = function(skillId) {
	var skill = $dataSkills[skillId];
	if (this._actor.isLearnedSkill(parseInt(skillId))) {
		this.addCommand(skill.name, 'customSkill', this._actor.canUse(skill), skill);
	}
}

Window_ActorCommand.prototype.addCustomItemCommand = function(itemId) {
	var item = $dataItems[itemId];
	if ($gameParty.hasItem(item,true)) {
		this.addCommand(item.name, 'customItem', this._actor.canUse(item), item);
	}
}

//=============================================================================
// Scene_Battle
//=============================================================================
Bobstah.BattleCmds.SceneBattle_createActorCommandWindow = Scene_Battle.prototype.createActorCommandWindow;
Scene_Battle.prototype.createActorCommandWindow = function() {
    //Bobstah.BattleCmds.SceneBattle_createActorCommandWindow();
	this._actorCommandWindow = new Window_ActorCommand();
    this._actorCommandWindow.setHandler('attack', this.commandAttack.bind(this));
    this._actorCommandWindow.setHandler('skill',  this.commandSkill.bind(this));
    this._actorCommandWindow.setHandler('guard',  this.commandGuard.bind(this));
    this._actorCommandWindow.setHandler('item',   this.commandItem.bind(this));
    this._actorCommandWindow.setHandler('cancel', this.selectPreviousCommand.bind(this));
    this._actorCommandWindow.setHandler('customSkill', this.onCustomSkillOk.bind(this));
	this._actorCommandWindow.setHandler('customItem', this.onCustomItemOk.bind(this));
	this.addWindow(this._actorCommandWindow);
};

Scene_Battle.prototype.onCustomSkillOk = function() {
    var skill = this._actorCommandWindow.currentExt();
    var action = BattleManager.inputtingAction();
    action.setSkill(skill.id);
    BattleManager.actor().setLastBattleSkill(skill);
    this.onSelectAction();
};

Scene_Battle.prototype.onCustomItemOk = function() {
    var item = this._actorCommandWindow.currentExt();
    var action = BattleManager.inputtingAction();
    action.setItem(item.id);
    $gameParty.setLastItem(item);
    this.onSelectAction();
};