import sourceWMTS from 'ol/source/WMTS';
import WMTSTileGrid from 'ol/tilegrid/WMTS';
import * as olExtent from 'ol/extent';
import TileLayer from 'ol/layer/Tile';
import XYZ from 'ol/source/XYZ.js';
import {WKT} from "ol/format";
import BufferOp from 'jsts/org/locationtech/jts/operation/buffer/BufferOp.js'
import {OL3Parser} from "jsts/org/locationtech/jts/io"
import * as geom from "ol/geom";
let jstsParser = new OL3Parser();
jstsParser.inject(geom.Point, geom.LineString, geom.LinearRing, geom.Polygon, geom.MultiPoint, geom.MultiLineString, geom.MultiPolygon);



//Code To RGB
function CodeToRGB(code){
    let result = [];
    result.push(parseInt(code.substring(1, 3), 16));
    result.push(parseInt(code.substring(3, 5), 16));
    result.push(parseInt(code.substring(5) , 16));
    return result;
}



//创建图层(WMTS方式)
function createGDWmts(){
    let AMapLayer = new TileLayer({
        source: new XYZ({
            url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}'
        }),
        visible: false,
    });
    return AMapLayer;
}


function crtLayerWMTS(wmtsData, opacity, projection){
    let projectionExtent = projection.getExtent();
    let size = olExtent.getWidth(projectionExtent) / 256;
    let resolutions = new Array(19);
    let matrixIds = new Array(19);
    for (var z = 1; z < 19; ++z) {
        // generate resolutions and matrixIds arrays for this WMTS
        resolutions[z] = size / Math.pow(2, z);
        matrixIds[z] = z;
    }

    let source = new sourceWMTS({
        url: wmtsData.url,
        layer: wmtsData.layer,
        matrixSet: wmtsData.matrixSet,
        format: 'tiles',
        projection: projection,
        tileGrid: new WMTSTileGrid({
            origin: olExtent.getTopLeft(projectionExtent),
            resolutions: resolutions,
            matrixIds: matrixIds
        }),
        style: 'default',
        wrapX: true
    })

    let layer = new TileLayer({
        zIndex:wmtsData.zIndex,
        source: source,
        visible: false,
    });
    layer.id = wmtsData.layer+"_"+wmtsData.matrixSet;
    return layer;
}

function wktCastGeom(wkt){
    return new WKT().readGeometry(wkt);
}

function fit(wkt,view,zoom){
    let extent = wktCastGeom(wkt).getExtent()
    extent[0] = extent[0] / zoom;
    extent[1] = extent[1] / zoom;
    extent[2] = extent[2] * zoom;
    extent[3] = extent[3] * zoom;
    view.fit(extent,{duration:700})
}

function fitExtent(extent, view, zoom){
    let extent2 = [];
    extent2[0] = extent[0] / zoom;
    extent2[1] = extent[1] / zoom;
    extent2[2] = extent[2] * zoom;
    extent2[3] = extent[3] * zoom;
    view.fit(extent2,{duration:700})
}

const fitBuffer = (pointWkt, map, meter)=>{
    let geom = new WKT().readGeometry(pointWkt);
    let degree = meter / (2 * Math.PI * 6371004) * 360;
    geom = jstsParser.write(BufferOp.bufferOp(jstsParser.read(geom), degree, 8))
    map.getView().fit(geom,{duration: 1000})
}


export{
    crtLayerWMTS,CodeToRGB,createGDWmts,wktCastGeom,fit,fitBuffer,fitExtent
};


