'use strict';

var ConfigParser           = require('./config')
var dataServiceFactory     = require('./dataService')
var mapFactory             = require('../wrappers/map')
var popupPresenterFactory  = require('./popupPresenter')
var filterFactory          = require('./geojsonFeatureFilterer')
var stylePresenterFactory  = require('./layerStylePresenter')
var key                    = require('./key')
var tileLayerManager       = require('./tileLayerManager')
var mapKeyController       = require('./keyController')
var geojsonLayerController = require('./geojsonLayerController')
var tileLayerController    = require('./tileLayerController')
var dataServiceController  = require('./dataServiceController')

var create = function (config) {
  config = new ConfigParser(config)
  var mapConfig         = config.getMap()
  var keyConfig         = config.getKey()
  var layerConfigs      = config.getLayers()
  var dataSourceConfigs = config.getDataSources()
  var map               = mapFactory(mapConfig)

  tileLayerController({
    map: map,
    manager: tileLayerManager(mapConfig.tileLayers)
  })

  dataServiceController = dataServiceController(dataSourceConfigs)

  function clickLayer(layerConfig) {
    var layerStylePresenter = stylePresenterFactory(layerConfig.styles.layer)
    var dataService = dataServiceController.getDataServiceForLayer(layerConfig)

    map.on('click', (event) => {
      var lng         = event.latlng.lng
      var lat         = event.latlng.lat

      dataService.url = dataService.url.replace('{x}', lng).replace('{y}', lat)
      dataService.on('data', () => {
        map.setGeojsonLayer(layerConfig.name, {
          geojson: dataService.getData(),
          style: (properties) => layerStylePresenter.present(properties)
        })
        map.showGeojsonLayer(layerConfig.name)
      })

      dataService.start()
    })
  }

  if (typeof layerConfigs !== 'undefined') {
    var keyController
    if (typeof keyConfig !== 'undefined') {
      keyController = mapKeyController({
        map: map,
        key: key(keyConfig),
        layerNames: config.getLayerNames()
      })
    }

    geojsonLayerController({
      map: map,
      keyController: keyController,
      dataServiceController: dataServiceController,
      popupPresenterFactory: popupPresenterFactory,
      filterFactory: filterFactory,
      stylePresenterFactory: stylePresenterFactory
    }).addLayersFromConfig(layerConfigs)

    //add feature layers
    layerConfigs.forEach((layerConfig) => {
      if (layerConfig.type === 'click')
        clickLayer(layerConfig)
    })
  }

  return map
}

var Mappy = {
  create: create
}

module.exports = Mappy

@@ -1365,206 +1365,8 @@
 g)%0A%0A
-    map.on('click', (event) =%3E %7B%0A      var lng         = event.latlng.lng%0A      var lat         = event.latlng.lat%0A%0A      dataService.url = dataService.url.replace('%7Bx%7D', lng).replace('%7By%7D', lat)%0A  
     
@@ -1392,26 +1392,24 @@
 a', () =%3E %7B%0A
-  
       map.se
@@ -1442,18 +1442,16 @@
 name, %7B%0A
-  
         
@@ -1486,26 +1486,24 @@
 (),%0A        
-  
 style: (prop
@@ -1559,23 +1559,19 @@
 )%0A      
-  
 %7D)%0A
-  
       ma
@@ -1611,21 +1611,215 @@
 me)%0A    
-  
 %7D)%0A
+%0A    map.on('click', (event) =%3E %7B%0A      var lng         = event.latlng.lng%0A      var lat         = event.latlng.lat%0A%0A      dataService.url = dataService.url.replace('%7Bx%7D', lng).replace('%7By%7D', lat)
 %0A      d

Update to only add data handler once