import CombatantTracker from './CombatantTracker';
import LogEventHandler from './LogEventHandler';
import PetNamesByLang from '../../../../resources/pet_names';
import EmulatorCommon from '../EmulatorCommon';

export default class Encounter {
  constructor(encounterDay, encounterZoneId, encounterZoneName, logLines) {
    this.id = null;
    this.encounterZoneId = encounterZoneId;
    this.encounterZoneName = encounterZoneName;
    this.encounterDay = encounterDay;
    this.logLines = logLines;
    this.initialize();
  }

  initialize() {
    this.language = 'en';
    this.initialOffset = Number.MAX_SAFE_INTEGER;
    this.endStatus = 'Unknown';
    this.startStatus = new Set();
    this.engageAt = Number.MAX_SAFE_INTEGER;
    this.firstPlayerAbility = Number.MAX_SAFE_INTEGER;
    this.firstEnemyAbility = Number.MAX_SAFE_INTEGER;

    const petNames = PetNamesByLang[this.language];

    this.firstLineIndex = 0;

    for (let i = 0; i < this.logLines.length; ++i) {
      const line = this.logLines[i];
      let res = EmulatorCommon.matchStart(line.networkLine);
      if (res) {
        this.firstLineIndex = i;
        this.startStatus.add(res.groups.StartType);
        const startIn = parseInt(res.groups.StartIn);
        if (startIn >= 0)
          this.engageAt = Math.min(line.timestamp + startIn, this.engageAt);
      } else {
        res = EmulatorCommon.matchEnd(line.networkLine);
        if (res) {
          this.endStatus = res.groups.EndType;
        } else if (line.id && line.targetId) {
          if (line.id.startsWith('1') ||
            (line.id.startsWith('4') && petNames.includes(line.name))) {
            // Player or pet ability
            if (line.targetId.startsWith('4') && !petNames.includes(line.targetName)) {
              // Targetting non player or pet
              this.firstPlayerAbility = Math.min(this.firstPlayerAbility, line.timestamp);
            }
          } else if (line.id.startsWith('4') && !petNames.includes(line.name)) {
            // Non-player ability
            if (line.targetId.startsWith('1') || petNames.includes(line.targetName)) {
              // Targetting player or pet
              this.firstEnemyAbility = Math.min(this.firstEnemyAbility, line.timestamp);
            }
          }
        }
      }
      if (res && res.groups && res.groups.language)
        this.language = res.groups.language || this.language;
    }


    if (this.firstPlayerAbility === Number.MAX_SAFE_INTEGER)
      this.firstPlayerAbility = null;

    if (this.firstEnemyAbility === Number.MAX_SAFE_INTEGER)
      this.firstEnemyAbility = null;

    if (this.engageAt === Number.MAX_SAFE_INTEGER)
      this.engageAt = null;

    this.combatantTracker = new CombatantTracker(this.logLines, this.language);
    this.startTimestamp = this.combatantTracker.firstTimestamp;
    this.endTimestamp = this.combatantTracker.lastTimestamp;
    this.duration = this.endTimestamp - this.startTimestamp;

    if (this.initialOffset === Number.MAX_SAFE_INTEGER) {
      if (this.engageAt !== null)
        this.initialOffset = this.engageAt - this.startTimestamp;
      else if (this.firstPlayerAbility !== null)
        this.initialOffset = this.firstPlayerAbility - this.startTimestamp;
      else if (this.firstEnemyAbility !== null)
        this.initialOffset = this.firstEnemyAbility - this.startTimestamp;
      else
        this.initialOffset = 0;
    }

    this.playbackOffset = this.logLines[this.firstLineIndex].offset;

    this.startStatus = [...this.startStatus].sort().join(', ');
  }

  shouldPersistFight() {
    return this.firstPlayerAbility > 0 && this.firstEnemyAbility > 0;
  }
}

@@ -204,16 +204,265 @@
 mmon';%0A%0A
+const isPetName = (name, language = undefined) =%3E %7B%0A  if (language)%0A    return PetNamesByLang%5Blanguage%5D.includes(name);%0A%0A  for (const lang in PetNamesByLang) %7B%0A    if (PetNamesByLang%5Blang%5D.includes(name))%0A      return true;%0A  %7D%0A%0A  return false;%0A%7D;%0A%0A
 export d
@@ -791,34 +791,8 @@
 ) %7B%0A
-    this.language = 'en';%0A
     
@@ -1062,61 +1062,8 @@
 R;%0A%0A
-    const petNames = PetNamesByLang%5Bthis.language%5D;%0A%0A
     
@@ -1768,34 +1768,41 @@
  && 
-p
+isP
 etName
-s.includes(line.nam
+(line.name, this.languag
 e)))
@@ -1891,33 +1891,25 @@
 4') && !
-p
+isP
 etName
-s.includes
 (line.ta
@@ -1908,32 +1908,47 @@
 (line.targetName
+, this.language
 )) %7B%0A           
@@ -2140,34 +2140,41 @@
 && !
-p
+isP
 etName
-s.includes(line.nam
+(line.name, this.languag
 e)) 
@@ -2262,25 +2262,17 @@
  %7C%7C 
-p
+isP
 etName
-s.includes
 (lin
@@ -2283,16 +2283,31 @@
 rgetName
+, this.language
 )) %7B%0A   
@@ -2599,16 +2599,59 @@
 %0A    %7D%0A%0A
+    this.language = this.language %7C%7C 'en';%0A
 %0A    if 

Fix Encounter pet name checks not respecting language (#2976)