//=============================================================================
// Easy Transform Emotion - By TomatoRadio
// TR_EasyTransformEmotion.js
//=============================================================================

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

var TR = TR || {};
TR.ETE = TR.ETE || {};
TR.ETE.version = 1.0;

/*: 
 *
 * @plugindesc Allows for easy adding of new emotions to enemies.
 * Version 1.0
 * @author TomatoRadio
 * 
 * @help
 * In base OMORI, enemy emotions are controlled by the TransformEmotion notetag.
 * What this does is tell the game to transform the enemy to a slot some number forward.
 * However, only the main three emotions are configured to work.
 * This plugin changes that to be more moddable.
 * Simply input the name of each emotion that an enemy can feel in the order you want
 * them listed in the Database.
 * 
 * Note1: Enemies can feel higher tiered emotions like Enraged, however these will still use
 * their T1 slots unless you add cases for higher tiers. See Space Ex-Husband.
 * 
 * Note2: You cannot name a TransformEmotion 'normal'
 * This is because the game uses 'normal' as the name for lacking an emotion.
 * ie. Neutral.
 * 
 * @param TransformEmotions
 * @text TransformEmotions
 * @type []
 * @desc The ID of the Switch to Enable or Disable persistence for BGM.
 * @default ["happy","sad","angry"]
 * 
*/
TR.ETE.Param = PluginManager.parameters('TR_PersistentBattleSound');

TR.TransformEmotions = JSON.parse(TR.ETE.Param["TransformEmotions"])

Game_Enemy.prototype.refreshEmotionStateTransform = function() {
  // If Not Dead
  if (!this.isDead() && !this._isStateTransforming) {
    // Get Emotion
    var emotion = this.getStateEmotion();
    // Get base Id
    var baseId = this.enemy().meta.TransformBaseID;
    // Set Base ID
    baseId = baseId ? Number(baseId) : this._enemyId;
    // Set Transform ID
    var transformId = this._enemyId;
    // Switch Case Emotion
    switch (emotion) {
      case 'normal': transformId = baseId ;break;
      default:
        if (TR.TransformEmotions.includes(emotion)) { //This should always be the case but fuck idk
          const isTransformEmotion = (element) => element === emotion
          var googlegoggle = TR.TransformEmotions.findIndex(isTransformEmotion) + 1;
          transformId = baseId + googlegoggle;
          break;
        } else {
          transformId = baseId
          console.log('YOU MADE A TRANSFORMEMOTION WITHOUT INCLUDING IT IN THE ARRAY FOR TR_EASYTRANSFORMEMOTION IDIOT NOW START DOING IT RIVHWHWIGKJ');
          break;
        }
    };
    // If Transform Id
    if (transformId !== this._enemyId) {
      this._isStateTransforming = true;
      if(this.name() === "SPACE EX-HUSBAND") {
        $gameScreen.setFlashWait(60)
        $gameScreen.startFlash([255,255,255,255], 130)
      }
      this.transform(transformId)
      this._isStateTransforming = false;
    };
  };

};

Game_Action.prototype.calcElementRate = function(target) {
    /*if (this.item().damage.elementId < 0) {
        return this.elementsMaxRate(target, this.subject().attackElements());
    } else {*/
        // YIN Instead of bypassing the subject's attack element, we want to use it if they are inflicted with an emotion.
        // If elementId == None or Normal Attack
        if (this.item().damage.elementId < 1) {
            // These are all base emotion states (non afraid)
            var emotionStates = [6, 7, 8, 10, 11, 12, 14, 15, 16];
            emotionStates = [...emotionStates, 119,120,121]; // Space Ex Boyfriend
            emotionStates = [...emotionStates, 122,123,197]; // Sweetheart
            emotionStates = [...emotionStates, 124,125,126]; // Unbread Twins
            // Search states for emotion states
            var emotionAfflicted;

            var emotion = this.getStateEmotion();
            if (emotion === 'normal') {
              emotionAfflicted = false;
            } else {
              emotionAfflicted = true;
            }

            if (emotionAfflicted) {
                return target.elementRate(this.subject().attackElements()[0]); // If so, that is our attack element
            } else {
                return target.elementRate(this.item().damage.elementId);
            }
        } else {
        return target.elementRate(this.item().damage.elementId);
        }
    //}
};