// ==UserScript==
// @name         Warcraft Logs Enhancement
// @namespace    https://github.com/icyblade/warcraftlogs_enhancement
// @version      0.1
// @description  Some Enhancement Scripts of Warcraft Logs
// @author       swqsldz, kingofpowers, icyblade
// @match        https://www.warcraftlogs.com/*
// @run-at       document-idle
// ==/UserScript==

const attributes = ['critSpell', 'hasteSpell', 'mastery', 'versatilityDamageDone'];
const columnNames = ['Crit', 'Haste', 'Mastery', 'Versatility'];

const regex = /\/reports\/([\S\s]+?)#fight=([0-9]+)/;

// initialize attribute columns
for (var i = 0; i < attributes.length; i++) {
    $('<th class="sorting ui-state-default">' + columnNames[i] + '</th>').insertBefore('th.zmdi.zmdi-flag.sorting.ui-state-default');
}
for (var i = 0; i < attributes.length; i++) {
    $('<td class="attr-' + attributes[i] + '"></td>').insertBefore('td.zmdi.zmdi-flag');
}

// extract fights from ranking page
var PlayerList = new Array();

$('td.unique-gear').parent().each(function() {
    var player = new Object();

    player.rowID = $(this).attr('id');
    player.name = $(this).find('.players-table-name .main-table-player').text();

    var href = $(this).find('.players-table-name .main-table-player').attr('href');
    player.logID = href.match(regex)[1];
    player.fightID = href.match(regex)[2];

    PlayerList.push(player);
});


function loadFights(index) {
    $.ajax({
        type: 'GET',
        url: 'https://www.warcraftlogs.com/reports/fights_and_participants/' + PlayerList[index].logID + '/0',
        dataType: 'json',
        success: function(data) {
            console.log(index);
            callback_fights(data, index);
        }
    });
}

function loadStats(rowID, logID, fightID, timestamp, sourceID) {
    $.ajax({
        type: 'GET',
        url: 'https://www.warcraftlogs.com/reports/summary_events/' + logID + '/' + fightID + '/' + timestamp + '/' + (timestamp + 3000) + '/' + sourceID + '/0/Any/0/-1.0.-1/0',
        dataType: 'json',
        success: function(data) {
            callback_stats(data, rowID, logID, fightID, timestamp, sourceID);
        }
    });
}

function callback_stats(data, rowID, logID, fightID, timestamp, sourceID) {
    for (var key in attributes) {
        try {
            $('#' + rowID + ' .attr-' + attributes[key]).html(data.events[0][attributes[key]]);
        } catch (e) {
            console.error(e);
            console.error(rowID);
            console.error(data);
        }
    }
}

function callback_fights(data, idx) {
    PlayerList[idx].fight = data;

    for (let j in PlayerList[idx].fight.friendlies) {
        if (PlayerList[idx].fight.friendlies[j].name == PlayerList[idx].name) {
            PlayerList[idx].sourceID = PlayerList[idx].fight.friendlies[j].id;
            break;
        }
    }

    for (let j in PlayerList[idx].fight.fights) {
        if (PlayerList[idx].fight.fights[j].id == PlayerList[idx].fightID) {
            PlayerList[idx].timestamp = PlayerList[idx].fight.fights[j].start_time;
            break;
        }
    }

    loadStats(PlayerList[idx].rowID, PlayerList[idx].logID, PlayerList[idx].fightID, PlayerList[idx].timestamp, PlayerList[idx].sourceID);

    idx++;

    if (idx >= PlayerList.length) {
        console.log(PlayerList);
        return;
    }

    loadFights(idx);
}

loadFights(0);

@@ -351,17 +351,16 @@
 cript==%0A
-%0A
 const at
@@ -500,17 +500,16 @@
 lity'%5D;%0A
-%0A
 const re
@@ -555,16 +555,75 @@
 9%5D+)/;%0A%0A
+var PlayerList = new Array();%0A%0Afunction initialize() %7B%0A    
 // initi
@@ -646,16 +646,20 @@
 columns%0A
+    
 for (var
@@ -688,32 +688,36 @@
 .length; i++) %7B%0A
+    
     $('%3Cth class
@@ -834,18 +834,26 @@
 ault');%0A
-%7D%0A
+    %7D%0A    
 for (var
@@ -886,24 +886,28 @@
 gth; i++) %7B%0A
+    
     $('%3Ctd c
@@ -983,19 +983,27 @@
 flag');%0A
-%7D%0A%0A
+    %7D%0A%0A    
 // extra
@@ -1034,39 +1034,12 @@
 age%0A
-var PlayerList = new Array();%0A%0A
+    
 $('t
@@ -1077,24 +1077,28 @@
 unction() %7B%0A
+    
     var play
@@ -1117,24 +1117,28 @@
 ect();%0A%0A    
+  
+  
 player.rowID
@@ -1156,24 +1156,28 @@
 attr('id');%0A
+    
     player.n
@@ -1246,24 +1246,28 @@
 ext();%0A%0A    
+   
+ 
 var href = $
@@ -1330,24 +1330,28 @@
 tr('href');%0A
+    
     player.l
@@ -1379,16 +1379,20 @@
 ex)%5B1%5D;%0A
+    
     play
@@ -1427,16 +1427,20 @@
 x)%5B2%5D;%0A%0A
+    
     Play
@@ -1460,20 +1460,25 @@
 layer);%0A
+    
 %7D);%0A
+%7D
 %0A%0Afuncti
@@ -3435,19 +3435,197 @@
 %0A%7D%0A%0A
-loadFights(0
+function delayLoad() %7B%0A    if ($('.ranking-table tr:eq(5)').length === 0) %7B%0A        setTimeout(delayLoad, 1000);%0A    %7D else %7B%0A        initialize();%0A        loadFights(0);%0A    %7D%0A%7D%0A%0AdelayLoad(
 );%0A

add delay load