import React from 'react';

import SPELLS from 'common/SPELLS';
import SpellIcon from 'common/SpellIcon';
import SpellLink from 'common/SpellLink';
import ITEMS from 'common/ITEMS';
import { formatPercentage } from 'common/format';

import Analyzer from 'Parser/Core/Analyzer';
import Combatants from 'Parser/Core/Modules/Combatants';
import StatisticBox, { STATISTIC_ORDER } from 'Main/StatisticBox';

class ColdHeartEfficiency extends Analyzer {
  static dependencies = {
    combatants: Combatants,
  };
  on_initialized() {
    this.active = this.combatants.selected.hasChest(ITEMS.COLD_HEART.id);
  }

  totalColdHeartCasts = 0;
  correctColdHeartCasts = 0;
  buffColdHeart = 0;

  on_byPlayer_applybuffstack(event){
    const spellID = event.ability.guid;
    if(spellID === SPELLS.COLD_HEART_BUFF.id){
      this.buffColdHeart=event.stack;
    }
  }

  on_byPlayer_cast(event) {
    const spellId = event.ability.guid;
    if (spellId === SPELLS.CHAINS_OF_ICE.id) {
       this.totalColdHeartCasts++;
    	 if  (this.combatants.selected.hasBuff(SPELLS.UNHOLY_STRENGTH.id)) {
		      if (this.buffColdHeart < 20) {
			      if (this.buffColdHeart > 16) {
              this.correctColdHeartCasts++;
			      }
	      	}
       }
	    if (this.buffColdHeart === 20) {
        this.correctColdHeartCasts++;
      }
      //This is only here for double casting Cold Heart. If Cold Heart is casted again before it reaches 2 stacks, the event won't update.
      this.buffColdHeart=0;
   }

  }

  suggestions(when) {
    const castEfficiency = this.correctColdHeartCasts/this.totalColdHeartCasts;
    when(castEfficiency).isLessThan(0.8)
        .addSuggestion((suggest, actual, recommended) => {
          return suggest(<span>You are casting <SpellLink id={SPELLS.CHAINS_OF_ICE.id}/> at the wrong times. You either want to cast <SpellLink id={SPELLS.CHAINS_OF_ICE.id}/> when at 20 stacks of <SpellLink id={SPELLS.COLD_HEART_BUFF.id}/> or when you are above 16 stacks and you have the buff <SpellLink id={SPELLS.UNHOLY_STRENGTH.id}/></span>)
            .icon(SPELLS.CHAINS_OF_ICE.icon)
            .actual(`${formatPercentage(actual)}% of Chains of Ice were cast correctly.`)
            .recommended(`>${formatPercentage(recommended)}% is recommended`)
            .regular(recommended - 0.30).major(recommended - 0.40);
        });
  }
  statistic() {
    const castEfficiency = this.correctColdHeartCasts/this.totalColdHeartCasts;
        return (
      <StatisticBox
        icon={<SpellIcon id={SPELLS.CHAINS_OF_ICE.id} />}
        value={`${formatPercentage(castEfficiency)} %`}
        label={'Cold Heart Efficiency'}
        tooltip={`${this.correctColdHeartCasts} out of ${this.totalColdHeartCasts} casts of Cold Heart were made correctly.`}
      />
    );
  }
  statisticOrder = STATISTIC_ORDER.CORE(5);
}

export default ColdHeartEfficiency;

@@ -676,16 +676,392 @@
 art = 0;
+%0A  unholyStrengthStart = 0;%0A%0A  on_byPlayer_applybuff(event) %7B%0A    const spellID = event.ability.guid;%0A    if(spellID === SPELLS.UNHOLY_STRENGTH.id)%7B%0A      this.unholyStrengthStart = event.timestamp;%0A    %7D%0A  %7D%0A%0A  on_byPlayer_removebuff(event) %7B%0A    const spellID = event.ability.guid;%0A    if(spellID === SPELLS.UNHOLY_STRENGTH.id)%7B%0A      this.unholyStrengthStart = 0;%0A    %7D%0A  %7D
 %0A%0A  on_b
@@ -1291,24 +1291,111 @@
 ility.guid;%0A
+    const unholyStrengthRemaining = 15000-(event.timestamp - this.unholyStrengthStart)%0A
     if (spel
@@ -1470,16 +1470,19 @@
 ;%0A    %09 
+   
 if  (thi
@@ -1487,68 +1487,98 @@
 his.
-combatants.selected.hasBuff(SPELLS.UNHOLY_STRENGTH.id)) %7B%0A%09%09
+unholyStrengthRemaining%3E0) %7B%0A%09%09         if (this.unholyStrengthRemaining %3C4000) %7B%0A%09%09      
     
@@ -1615,24 +1615,30 @@
  %7B%0A%09%09%09      
+  
+    
 if (this.buf
@@ -1652,24 +1652,30 @@
 art %3E 16) %7B%0A
+      
             
@@ -1711,24 +1711,31 @@
 +;%0A%09%09%09      
+       
 %7D%0A%09      %09%7D%0A
@@ -1735,11 +1735,32 @@
     
+  
-%09%7D%0A
+      %7D%0A%09      %09  %7D%0A  
     
@@ -2324,17 +2324,19 @@
  at 
-the wrong
+non optimal
  tim
@@ -3273,23 +3273,23 @@
 re made 
-correct
+optimal
 ly.%60%7D%0A  

Update ColdHeartEfficiency.js