// ==UserScript==
// @id             iitc-plugin-highlight-portals-by-ap-by-energy-relative@vita10gy
// @name           IITC plugin: highlight portals by ap/energy (relative)
// @version        0.1.1.@@DATETIMEVERSION@@
// @namespace      https://github.com/jonatkins/ingress-intel-total-conversion
// @updateURL      @@UPDATEURL@@
// @downloadURL    @@DOWNLOADURL@@
// @description    [@@BUILDNAME@@-@@BUILDDATE@@] Uses the fill color of the portals to denote AP/Energy value relative to what's currently on the screen. Brighter is better. Orange means your standard 8 down 8 up swap.  
// @include        https://www.ingress.com/intel*
// @include        http://www.ingress.com/intel*
// @match          https://www.ingress.com/intel*
// @match          http://www.ingress.com/intel*
// ==/UserScript==

function wrapper() {
// ensure plugin framework is there, even if iitc is not yet loaded
if(typeof window.plugin !== 'function') window.plugin = function() {};


// PLUGIN START ////////////////////////////////////////////////////////

// use own namespace for plugin
window.plugin.portalHighligherPortalAPPerEnergyRelative = function() {};

window.plugin.portalHighligherPortalAPPerEnergyRelative.minAP = null;
window.plugin.portalHighligherPortalAPPerEnergyRelative.maxAP = null;
//This is the AP for a run of the mill takedown/putback
window.plugin.portalHighligherPortalAPPerEnergyRelative.baseSwapAP = 2350;


window.plugin.portalHighligherPortalAPPerEnergyRelative.highlight = function(data) {
  var d = data.portal.options.details;
  var color = 'red';
  
  if(window.plugin.portalHighligherPortalAPPerEnergyRelative.minAP == null ||
     window.plugin.portalHighligherPortalAPPerEnergyRelative.maxAP == null) {
    window.plugin.portalHighligherPortalAPPerEnergyRelative.calculateAPLevels();
  }
  var minApE = window.plugin.portalHighligherPortalAPPerEnergyRelative.minAP;
  var maxApE = window.plugin.portalHighligherPortalAPPerEnergyRelative.maxAP;
  
  if(PLAYER.team !== d.controllingTeam.team) {
    var ap = getAttackApGain(d);
    var energy = getCurrentPortalEnergy(d);
    if(energy < 1) {
      energy = 1;
    }
    portal_ap = ap.enemyAp;
      
    var opacity =  1;
    if(minApE !== maxApE) {
      opacity = ((ap.enemyAp / energy) - minApE) / (maxApE - minApE);
    }
    
    if(opacity < 0) {
      opacity = 0;
    }
    if(opacity > 1) {
      opacity = 1;
    }
    data.portal.setStyle({fillColor: color, fillOpacity: opacity});
    window.COLOR_SELECTED_PORTAL = '#f0f';
  }
}

window.plugin.portalHighligherPortalAPPerEnergyRelative.resetAPLevels = function() {
  window.plugin.portalHighligherPortalAPPerEnergyRelative.minAP = null;
  window.plugin.portalHighligherPortalAPPerEnergyRelative.maxAP = null;
}

window.plugin.portalHighligherPortalAPPerEnergyRelative.calculateAPLevels = function() {
  var displayBounds = map.getBounds();
  $.each(window.portals, function(qk, portal) {
    if(displayBounds.contains(portal.getLatLng())) {
      if(PLAYER.team !== portal.options.details.controllingTeam.team) {
        var ap = getAttackApGain(portal.options.details);
        var energy = getCurrentPortalEnergy(portal.options.details);
        if(energy < 1) {
          energy = 1;
        }
        var portal_ap = ap.enemyAp / energy;
        if(window.plugin.portalHighligherPortalAPPerEnergyRelative.minAP === null ||
           portal_ap < window.plugin.portalHighligherPortalAPPerEnergyRelative.minAP) {
          window.plugin.portalHighligherPortalAPPerEnergyRelative.minAP = portal_ap;
        }
        if(window.plugin.portalHighligherPortalAPPerEnergyRelative.maxAP === null ||
           portal_ap > window.plugin.portalHighligherPortalAPPerEnergyRelative.maxAP) {
          window.plugin.portalHighligherPortalAPPerEnergyRelative.maxAP = portal_ap;
        }
      
      }
    }
  });  
}

  

var setup =  function() {
  window.addPortalHighlighter('AP/Energy (Relative)', window.plugin.portalHighligherPortalAPPerEnergyRelative.highlight);
  window.addHook('requestFinished', window.plugin.portalHighligherPortalAPPerEnergyRelative.resetAPLevels);
  
}

// PLUGIN END //////////////////////////////////////////////////////////

if(window.iitcLoaded && typeof setup === 'function') {
  setup();
} else {
  if(window.bootPlugins)
    window.bootPlugins.push(setup);
  else
    window.bootPlugins = [setup];
}
} // wrapper end
// inject code into site context
var script = document.createElement('script');
script.appendChild(document.createTextNode('('+ wrapper +')();'));
(document.body || document.head || document.documentElement).appendChild(script);
