import * as ajax from '../support/ajax';
import * as system from '../support/system';

// Taking the Not Save in case they add new enhancements.
var notSave = ['Breaker', 'Protection', 'Master Thief', 'Protect Gold',
  'Disarm', 'Duelist', 'Thievery', 'Master Blacksmith', 'Master Crafter',
  'Fury Caster', 'Master Inventor', 'Sustain'];
var combatLog = [];

function combatResponse(e, data) { // Native
  // If bad response do nothing.
  if (data.response.response !== 0) {return;}
  var l;
  var i;
  var combatData = {};
  combatData.combat = data.response.data;
  if (combatData.combat.inventory_id) {
    combatData.combat.drop = combatData.combat.item.id;
  }

  combatData.player = {};
  combatData.player.buffs = {};
  combatData.player.enhancements = {};
  l = data.player.buffs.length;
  for (i = 0; i < l; i += 1) { // loop through buffs, only need to keep CA and Doubler 54 = ca, 26 = doubler */
    var buff = data.player.buffs[i];
    if (buff.id === 54 || buff.id === 26) {
      combatData.player.buffs[buff.id] = parseInt(buff.level, 10);
    }
  }
  if (data.player.enhancements) {
    l = data.player.enhancements.length;
    for (i = 0; i < l; i += 1) { // loop through enhancements
      var enh = data.player.enhancements[i];
      if (notSave.indexOf(enh.name) === -1) {
        combatData.player.enhancements[enh.name] = enh.value;
      }
    }
  }
  combatData.time = data.time;
  combatLog.push(combatData);
  ajax.setForage('fsh_combatLog', combatLog);
}

function gotCombatLog(data) { // jQuery.min
  if (data) {combatLog = data;}
  $.subscribe('2-success.action-response', combatResponse);
}

export function init() { // jQuery.min
  if (system.getValue('keepLogs')) {
    ajax.getForage('fsh_combatLog').done(gotCombatLog);
  }
}

@@ -351,16 +351,335 @@
 og = %5B%5D;
+%0Avar combatData;%0A%0Afunction storeBuffs(buff) %7B // Native%0A  if (buff.id === 54 %7C%7C buff.id === 26) %7B%0A    combatData.player.buffs%5Bbuff.id%5D = parseInt(buff.level, 10);%0A  %7D%0A%7D%0A%0Afunction storeEnhancements(enh) %7B // Native%0A  if (notSave.indexOf(enh.name) === -1) %7B%0A    combatData.player.enhancements%5Benh.name%5D = enh.value;%0A  %7D%0A%7D
 %0A%0Afuncti
@@ -802,30 +802,8 @@
 %7D%0A  
-var l;%0A  var i;%0A  var 
 comb
@@ -1056,20 +1056,16 @@
  = %7B%7D;%0A 
- l =
  data.pl
@@ -1079,46 +1079,28 @@
 ffs.
-length;%0A  for (i = 0; i %3C l; i += 1) %7B
+forEach(storeBuffs);
  // 
@@ -1177,169 +1177,8 @@
 bler
- */%0A    var buff = data.player.buffs%5Bi%5D;%0A    if (buff.id === 54 %7C%7C buff.id === 26) %7B%0A      combatData.player.buffs%5Bbuff.id%5D = parseInt(buff.level, 10);%0A    %7D%0A  %7D
 %0A  i
@@ -1215,12 +1215,8 @@
 %0A   
- l =
  dat
@@ -1241,244 +1241,64 @@
 nts.
-length;%0A    for (i = 0; i %3C l; i += 1) %7B // loop through enhancements%0A      var enh = data.player.enhancements%5Bi%5D;%0A      if (notSave.indexOf(enh.name) === -1) %7B%0A        combatData.player.enhancements%5Benh.name%5D = enh.value;%0A      %7D%0A    %7D
+forEach(storeEnhancements); // loop through enhancements
 %0A  %7D

break up combat logger