import { Map } from 'maptalks'
import 'babel-core/register'
import 'babel-polyfill'
import './style/public.css'
import Event from './core/Event'
import { listStatus, analysis, mapGoto } from './business/statusManagement'
import { singleDelay } from './core/util'
import config from './business/config'
import getTile from './business/selectTile'
import './business/test'

global.tile = getTile()
global.bus = new Event()

let args = analysis()
global.map = new Map('map', {
  center: [args.lon, args.lat],
  zoom: args.zoom,
  bearing: args.bearing,
  maxZoom: 19,
  minZoom: 1,
  maxPitch: 70,
  maxVisualPitch: 100,
  doubleClickZoom: false,
  forceRenderOnMoving: true,
  forceRenderOnZooming: true,
  forceRenderOnRotating: true,
  baseLayer: tile,
})


require('./business/initScene3D.js')
// require('./business/addAC.js')

// url状态
let status = singleDelay(listStatus, 500)
map.on('moving zoomend dragrotating', status);
map.on('zoomend', args => {
  if (args.to <= config.criticalZoom && args.from > config.criticalZoom) {
    mapGoto({ pitch: 1, bearing: 7.5 }, 300)
    bus.emit('uninstall3D')
    bus.emit('install2D')
  } else if (args.to > config.criticalZoom && args.from <= config.criticalZoom) {
    bus.emit('uninstall2D')
    bus.emit('install3D')
    mapGoto({ pitch: 62, bearing: 90 }, 300)
  }
})

if (args.zoom > config.criticalZoom) {
  bus.emit('install3D')
} else {
  bus.emit('install2D')
}