import * as KMap from '@/utils/ol-map/KMap';
import config from "@/api/config.js";
import Style from 'ol/style/Style';
import Fill from 'ol/style/Fill';
import Text from 'ol/style/Text';
import Icon from 'ol/style/Icon';
import Stroke from 'ol/style/Stroke';
import Feature from 'ol/Feature';
import Point from 'ol/geom/Point';
import {getCenter} from 'ol/extent';
import Polygon from '@/utils/ol-map/Polygon.js'
import * as olProj from 'ol/proj';
import WKT from 'ol/format/WKT'
import MultiPolygon from '@/utils/ol-map/MultiPolygon.js'
import {base_img_url} from '@/api/config'
// 预警地图页面
class SampleVTLayer {
  constructor(map,callback,url){
    this.selectFeature = null;
    this.map = map.map;
    this.mapP = map;
      let vectorStyle = new KMap.VectorStyle();
      let iconStyle =vectorStyle.getPointSimpleStyle(1,"#9C9C9C","#FFFFFF",1)

    let vtlayer = new KMap.VTLayer(url,{minZoom:20,maxZoom:21,tileSize:512,styleFunVT:function(feature){
            return iconStyle;
        },
        styleFunV:function(feature){
            return iconStyle;
        },funThis:this},4,function(features){
        let features2 = [];
        features.forEach(function(feature){
          let properties = feature.getProperties();
          if(properties.status){
            let extent = feature.getGeometry().getExtent();
            let center = getCenter(extent);
            let feature2 = new Feature({
              geometry: new Point(center),
            });
            feature2.setId(properties.id);
            feature2.setProperties(properties);
            features2.push(feature2);
          }
        })
        vtlayer.vLayer.getSource().addFeatures(features2);
    });
    this.layer = vtlayer.layer;
    this.vLayer = vtlayer.vLayer;
    // this.initLandLayerEvent(vtlayer.layer,callback)
    map.addLayer(this.layer);
    // map.map.addLayer(this.vLayer);
  }
  initLandLayerEvent(vectorLayer,callback){
    const that = this;
    let map = that.map;
    map.on('click', function(e) {
        var hit = map.hasFeatureAtPixel(e.pixel, {
            layerFilter: function (layer) {
                return layer === vectorLayer;
            }
        });
        if(hit){
            map.forEachFeatureAtPixel(e.pixel, function (feature, layer){
                if(callback){
                  let properties = feature.getProperties();
                  let coors2 = [];
                  let coor = feature.getFlatCoordinates();
                  for(let i=0;i<coor.length;i+=2){
                    let lng = coor[i];
                    let lat = coor[i+1];
                    let coor2 = olProj.transform([lng,lat], 'EPSG:3857', 'EPSG:4326');
                    coors2.push(coor2);
                  }
                  let polygon = new Polygon(coors2,{strokeWidth:3});
                  if(that.selectFeature){
                    that.selectFeature.remove();
                  }
                  that.selectFeature = polygon;
                  callback(properties);
                }
            },{layerFilter: function (layer) {return layer === vectorLayer}});
        }
    });
  }
  //矢量切片样式函数
  styleFunVT(evt){
    const that = this;
    let feature = evt;
    let info = feature.getProperties();
    let color = '#9C9C9C'
    let style = new Style({
        stroke: new Stroke({
            color: '#9C9C9C',
            width: 2
        }),
        fill: new Fill({
            color: color
        })
    });
    return style;
  }

  getExtent(geomArray){
      let minx = geomArray[0].getExtent()[0];
      let miny = geomArray[0].getExtent()[1];
      let maxx = geomArray[0].getExtent()[2];
      let maxy = geomArray[0].getExtent()[3];
      for(let i = 1;i<geomArray.length;i++){
          minx = geomArray[i].getExtent()[0] < minx ? geomArray[i].getExtent()[0] : minx;
          miny = geomArray[i].getExtent()[1] < minx ? geomArray[i].getExtent()[1] : miny;
          maxx = geomArray[i].getExtent()[2] > maxx ? geomArray[i].getExtent()[2] : maxx;
          maxy = geomArray[i].getExtent()[3] > maxy ? geomArray[i].getExtent()[3] : maxy;
      }
      let padding = 120;
      return [minx - padding,miny - padding * 2,maxx + padding,maxy + padding]
  }

}
export default SampleVTLayer;
