import {Vector as VectorLayer} from 'ol/layer.js'
import VectorSource from 'ol/source/Vector'
import  Feature  from 'ol/Feature'
import {Fill, Stroke, Style, Text, Icon} from 'ol/style'
import {fromCircle as fromCirclePolygon} from 'ol/geom/Polygon.js'
import Circle from 'ol/geom/Circle'
import WKT from 'ol/format/WKT'
import qs from 'qs'
import axios from 'axios'
import {gcj_encrypt} from '../base/poiQuery.js'
import turf_circle from '@turf/circle'
import GeoJSON from 'ol/format/GeoJSON'
class mapSearch {
    constructor(map){
        this.map = map;//初始化赋值
    }
    queryByGeometry(posArr,map,config){
        return new Promise((resolve)=>{
            if(config==undefined) return;
            let posArrStr = '';
            for(var i = 0;i<posArr.length;i++){
                let pos=[];
            if(i == posArr.length-1){
                //  pos  = gcj_encrypt(posArr[i][0],posArr[i][1])
                posArrStr += "["+[posArr[i][0],posArr[i][1]]+"]";
            }else{
                // pos  = gcj_encrypt(posArr[i][0],posArr[i][1])
                posArrStr += "["+[posArr[i][0],posArr[i][1]]+"],";
            }
            }
            var initData = {
                "posArray": [{
                    "posArr": posArrStr,
                    'pageSize':config.pageSize,
                    'currentPage':config.currentPage,
                    "tableName": config.tableName
                }]
            }
            axios.post(config.serurl, qs.stringify(initData), {
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
                }
            }).then(res => {
                    let data = res.data
                    resolve(data)
            })
        })
    }
    nearByQuery(id,center,radius){
        return new Promise((resolve)=>{
            if(radius!=undefined && radius!=''){
                // var center = center;
                // var radius = radius/1000;
                // var options = {steps: 128, units: 'kilometers'};
                // var circle = turf_circle(center, radius/1000, options);//为feature
                // let geojsonFormat = new GeoJSON({ defaultDataProjection: "EPSG:4326" })
                // let features = geojsonFormat.readFeatures(circle)
                // let circleFeature = features[0]
                // let vectorSource = new VectorSource({ 
                //     features:[circleFeature],
                //     // projection:'EPSG:4326'//WGS84 — SRID 4326，也表示为EPSG 4326
                // })
                // let vectorLayer = new VectorLayer({
                //     id:id,
                //     source: vectorSource,
                //     style: new Style({
                //         stroke: new Stroke({
                //             color: '#ff3100',
                //             width: 1
                //         }),
                //         fill: new Fill({
                //             color: 'rgba(0, 0, 255, 0.1)'
                //         })
                //     })
                // })
                // this.map.addLayer(vectorLayer)
                // resolve(circle.geometry.coordinates[0])
                let circle = new Circle(center,radius/100000)
                let feature = new Feature({
                        position:center,
                        radius:radius/100000,
                        type: 'circle',
                        id: 'N',
                        geometry:fromCirclePolygon(circle)
                })
                let vectorSource = new VectorSource({ 
                    features:[feature],
                    projection:'EPSG:4326'//WGS84 — SRID 4326，也表示为EPSG 4326
                })
                let vectorLayer = new VectorLayer({
                    id:id,
                    source: vectorSource,
                    style: new Style({
                        stroke: new Stroke({
                            color: '#ff3100',
                            width: 1
                        }),
                        fill: new Fill({
                            color: 'rgba(0, 0, 255, 0.1)'
                        })
                    })
                })
                this.map.addLayer(vectorLayer)
                let geometry = feature.getGeometry()//获取圆转为多边形后的范围
                let wkt = new WKT()
                // let wktgeometry = wkt.writeGeometry(geometry)
                resolve(geometry.flatCoordinates)
            }
        })
    }
  }
  export default mapSearch;