var insertCss = require('insert-css')
var ipc = require('electron').ipcRenderer
var remote = require('electron').remote
var merge = require('lodash/merge')

var defaultPresets = require('../vendor/iD/presets.json')
var defaultImagery = require('../vendor/iD/imagery.json')

var log = require('../lib/log').Browser()

var prevhash = localStorage.getItem('location')
if (location.hash) localStorage.setItem('location', location.hash)
else if (prevhash) location.hash = prevhash

window.addEventListener('hashchange', function (ev) {
  localStorage.setItem('location', location.hash)
})

var serverUrl = 'http://' + remote.getGlobal('osmServerHost')

iD.oneWayTags.waterway.spring = true;

var id = iD()
  .imagery(defaultImagery)
  .taginfo(iD.services.taginfo())
  .assetPath('vendor/iD/')
  .preauth({url: serverUrl})
  .minEditableZoom(14)

window.locale.en.inspector.view_on_osm = 'View element source XML'

id.loadLocale = function(cb) {
  var locale = iD.detect().locale
  if (locale && iD.data.locales.indexOf(locale) === -1) {
    locale = locale.split('-')[0]
  }
  if (locale && locale !== 'en' && iD.data.locales.indexOf(locale) !== -1) {
    var localePath = id.asset('locales/' + locale + '.json')
    d3.json(localePath, function (err, result) {
      window.locale[locale] = result
      window.locale.current(locale)
      var translations = ipc.sendSync('get-user-data', 'translations')
      merge(window.locale, translations)
      cb()
      window.onbeforeunload = myOnBeforeLoad
    })
  } else {
    cb()
    window.onbeforeunload = myOnBeforeLoad
  }
}

d3.select('#container')
  .call(id.ui())

function myOnBeforeLoad () {
  context.save()
}

var parser = new DOMParser()
var customDefs = id.container()
  .append('svg')
  .style('position', 'absolute')
  .style('width', '0px')
  .style('height', '0px')
  .attr('id', 'custom-defs')
  .append('defs')

customDefs.append('svg')

updateSettings()
ipc.on('updated-settings', function () {
  updateSettings()
  ipc.send('refresh-window')
})
ipc.on('zoom-to-data-request', zoomToDataRequest)
ipc.on('zoom-to-data-response', zoomToDataResponse)

function updateSettings () {
  var presets = ipc.sendSync('get-user-data', 'presets')
  var customCss = ipc.sendSync('get-user-data', 'css')
  var imagery = ipc.sendSync('get-user-data', 'imagery')
  var translations = ipc.sendSync('get-user-data', 'translations')
  var icons = ipc.sendSync('get-user-data', 'icons')
  if (icons) {
    var iconsSvg = parser.parseFromString(icons, 'image/svg+xml').documentElement
    customDefs.node().replaceChild(iconsSvg, customDefs.node().firstChild)
  }
  if (customCss) insertCss(customCss)
  if (translations) merge(window.locale, translations)
  if (imagery) id.imagery(imagery)
  log('updating settings')
  id.presets(presets || defaultPresets)
}

function zoomToDataRequest () {
  ipc.send('zoom-to-data-get-centroid')
}

function zoomToDataResponse (_, loc) {
  translateAndZoomToLocation(loc, 14)
}

function translateAndZoomToLocation (loc, zoom) {
  id.map().centerEase(loc, 1000)
  setTimeout(function () {
    id.map().zoom(zoom)
  }, 1000)
}


@@ -641,16 +641,19 @@
 Host')%0A%0A
+// 
 iD.oneWa
@@ -698,70 +698,17 @@
 = iD
-()%0A  .imagery(defaultImagery)%0A  .taginfo(iD.services.taginfo()
+.Context(
 )%0A  
@@ -747,16 +747,20 @@
 uth(%7Burl
+root
 : server
@@ -789,16 +789,19 @@
 om(14)%0A%0A
+// 
 window.l
@@ -860,19 +860,25 @@
 e XML'%0A%0A
-id.
+function 
 loadLoca
@@ -884,21 +884,9 @@
 ale 
-= function(cb
+(
 ) %7B%0A
@@ -903,17 +903,17 @@
 le = iD.
-d
+D
 etect().
@@ -939,46 +939,31 @@
  && 
+!
 iD.data
-.l
+L
 ocales
-.indexOf(locale) === -1
+%5Blocale%5D
 ) %7B%0A
@@ -1046,39 +1046,23 @@
 data
-.l
+L
 ocales
-.indexOf(locale) !== -1
+%5Blocale%5D
 ) %7B%0A
@@ -1430,25 +1430,16 @@
  else %7B%0A
-    cb()%0A
     wind
@@ -1483,21 +1483,41 @@
 %7D%0A%7D%0A
-%0Ad3.select
+id.ui()(document.getElementById
 ('
-#
 cont
@@ -1527,24 +1527,20 @@
 er')
-%0A  .call(id.ui()
+, loadLocale
 )%0A%0Af
@@ -1573,15 +1573,10 @@
 %7B%0A  
-context
+id
 .sav
@@ -1611,16 +1611,19 @@
 arser()%0A
+// 
 var cust
@@ -1646,16 +1646,19 @@
 ainer()%0A
+// 
   .appen
@@ -1666,16 +1666,19 @@
 ('svg')%0A
+// 
   .style
@@ -1702,16 +1702,19 @@
 olute')%0A
+// 
   .style
@@ -1730,16 +1730,19 @@
  '0px')%0A
+// 
   .style
@@ -1759,18 +1759,21 @@
  '0px')%0A
+//
   
+ 
 .attr('i
@@ -1791,18 +1791,21 @@
 -defs')%0A
+//
   
+ 
 .append(
@@ -1812,17 +1812,22 @@
 'defs')%0A
-%0A
+//%0A// 
 customDe
@@ -3046,13 +3046,12 @@
  %7D, 1000)%0A%7D%0A
-%0A

update browser/main.js for new v2 api