"use strict";

var $ = require('jquery'),
    _ = require('lodash'),
    L = require('leaflet'),
    Bacon = require('baconjs'),
    BU = require('treemap/baconUtils'),
    buttonEnabler = require('treemap/buttonEnabler');

var config,  // Module-level config set in `init` and read by helper functions
    map,
    popup,  // Most recent popup (so it can be deleted)
    plotMarker,
    $fullDetailsButton;

function idToPlotDetailUrl(id) {
    if (id) {
        return config.instance.url + 'features/' + id + '/';
    } else {
        return '';
    }
}

function init(options) {
    config = options.config;
    map = options.map;
    plotMarker = options.plotMarker;
    $fullDetailsButton = options.$fullDetailsButton;

    var inMyMode = options.inMyMode, // function telling if my mode is active
        inlineEditForm = options.inlineEditForm,
        $accordionSection = options.$treeDetailAccordionSection,
        $buttonGroup = options.$buttonGroup;

    var clickedIdStream = map.utfEvents
        .filter(inMyMode)
        .map('.data.' + config.utfGrid.mapfeatureIdKey);


    var popupHtmlStream = BU.fetchFromIdStream(clickedIdStream,
                                               getPlotPopupContent);

    var accordionHtmlStream = BU.fetchFromIdStream(clickedIdStream,
                                                   getPlotAccordionContent,
                                                   '');


    var plotUrlStream = clickedIdStream
        .map(idToPlotDetailUrl);

    plotUrlStream.onValue($fullDetailsButton, 'attr', 'href');
    plotUrlStream.onValue(inlineEditForm.setUpdateUrl);

    // Leaflet needs both the content and a coordinate to
    // show a popup, so zip map clicks together with content
    // requested via ajax
    BU.leafletEventStream(map, 'click')
       .filter(inMyMode)
       .map('.latlng')
       .zip(popupHtmlStream, makePopup) // TODO: size is not being sent to makePopup
       .onValue(showPlotDetailPopup);

    accordionHtmlStream.onValue(function () { $buttonGroup.show(); });

    var featureTypeStream = accordionHtmlStream.map($)
            .map('.attr', 'data-map-feature-type');

    featureTypeStream
        .decode({plot: 'show', resource: 'hide'})
        .onValue($accordionSection.collapse);

    featureTypeStream
        .decode({plot: 'visible', resource: 'hidden'})
        .assign($('#quick-edit-button'), 'css', 'visibility');

    featureTypeStream
        .decode({plot: config.trans.treeDetails,
                 resource: config.trans.resourceDetails})
        .assign($('#accordion-map-feature-detail-tab'), 'html');

    var showTreeDetailLink = $accordionSection.parent().find('a');
    showTreeDetailLink.on('click', function(event) {
        if ($('#map-feature-accordion').html().length === 0) {
            event.stopPropagation();
        }
    });

    // Need to manually wire this here or we wont get the accordion effect
    $accordionSection.collapse({
        parent: $('#map-info'),
        toggle: false
    });

    $accordionSection.collapse('hide');
}

function getPlotPopupContent(id) {
    var search = $.ajax({
        url: config.instance.url + 'features/' + id + '/popup',
        type: 'GET',
        dataType: 'html'
    });
    return Bacon.fromPromise(search);
}

function makePopup(latLon, html, size) {
    size = size || {};
    if (latLon && html) {
        var popupOptions = {
            maxWidth: size.width || 320,
            maxHeight: size.height || 130
        };

        var isPlot = $(html).data('mapfeature-type') === 'Plot';

        var popup = L.popup(popupOptions)
            .setLatLng(latLon)
            .setContent(html);

        popup.isPlot = isPlot;

        return popup;

        //TODO: Pan map if out of view
    } else {
        return null;
    }
}

function showPlotDetailPopup(newPopup) {
    if (popup) {
        map.closePopup(popup);
    }

    popup = newPopup;

    plotMarker.useTreeIcon(popup.isPlot);

    if (popup) {
        // Add the popup
        map.openPopup(popup);

        // Move the plot marker to this location
        plotMarker.place(popup._latlng);
        plotMarker.bindPopup(popup);

    } else {
        plotMarker.unbindPopup();
        plotMarker.hide();
    }

    buttonEnabler.run({ config: config });

}

function getPlotAccordionContent(id) {
    var search = $.ajax({
        url: config.instance.url + 'features/' + id + '/detail',
        type: 'GET',
        dataType: 'html'
    });
    return Bacon.fromPromise(search);
}

function deactivate() {
    if (popup) {
        plotMarker.unbindPopup();
        map.closePopup(popup);
    }
}

module.exports = {
    name: 'browseTrees',
    init: init,
    deactivate: deactivate
};

@@ -2049,133 +2049,132 @@
     
-var featureTypeStream = accordionHtmlStream.map($)%0A            .map('.attr', 'data-map-feature-type');%0A%0A    featureTypeStream
+accordionHtmlStream.assign($('#map-feature-accordion'), 'html');%0A%0A    accordionHtmlStream%0A        .map(BU.isDefinedNonEmpty)
 %0A   
@@ -2187,20 +2187,20 @@
 decode(%7B
-plot
+true
 : 'show'
@@ -2201,23 +2201,20 @@
 'show', 
-resourc
+fals
 e: 'hide
@@ -2234,16 +2234,23 @@
 onValue(
+_.bind(
 $accordi
@@ -2267,16 +2267,144 @@
 collapse
+, $accordionSection));%0A%0A    var featureTypeStream = accordionHtmlStream.map($)%0A            .map('.attr', 'data-map-feature-type'
 );%0A%0A    

Fix map feature sidebar