
import LTBaseObject from './KBaseObject'
import XYZ from 'ol/source/XYZ'
import Tile from 'ol/layer/Tile'
import Common from './Common'
/**
 * @description LTMap.XYZLayer XYZ图层类
*/
class XYZLayer extends LTBaseObject{
	/**
	 * @description 构造函数
	 * @param {String} url XYZ图层服务地址
	 * @param {LTMap.Map} [mapInstance=null] map对象，单地图的时候可不传，多地图时候需要传
	 * @memberof XYZLayer
	 */
	constructor(url,options,zIndex,mapInstance = null){
    super(mapInstance)
		const vm = this
		vm.initXYZLayer(url,options,zIndex)
  }
	/**
	 * @description 切片加载XYZ图层
	 * @param {*} url
	 * @param {*} layerName
	 * @memberof XYZLayer
	 */
	 initXYZLayer(url,options,zIndex){
		var minZoom = Common.BaseLayerZoom[0];
		var maxZoom = Common.BaseLayerZoom[1];
		if(options && options.minZoom != undefined){
			minZoom = options.minZoom
		}
		if(options && options.maxZoom != undefined){
			maxZoom = options.maxZoom
		}
		if(options && options.zIndex != undefined){
			zIndex = options.zIndex
		}
		const vm = this
		vm.source = new XYZ({
			crossOrigin:'anonymous',
      		url : url
    	})

		vm.layer = new Tile({
			source:vm.source,
			maxZoom:maxZoom,
			minZoom:minZoom,
			zIndex:zIndex,
			extent:options.extent || null
		})
		if(options && options.padding != undefined){
			vm.layer.set('padding',options.padding)
		}

		vm.map.addLayer(vm.layer)
	}
	setProperty(properties){
		const vm = this;
		vm.properties = properties;
	}
	getProperty(){
		const vm = this;
		return vm.properties?vm.properties:null;
	}
  /**
	* @description 添加XYZ图层到地图
	* @memberof XYZLayer
	*/
  add(){
		const vm = this
		if(!vm.state) {
			vm.map.addLayer(vm.layer)
			vm.state = true
		}
	}

	/**
	 * @description 从当前地图中移除XYZ图层
	 * @memberof XYZLayer
	 */
	remove() {
		const vm = this
		if(vm.state) {
			vm.map.removeLayer(vm.layer)
			vm.state = false
		}
	}

	/**
	 * @description 显示XYZ图层数据
	 * @memberof XYZLayer
	 */
	show(){
		const vm = this
		vm.layer.setVisible(true)
	}

	/**
	 * @description 隐藏XYZ图层数据
	 * @memberof XYZLayer
	 */
	hide(){
		const vm = this
		vm.layer.setVisible(false)
	}

	/**
	 * @description 缩放到过滤后的地图要素对应范围
	 * @memberof XYZLayer
	 */
	zoomTo() {
    const vm = this
	}
}
export default XYZLayer
