// ==UserScript==
// @id             iitc-plugin-show-linked-portals@fstopienski
// @name           IITC plugin: Show linked portals
// @category       Portal Info
// @version        0.1.0.@@DATETIMEVERSION@@
// @namespace      https://github.com/jonatkins/ingress-intel-total-conversion
// @updateURL      @@UPDATEURL@@
// @downloadURL    @@DOWNLOADURL@@
// @description    [@@BUILDNAME@@-@@BUILDDATE@@] Tries to show the linked portals (image, name and address) in portal detail view and jump to linked portal on click
// @include        https://www.ingress.com/intel*
// @include        http://www.ingress.com/intel*
// @match          https://www.ingress.com/intel*
// @match          http://www.ingress.com/intel*
// @grant          none
// ==/UserScript==
@@PLUGINSTART@@
// PLUGIN START ////////////////////////////////////////////////////////
/*
* 0.0.1 initial release, show images, names and addresses of linked portal in portal detailview
* - mouse click of the linked portal image selected the portal and adjust map
* - click of "Linked Portal is out of range" zoom a step out
*/
// use own namespace for plugin
window.plugin.showLinkedPortal = function () {
};
window.plugin.showLinkedPortal.handleUpdate = function () {
    if (!requests.isLastRequest('getThinnedEntitiesV4')) {
        return;
    }
}
window.plugin.showLinkedPortal.portalDetail = function (data) {
    // don't render linked portal data if portal is neutral.
    // (the data can remain sometimes - when a portal decays?)
    if (data.portalDetails.controllingTeam.team == 'NEUTRAL')
        return;
    var d = data.portalDetails.portalV2,
        c = 1;
    //get linked portals
    $(d.linkedEdges).each(function () {
        var portalInfo = window.plugin.showLinkedPortal.getPortalByGuid(this.otherPortalGuid, this.isOrigin);
        $('#portaldetails').append('
' + portalInfo + '
');
        c = c + 1;
    });
    $('.showLinkedPortalLink:not(.outOfRange)').bind('click', function () {
        var guid = $(this).attr('data-guid');
        if (window.portals[guid] !== undefined) {
            window.selectPortal($(this).attr('data-guid'));
            window.renderPortalDetails(window.selectedPortal);
            var portalDetails = window.portals[guid].options.details;
            var lat0 = portalDetails.locationE6.latE6 / 1E6;
            var lon0 = portalDetails.locationE6.lngE6 / 1E6;
            var Rlatlng = [lat0, lon0];
            map.setView(Rlatlng, map.getZoom());
        }
        else {
            // TODO: instead of just zooming out one level, check the link data for the start+end coordinates,
            // and fit the map view to the bounding box
            map.setZoom((map.getZoom() - 1));
        }
    });
}
window.plugin.showLinkedPortal.getPortalByGuid = function (guid,isorigin) {
    var linkDirection = $('').text(isorigin?'↴ outgoing link':'↳ incoming link');
    var portalInfoString;
    if (window.portals[guid] !== undefined) {
        var portalDetails = window.portals[guid].options.details;
        var portalNameAddressAlt = "'" + portalDetails.portalV2.descriptiveText.TITLE + "' (" + portalDetails.portalV2.descriptiveText.ADDRESS + ")";
        var portalNameAddressTitle = $('').append($('').text(portalDetails.portalV2.descriptiveText.TITLE))
                                                .append($('
'))
                                                .append($('').text('(' + portalDetails.portalV2.descriptiveText.ADDRESS + ')'))
                                                .append($('
'))
                                                .append(linkDirection)
                                                .html();
        var imageUrl = getPortalImageUrl(portalDetails);
        portalInfoString = $('').html($('![]() ').attr('src', imageUrl)
                                                       .attr('class', 'minImg')
                                                       .attr('alt', portalNameAddressAlt)
                                                       .attr('title', portalNameAddressTitle))
                                      .html();
    } else {
        var title = $('').append($('').text('Zoom out'))
                               .append($('
').attr('src', imageUrl)
                                                       .attr('class', 'minImg')
                                                       .attr('alt', portalNameAddressAlt)
                                                       .attr('title', portalNameAddressTitle))
                                      .html();
    } else {
        var title = $('').append($('').text('Zoom out'))
                               .append($('
'))
                               .append(linkDirection)
                               .html();
        portalInfoString = $('').html($('').attr('class','outOfRange')
                                                        .attr('title',title)
                                                        .text('Portal out of range.'))
                                      .html();
    }
    return portalInfoString;
};
window.plugin.showLinkedPortal.setupCallback = function () {
    // make the value update when the map data updates
    var handleDataResponseOrig = window.handleDataResponse;
    window.handleDataResponse = function (data, textStatus, jqXHR) {
        handleDataResponseOrig(data, textStatus, jqXHR);
        window.renderPortalDetails(window.selectedPortal);
    }
}
var setup = function () {
    window.addHook('requestFinished', window.plugin.showLinkedPortal.handleUpdate);
    window.addHook('portalDetailsUpdated', window.plugin.showLinkedPortal.portalDetail);
    $('head').append('');
    window.plugin.showLinkedPortal.setupCallback();
}
// PLUGIN END //////////////////////////////////////////////////////////
@@PLUGINEND@@