import daRanksView from '../_dataAccess/daRanksView';
import fallback from '../system/fallback';
import lastActivityToDays from '../common/lastActivityToDays';
import { nowSecs } from '../support/now';
import partial from '../common/partial';
import {
  act, cur, gxp, lvl, max, utc, vl,
} from './guildTracker/indexConstants';
import { get, set } from '../system/idb';

let oldArchive;
let guild;

function pushNewRecord(member) {
  oldArchive.members[member.name].push([
    lastActivityToDays(member.last_activity),
    member.current_stamina,
    member.level,
    member.max_stamina,
    nowSecs,
    member.vl,
    member.guild_xp,
  ]);
}

function initMember(member) {
  if (!oldArchive.members[member.name]) {
    oldArchive.members[member.name] = [];
    pushNewRecord(member);
  }
}

const type2tests = [
  // Has current stam changed ?
  // probably want a weighted percentage here
  // Might only care if it has dropped significantly ?
  (archive, current) => current.current_stamina !== archive[cur],
  // Has Max Stam increased ?
  // probably want a weighted percentage here
  (archive, current) => current.max_stamina > archive[max],
  // Has level changed ?
  (archive, current) => current.level !== archive[lvl],
  // Has VL changed ?
  (archive, current) => current.vl !== archive[vl],
  // Has GXP changed ?
  // probably want a weighted percentage here
  (archive, current) => current.guild_xp !== archive[gxp],
];

function change(archiveRecord, member, test) {
  return test(archiveRecord, member);
}

function hasChanged(archiveRecord, member) {
  return type2tests.some(partial(change, archiveRecord, member));
}

function upsert(archiveRecord, member) {
  if (hasChanged(archiveRecord, member)) {
    pushNewRecord(member);
  } else {
    // eslint-disable-next-line no-param-reassign
    archiveRecord[act] = lastActivityToDays(member.last_activity);
    // eslint-disable-next-line no-param-reassign
    archiveRecord[utc] = nowSecs;
  }
}

function processMemberRecord(newArchive, member) {
  initMember(member);
  const archiveMember = oldArchive.members[member.name];
  const archiveRecord = archiveMember[archiveMember.length - 1];
  const archiveAge = nowSecs - archiveRecord[utc];
  if (archiveAge >= 86100) {
    upsert(archiveRecord, member);
  }
  // eslint-disable-next-line no-param-reassign
  newArchive.members[member.name] = oldArchive.members[member.name];
}

function processRank(newArchive, rank) {
  rank.members.forEach(partial(processMemberRecord, newArchive));
}

function doMerge() { // jQuery.min
  const newArchive = { lastUpdate: nowSecs, members: {} };
  guild.r.forEach(partial(processRank, newArchive));
  set('fsh_guildActivity', newArchive);
}

function gotGuild(data) {
  if (data && data.r) {
    guild = data;
    doMerge();
  }
}

function gotActivity(data) { // jQuery.min
  if (data) {
    oldArchive = data;
  } else {
    oldArchive = { lastUpdate: 0, members: {} };
  }
  if (nowSecs > fallback(oldArchive.lastUpdate, 0) + 300) { // 5 mins - probably want to increase
    daRanksView().then(gotGuild);
  }
}

export default function guildActivity() { // jQuery.min
  get('fsh_guildActivity').then(gotActivity);
}

@@ -1,58 +1,4 @@
-import daRanksView from '../_dataAccess/daRanksView';%0A
 impo
@@ -36,16 +36,16 @@
 lback';%0A
+
 import l
@@ -182,16 +182,76 @@
 rtial';%0A
+import ranksView from '../_dataAccess/fallbacks/ranksView';%0A
 import %7B
@@ -388,19 +388,12 @@
 hive
-;%0Alet guild
+ = 0
 ;%0A%0Af
@@ -2523,16 +2523,21 @@
 doMerge(
+guild
 ) %7B // j
@@ -2741,40 +2741,15 @@
 data
- && data
+?
 .r) %7B%0A
-    guild = data;%0A
     
@@ -2756,16 +2756,20 @@
 doMerge(
+data
 );%0A  %7D%0A%7D
@@ -2762,32 +2762,38 @@
 e(data);%0A  %7D%0A%7D%0A%0A
+async 
 function gotActi
@@ -2823,24 +2823,8 @@
 min%0A
-  if (data) %7B%0A  
   ol
@@ -2842,37 +2842,11 @@
 data
-;%0A  %7D else %7B%0A    oldArchive =
+ ??
  %7B l
@@ -2874,20 +2874,16 @@
 : %7B%7D %7D;%0A
-  %7D%0A
   if (no
@@ -2980,11 +2980,28 @@
     
-daR
+const json = await r
 anks
@@ -3010,22 +3010,22 @@
 ew()
-.then(
+;%0A    
 gotGuild
 );%0A 
@@ -3020,16 +3020,21 @@
 gotGuild
+(json
 );%0A  %7D%0A%7D
@@ -3050,16 +3050,22 @@
 default 
+async 
 function
@@ -3096,19 +3096,39 @@
 uery.min
+s
 %0A  
+const data = await 
 get('fsh
@@ -3147,14 +3147,12 @@
 ty')
-.then(
+;%0A  
 gotA
@@ -3158,13 +3158,18 @@
 Activity
+(data
 );%0A%7D%0A

revert guild activity to fallback (#338)