import * as maptalks from 'maptalks'
import { ThreeLayer } from 'maptalks.three'
import { unMeshAll, meshStatus } from './meshStaus'
import initAmbient from './initAmbient'
import { addGeo } from '../core/dyWoker'
import { referText } from './addText'
import { singleDelay } from '../core/util'
import config from './config'


// 高层数据获取
const canvasTile = new maptalks.CanvasTileLayer('tile', {
  urlTemplate: 'https://{s}.data.osmbuildings.org/0.2/anonymous/tile/{z}/{x}/{y}.json',
  subdomains: ['a', 'b', 'c', 'd'],
  forceRenderOnMoving: true,
  forceRenderOnZooming: true,
  forceRenderOnRotating: true,
  repeatWorld: false,
  maxAvailableZoom: 15
})

// 3DThree与map结合
const threeLayer = new ThreeLayer('t', {
  forceRenderOnMoving: true,
  forceRenderOnZooming: true,
  forceRenderOnRotating: true
})
threeLayer.prepareToDraw = initAmbient

global.threeLayer = threeLayer

// Maker承载容器
let vector = new maptalks.VectorLayer('vector', {
  enableAltitude: true,        // enable altitude
  altitudeProperty: 'altitude', // altitude property in properties, default by 'altitude',
  forceRenderOnMoving: true,
  forceRenderOnZooming: true,
  forceRenderOnRotating: true
})


// 获取高层数据
function getCurrTiles() {
  let tileGrids = canvasTile.getTiles().tileGrids

  for (var i = 0; i < tileGrids.length; i++) {
    if (tileGrids[i].zoom >= 15) break
  }
  let zoom = tileGrids[i].zoom
  let tiles = tileGrids[i].tiles
  let maxX = tiles[0].x
  let minX = tiles[0].x
  let maxY = tiles[0].y
  let minY = tiles[0].y

  tiles.forEach(tile => {
    tile.x > maxX && (maxX = tile.x)
    tile.y > maxY && (maxY = tile.y)
    tile.x < minX && (minX = tile.x)
    tile.y < minY && (minY = tile.y)
  })

  var test = []

  for (let x = minX - config.manyOffset; x <= maxX + config.manyOffset; x++ ) {
    for (let y = minY - config.manyOffset; y <= maxY + config.manyOffset; y++) {
      test.push({x, y, z: zoom});
    }
  }


  for (let i = 1; i <= config.manyOffset; i++) {
    let largeX = maxX + i
    let smallX = minX - i
    for (let i = 1; i <= config.manyOffset; i++) {
      let largeY = maxY + i
      let smallY = minY - i

      tiles.push({ x: largeX, y: largeY, z: zoom })
      tiles.push({ x: largeX, y: smallY, z: zoom })
      tiles.push({ x: smallX, y: smallY, z: zoom })
      tiles.push({ x: smallX, y: largeY, z: zoom })
    }
  }

  return test
}

let gAddGeo = singleDelay(() => addGeo(getCurrTiles()), 100)
let isUninstall = false
// 动态添加删除建筑
let dyAddGeo = singleDelay(() => {
  if (!isUninstall) {
    meshStatus()
    addGeo(getCurrTiles())
  }
}, 500);


map.addLayer([canvasTile, threeLayer, vector])

bus.on('install3D', () => {
  isUninstall = false
  gAddGeo()
  
  map.on('moveend zoomend dragrotateend', referText)
  map.on('moving zoomend dragrotating', dyAddGeo)
})

bus.on('uninstall3D', () => {
  isUninstall = true
  map.off('moveend zoomend dragrotateend', referText)
  map.off('moving zoomend dragrotating', dyAddGeo)
  
  unMeshAll()
})

export {
  vector,
  threeLayer
}