import React from 'react';

import Analyzer from 'Parser/Core/Analyzer';
import Combatants from 'Parser/Core/Modules/Combatants';

import SPELLS from 'common/SPELLS';
import SpellIcon from 'common/SpellIcon';
import { formatPercentage, formatNumber } from 'common/format';
import StatisticBox from 'Main/StatisticBox';
import getDamageBonus from "Parser/Hunter/Shared/Core/getDamageBonus";

const MAX_STACKS = 10;
const TRUE_AIM_MODIFIER = 0.02;

class TrueAim extends Analyzer {
  static dependencies = {
    combatants: Combatants,
  };

  //stack counters
  _currentStacks = 0;
  startOfMaxStacks = 0;
  timeAtMaxStacks = 0;

  //starts -1 because the stacks drop at end of combat, but that shouldn't count as a time where it dropped
  timesDropped = -1;
  totalTimesDropped = -1;

  //bonusDmg
  bonusDmg = 0;
  aimedBonusDmg = 0;
  arcaneBonusDmg = 0;

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

  on_byPlayer_applydebuffstack(event) {
    const debuffId = event.ability.guid;
    if (debuffId !== SPELLS.TRUE_AIM_DEBUFF.id) {
      return;
    }
    //I can't use on_byPlayer_applydebuff as it won't work in conjunction with on_byplayer_removedebuff
    //so we do it this way, and just make a check if _currentStacks is 0, in which case we set it to 1 to indicate it already has been applied.
    if (this._currentStacks === 0) {
      this._currentStacks = 1;
    }
    this._currentStacks += 1;
    if (this._currentStacks === MAX_STACKS) {
      this.startOfMaxStacks = event.timestamp;
    }
  }
  on_byPlayer_removedebuff(event) {
    const debuffId = event.ability.guid;
    if (debuffId !== SPELLS.TRUE_AIM_DEBUFF.id) {
      return;
    }
    if (this._currentStacks === MAX_STACKS) {
      this.timeAtMaxStacks += event.timestamp - this.startOfMaxStacks;
    }
    //ensures Trickshot cleaving doesn't count as multiple resets of stacks
    if (this._currentStacks >= 3) {
      this.timesDropped += 1;
    }
    this.totalTimesDropped += 1;
    this._currentStacks = 0;
  }

  on_byPlayer_damage(event) {
    const spellId = event.ability.guid;
    if (spellId !== SPELLS.AIMED_SHOT.id && spellId !== SPELLS.ARCANE_SHOT.id) {
      return;
    }
    if (spellId === SPELLS.AIMED_SHOT.id) {
      this.aimedBonusDmg += getDamageBonus(event, (TRUE_AIM_MODIFIER*this._currentStacks));
    }
    if (spellId === SPELLS.ARCANE_SHOT.id) {
      this.arcaneBonusDmg += getDamageBonus(event, (TRUE_AIM_MODIFIER*this._currentStacks));
    }
    this.bonusDmg += getDamageBonus(event, (TRUE_AIM_MODIFIER * this._currentStacks));
  }

  statistic() {
    const percentTimeAtMaxTAStacks = formatPercentage(this.timeAtMaxStacks / this.owner.fightDuration);
    return (
      <StatisticBox
        icon={<SpellIcon id={SPELLS.TRUE_AIM_DEBUFF.id} />}
        value={`${percentTimeAtMaxTAStacks} %`}
        label="10 stack uptime"
        tooltip={`You reset True Aim when you had 3 or more stacks (to exclude trickshot cleaving resets): ${this.timesDropped} times over the course of the encounter. <br />Your total amount of resets (including with trickshot cleaving) was: ${this.totalTimesDropped}. <br /> True Aim contributed with           ${formatNumber(this.bonusDmg)} - ${this.owner.formatItemDamageDone(this.bonusDmg)}. <br/> Aimed Shot did ${formatPercentage(
          this.aimedBonusDmg/this.bonusDmg)}% of that and Arcane Shot did ${formatPercentage(this.arcaneBonusDmg/this.bonusDmg)}%.`}
      />
    );

  }
}

export default TrueAim;

@@ -2324,33 +2324,35 @@
 RUE_AIM_MODIFIER
-*
+ * 
 this._currentSta
@@ -2478,17 +2478,19 @@
 MODIFIER
-*
+ * 
 this._cu
@@ -3171,17 +3171,17 @@
 . %3Cbr /%3E
- 
+%0A
 True Aim
@@ -3198,26 +3198,16 @@
 ed with 
-          
 $%7Bformat
@@ -3285,13 +3285,17 @@
 g)%7D.
- %3Cbr/
+%0A%3Cul%3E%0A%3Cli
 %3E Ai
@@ -3299,26 +3299,34 @@
  Aimed Shot 
-di
+contribute
 d $%7BformatPe
@@ -3338,19 +3338,8 @@
 age(
-%0A          
 this
@@ -3344,33 +3344,35 @@
 is.aimedBonusDmg
-/
+ / 
 this.bonusDmg)%7D%25
@@ -3375,21 +3375,19 @@
 g)%7D%25
- of that and 
+.%3C/li%3E%0A%3Cli%3E
 Arca
@@ -3394,18 +3394,26 @@
 ne Shot 
-di
+contribute
 d $%7Bform
@@ -3444,17 +3444,19 @@
 BonusDmg
-/
+ / 
 this.bon
@@ -3464,16 +3464,27 @@
 sDmg)%7D%25.
+%3C/li%3E%0A%3C/ul%3E
 %60%7D%0A     

update true aim to include UL