import React from 'react';

import Wrapper from 'common/Wrapper';
import SPELLS from 'common/SPELLS';
import SpellLink from 'common/SpellLink';
import Combatants from 'Parser/Core/Modules/Combatants';
import Enemies from 'Parser/Core/Modules/Enemies';
import { encodeTargetString } from 'Parser/Core/Modules/EnemyInstances';
import Analyzer from 'Parser/Core/Analyzer';

const EXECUTE_RANGE = 0.2;

class RendAnalyzer extends Analyzer {
  static dependencies = {
    combatants: Combatants,
    enemies: Enemies,
  };

  rendsInExecuteRange = 0;
  targets = [];
  failedTargets = [];

  on_initialized() {
		this.active = this.combatants.selected.hasTalent(SPELLS.REND_TALENT.id);
	}

  on_byPlayer_cast(event) {
    if(SPELLS.REND_TALENT.id === event.ability.guid) {
      // If Rend is used flag the target to check their health on the next damage event.
      this.targets.push(encodeTargetString(event.targetID, event.targetInstance));
    }
  }

  on_byPlayer_damage(event) {
    const targetString = encodeTargetString(event.targetID, event.targetInstance);

    if(SPELLS.REND_TALENT.id === event.ability.guid && this.targets.indexOf(targetString) !== -1) {
      // Unflag the target for checking.
      this.targets.splice(this.targets.indexOf(targetString), 1);

      // Check if the target's health was within execute range.
      if(event.hitPoints / event.maxHitPoints >= EXECUTE_RANGE) {
        return;
      }

      if(this.failedTargets.indexOf(targetString) === -1) {
        // If this is the first time Rend was used on this target in execute range we flag that target.
        // This is to prevent false positives from Rend being applied just as the target enters execute range.
        this.failedTargets.push(targetString);
      } else {
        // Increment the counter
        this.rendsInExecuteRange += 1;
      }
    }
  }

  get executeRendsThresholds() {
    return {
			actual: this.rendsInExecuteRange,
			isGreaterThan: {minor: 0, average:1, major: 2},
			style: 'number',
		};
  }

  suggestions(when) {
    when(this.executeRendsThresholds).addSuggestion((suggest, actual, recommended) => {
        return suggest(<Wrapper>You should avoid using <SpellLink id={SPELLS.REND_TALENT.id} icon/> on a target in <SpellLink id={SPELLS.EXECUTE.id} icon/> range.</Wrapper>)
          .icon(SPELLS.REND_TALENT.icon)
          .actual(`Rend was used ${actual} times on a target in execute range`)
          .recommended(`${recommended} is recommended.`);
      });
  }
}

export default RendAnalyzer;

@@ -28,23 +28,36 @@
 %0Aimport 
-Wrapper
+%7B formatPercentage %7D
  from 'c
@@ -62,23 +62,22 @@
 'common/
-Wrapper
+format
 ';%0Aimpor
@@ -149,16 +149,100 @@
 lLink';%0A
+import Wrapper from 'common/Wrapper';%0A%0Aimport Analyzer from 'Parser/Core/Analyzer';%0A
 import C
@@ -417,53 +417,8 @@
 es';
-%0Aimport Analyzer from 'Parser/Core/Analyzer';
 %0A%0Aco
@@ -563,16 +563,29 @@
 ,%0A  %7D;%0A%0A
+  rends = 0;%0A
   rendsI
@@ -996,16 +996,40 @@
 tance));
+%0A%0A      this.rends += 1;
 %0A    %7D%0A 
@@ -2018,16 +2018,29 @@
 uteRange
+ / this.rends
 ,%0A%09%09%09isG
@@ -2052,16 +2052,25 @@
 rThan: %7B
+%0A        
 minor: 0
@@ -2070,16 +2070,24 @@
 inor: 0,
+%0A       
  average
@@ -2091,19 +2091,40 @@
 age:
-1, major: 2
+0.05,%0A        major: 0.1,%0A      
 %7D,%0A%09
@@ -2137,14 +2137,15 @@
 e: '
-number
+percent
 ',%0A%09
@@ -2517,21 +2517,46 @@
 d $%7B
-actual%7D
+formatPercentage(actual)%7D%25 of the
  time
-s
  on 
@@ -2580,16 +2580,17 @@
 te range
+.
 %60)%0A     
@@ -2610,16 +2610,33 @@
 nded(%60$%7B
+formatPercentage(
 recommen
@@ -2638,17 +2638,19 @@
 ommended
-%7D
+)%7D%25
  is reco
@@ -2656,17 +2656,16 @@
 ommended
-.
 %60);%0A    

Change Rend thresholds to percentage