import React from 'react';

import Analyzer from 'Parser/Core/Analyzer';

import SPELLS from 'common/SPELLS';
import SpellLink from 'common/SpellLink';

import { formatPercentage } from 'common/format';
import StatisticBox from 'Main/StatisticBox';
import SpellIcon from 'common/SpellIcon';

const CALL_DREADSTALKERS_COOLDOWN = 15;
const CALL_DREADSTALKERS_DURATION = 12;
const DEMONIC_EMPOWERMENT_DURATION = 12;
const MILLISECONDS = 1000;

class DemEmpUptimeDreadstalkers extends Analyzer{
  demonicEmpowermentCount = 0;
  totalDreadstalkerTime = DEMONIC_EMPOWERMENT_DURATION * MILLISECONDS;
  lastDemEmpTimestamp = null;
  lastCallDreadstalkersTimestamp = null;
  demEmpCasts = 0;
  callDreadstalkersCasts = 0;
  lastTimeDelta = 0;

  get uptime(){
    return this.totalDreadstalkerTime / (this.callDreadstalkersCasts * CALL_DREADSTALKERS_DURATION * MILLISECONDS);
  }

  get suggestionThresholds(){
    return {
      actual: this.uptime,
      isLessThan: {
        minor: 0.95,
        average: 0.90,
        major: 0.80,
      },
      style: 'percentage',
    };
  }

  on_byPlayer_cast(event){
    const spellId = event.ability.guid;
    if(spellId === SPELLS.CALL_DREADSTALKERS.id){
      this.lastCallDreadstalkersTimestamp = event.timestamp;
      this.callDreadstalkersCasts += 1;
    } else if(spellId === SPELLS.DEMONIC_EMPOWERMENT.id){
      this.demEmpCasts += 1;
      if(this.lastCallDreadstalkersTimestamp !== null &&
        event.timestamp - this.lastCallDreadstalkersTimestamp <= (CALL_DREADSTALKERS_DURATION * MILLISECONDS) &&
        event.timestamp - this.lastDemEmpTimestamp <= (DEMONIC_EMPOWERMENT_DURATION * MILLISECONDS)){
        const timeDelta = (CALL_DREADSTALKERS_COOLDOWN * MILLISECONDS) - (event.timestamp - this.lastCallDreadstalkersTimestamp);
        if(timeDelta > this.lastTimeDelta){ //Avoid adding time for empowerment refreshes.
          this.totalDreadstalkerTime += timeDelta;
        }
        this.lastTimeDelta = timeDelta;
      }
      this.lastDemEmpTimestamp = event.timestamp;
    }
  }

  suggestions(when){
    when(this.suggestionThresholds)
      .addSuggestion((suggest, actual, recommended) => {
        return suggest(
          <React.Fragment>Empowerment of <SpellLink id={SPELLS.CALL_DREADSTALKERS.id} icon/> can be improved. Remember to always empower demons immediately after summoning.</React.Fragment>
        ).icon(SPELLS.CALL_DREADSTALKERS.icon)
          .actual(`${formatPercentage(actual)}% empowered uptime.`)
          .recommended(`>${formatPercentage(recommended)}% is recommended.`);
      });
  }

  statistic(){
    return(
      <StatisticBox icon={<SpellIcon id={SPELLS.CALL_DREADSTALKERS.id}/>} value={`${formatPercentage(this.uptime)} %`} label={'Empowered Dreadstalker Uptime'} />
    );
  }


}

export default DemEmpUptimeDreadstalkers;

@@ -314,89 +314,8 @@
 ERS_
-COOLDOWN = 15;%0Aconst CALL_DREADSTALKERS_DURATION = 12;%0Aconst DEMONIC_EMPOWERMENT_
 DURA
@@ -409,196 +409,63 @@
 %7B%0A  
-demonicEmpowermentCount = 0;%0A  totalDreadstalkerTime = DEMONIC_EMPOWERMENT_DURATION * MILLISECONDS;%0A  lastDemEmpTimestamp = null;%0A  lastCallDreadstalkersTimestamp = null;%0A  demEmpCasts = 0
+totalEmpoweredTime = 0;%0A  unempoweredDreadstalkers = %5B%5D
 ;%0A  
@@ -495,29 +495,8 @@
 = 0;
-%0A  lastTimeDelta = 0;
 %0A%0A  
@@ -526,36 +526,33 @@
 n this.total
-Dreadstalker
+Empowered
 Time / (this
@@ -954,32 +954,35 @@
 %0A      this.
-lastCall
+unempowered
 Dreadstalker
@@ -982,28 +982,22 @@
 stalkers
-Timestamp = 
+.push(
 event.ti
@@ -999,24 +999,25 @@
 nt.timestamp
+)
 ;%0A      this
@@ -1114,87 +1114,58 @@
     
-this.demEmpCasts += 1;%0A      if(this.lastCallDreadstalkersTimestamp !== null &&
+for(const dread of this.unempoweredDreadstalkers)%7B
 %0A   
@@ -1161,32 +1161,35 @@
 lkers)%7B%0A        
+if(
 event.timestamp 
@@ -1194,43 +1194,13 @@
 p - 
-this.lastCallDreadstalkersTimestamp
+dread
  %3C= 
@@ -1247,114 +1247,13 @@
 NDS)
- &&
+)%7B
 %0A  
-      event.timestamp - this.lastDemEmpTimestamp %3C= (DEMONIC_EMPOWERMENT_DURATION * MILLISECONDS))%7B%0A
     
@@ -1298,15 +1298,15 @@
 ERS_
-COOLDOW
+DURATIO
 N * 
@@ -1341,297 +1341,123 @@
 tamp
- - this.lastCallDreadstalkersTimestamp);%0A        if(timeDelta %3E this.lastTimeDelta)%7B //Avoid adding time for empowerment refreshes.%0A          this.totalDreadstalkerTime += timeDelta;%0A        %7D%0A        this.lastTimeDelta = timeDelta;%0A      %7D%0A      this.lastDemEmpTimestamp = event.timestamp
+-dread);%0A          this.totalEmpoweredTime += timeDelta;%0A        %7D%0A      %7D%0A      this.unempoweredDreadstalkers = %5B%5D
 ;%0A  

Update DemEmpUptimeDreadstalkers to be consistent with other pet types.