import add from '../support/task';
import addStatTotalToMouseover from '../common/addStatTotalToMouseover';
import ajaxifyProfileSections from './ajaxifyProfileSections';
import {closestForm} from '../common/closest';
import colouredDots from '../common/colouredDots';
import components from './components/components';
import dontPost from '../common/dontPost';
import fallback from '../system/fallback';
import fastDebuff from './debuff';
import getElementsByTagName from '../common/getElementsByTagName';
import getText from '../common/getText';
import getUrlParameter from '../system/getUrlParameter';
import highlightPvpProtection from './highlightPvpProtection';
import injectFastWear from './fastWear';
import jQueryNotPresent from '../common/jQueryNotPresent';
import nekidBtn from './nekidBtn';
import on from '../common/on';
import {pCC} from '../support/layout';
import playerId from '../common/playerId';
import playerName from '../common/playerName';
import {profileInjectGuildRel} from './profileInjectGuildRel';
import profileInjectQuickButton from './profileInjectQuickButton';
import profileParseAllyEnemy from './profileAllyEnemy';
import profileRenderBio from './bio/bio';
import querySelector from '../common/querySelector';
import quickWearLink from './quickWearLink';
import selectAllLink from './selectAllLink';
import storeVL from './storeVL';
import updateNmv from './updateNmv';
import updateStatistics from './updateStatistics';

function ifSelf(self) {
  if (self) {
    // self inventory
    fastDebuff();
    profileParseAllyEnemy();
    injectFastWear();
    components();
    quickWearLink();
    selectAllLink();
    storeVL();
    nekidBtn();
    ajaxifyProfileSections();
  }
}

function guildRelationship(avyImg, playername, self) {
  // Must be before profileInjectQuickButton
  profileInjectGuildRel(self);
  // It sets up guildId and currentGuildRelationship
  var playerid = fallback(getUrlParameter('player_id'), playerId());
  profileInjectQuickButton(avyImg, playerid, playername);
}

function updateDom(avyImg, playername, self) {
  ifSelf(self);
  guildRelationship(avyImg, playername, self);
  updateNmv();
  updateStatistics();
  highlightPvpProtection();
  add(3, profileRenderBio, [self]);
  addStatTotalToMouseover();
  add(3, colouredDots);
}

function updateUrl(e) {
  e.preventDefault();
  dontPost(closestForm(e.target).parentNode);
}

function allowBack(self) {
  if (!self) {
    on(querySelector('#profileRightColumn'), 'submit', updateUrl);
  }
}

export default function injectProfile() { // Legacy
  if (jQueryNotPresent()) {return;}
  var avyImg = querySelector('#profileLeftColumn img[oldtitle*="\'s Avatar"]');
  if (!avyImg) {return;}
  var playername = getText(getElementsByTagName('h1', pCC)[0]);
  var self = playername === playerName();
  updateDom(avyImg, playername, self);
  allowBack(self);
}

@@ -160,24 +160,27 @@
 eSections';%0A
+// 
 import %7Bclos
@@ -311,24 +311,27 @@
 omponents';%0A
+// 
 import dontP
@@ -798,24 +798,27 @@
 /nekidBtn';%0A
+// 
 import on fr
@@ -2292,24 +2292,27 @@
 edDots);%0A%7D%0A%0A
+// 
 function upd
@@ -2323,16 +2323,19 @@
 rl(e) %7B%0A
+// 
   e.prev
@@ -2348,16 +2348,19 @@
 ault();%0A
+// 
   dontPo
@@ -2393,27 +2393,33 @@
 arentNode);%0A
-%7D%0A%0A
+// %7D%0A%0A// 
 function all
@@ -2429,24 +2429,27 @@
 ack(self) %7B%0A
+// 
   if (!self)
@@ -2447,24 +2447,27 @@
 f (!self) %7B%0A
+// 
     on(query
@@ -2521,20 +2521,26 @@
 teUrl);%0A
+// 
   %7D%0A
+// 
 %7D%0A%0Aexpor
@@ -2873,16 +2873,19 @@
 self);%0A 
+ //
  allowBa

disable allowBack for Profile - needs investigation