import SPELLS from 'common/SPELLS';
import RESOURCE_TYPES from 'common/RESOURCE_TYPES';

import Analyzer from 'Parser/Core/Analyzer';

class SpellManaCost extends Analyzer {
  incorrectCosts = {};

  on_byPlayer_cast(event) {
    // Manipulate the event to include mana information so that we don't have to copy paste this anywhere we want to know mana. This can't be done through static functions as some mana costs require state (through class properties) to work properly. E.g. Penance triggers up to 4 cast events but only the first costs mana.
    event.manaCost = this.getManaCost(event);
    event.rawManaCost = this.getRawManaCost(event);
    event.isManaCostNullified = this.selectedCombatant.hasBuff(SPELLS.INNERVATE.id, event.timestamp);
  }

  getHardcodedManaCost(event) {
    const spellId = event.ability.guid;
    const spell = SPELLS[spellId];
    return spell && spell.manaCost ? spell.manaCost : null;
  }
  getRawManaCost(event) {
    const hardcodedCost = this.getHardcodedManaCost(event);
    const actualCost = event.classResources ? event.classResources.reduce((cost, resource) => {
      if (resource.type !== RESOURCE_TYPES.MANA.id) {
        return cost;
      }
      return cost + (resource.cost || 0);
    }, 0) : 0;

    if (hardcodedCost !== null && actualCost && hardcodedCost !== actualCost) {
      console.error(event.ability.name, event.ability.guid, 'The hardcoded cost', hardcodedCost, 'did not match the actual cost', actualCost);
      this.incorrectCosts[event.ability.guid] = {
        ...SPELLS[event.ability.guid],
        manaCost: actualCost,
      };
    }
    return hardcodedCost !== null ? hardcodedCost : actualCost;
  }

  getManaCost(event) {
    return this.getRawManaCost(event);
  }

  on_finished() {
    const incorrectCostCount = Object.keys(this.incorrectCosts).length;
    if (incorrectCostCount === 0) return;

    console.debug(`There were ${incorrectCostCount} abilities that did not match their expected cost, see below for suggested values.`);
    Object.entries(this.incorrectCosts).forEach(([, ability]) => {
      console.debug(ability);
    });
  }
}

export default SpellManaCost;

@@ -1331,258 +1331,216 @@
     
-console.error(event.ability.name, event.ability.guid, 'The hardcoded cost', hardcodedCost, 'did not match the actual cost', actualCost);%0A      this.incorrectCosts%5Bevent.ability.guid%5D = %7B%0A        ...SPELLS%5Bevent.ability.guid%5D,%0A        manaCost: actual
+this.incorrectCosts%5Bevent.ability.guid%5D = %7B%0A        ...SPELLS%5Bevent.ability.guid%5D,%0A        manaCost: undefined, // delete if it's set in SPELLS%0A        actualCost: actualCost,%0A        hardcodedCost: hardcoded
 Cost
@@ -1837,21 +1837,20 @@
 console.
-debug
+warn
 (%60There 
@@ -1976,13 +1976,12 @@
 ect.
-entri
+valu
 es(t
@@ -2012,32 +2012,18 @@
 ach(
-(%5B, 
 ability
-%5D)
  =%3E
- %7B%0A     
  con
@@ -2031,13 +2031,12 @@
 ole.
-debug
+warn
 (abi
@@ -2040,23 +2040,16 @@
 ability)
-;%0A    %7D
 );%0A  %7D%0A%7D

Update SpellManaCost to no longer spam the console