
const Cesium = window.Cesium;
const turf = window.turf;
export const addPolyline = ({ features, viewer }) => {
    if (features.length === 0) {
        return;
    }
    const instances = features.forEach(f => {
        var coordinates = f.geometry.coordinates;

        var polygon = window.turf.polygon(coordinates);
        var center = window.turf.centerOfMass(polygon).geometry.coordinates;
        console.log(center)
        var position = window.Cesium.Cartesian3.fromDegrees(center[0], center[1])
        viewer.entities.add({
            position: position,
            heightReference: window.Cesium.HeightReference.CLAMP_TO_GROUND,
            label: {
                font: "normal 18px MicroSoft YaHei",
                clampToGround: true,
                text: f.properties.name,
                fillColor: window.Cesium.Color.WHITE,
                outlineColor: window.Cesium.Color.BLACK,
                outlineWidth: 4,
                heightReference: window.Cesium.HeightReference.CLAMP_TO_GROUND
            },
        });
        // const polygonArr = f.geometry.coordinates.toString().split(',').filter(v => {
        //     return v !== '0'
        // });
        // const id = 'zone-' + Math.random().toString().substring(3)
        // viewer.entities.add({
        //     id: id,
        //     name: '',
        //     polyline: {
        //         positions: Cesium.Cartesian3.fromDegreesArray(polygonArr),
        //         width: 5,
        //         clampToGround: true,
        //         // heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
        //         material: Cesium.Color.BLUE
        //     }
        // })
        // this.createGroundPolylineGeometryInstance(f);
    })
}

export const addGeoserverLayer = ({ layerName, extent, isWms, viewer, geoserverRoot = 'geoserver', cql_filter = '1=1' }) => {
    let layer;
    if (isWms) {
        let imageryProvider = window.testWms = new Cesium.WebMapServiceImageryProvider({
            url: `/${geoserverRoot}/yzt/wms`,
            layers: layerName,
            tileWidth: 256,
            tileHeight: 256,
            enablePickFeatures: false,
            parameters: { transparent: true, format: 'image/png', cql_filter },
            version: '1.1.0'
        });

        layer = viewer.imageryLayers.addImageryProvider(imageryProvider);
    } else {
        // const geoserverOptions = geoserverOptions;
        let matrixIds = ['EPSG:4326:0', 'EPSG:4326:1', 'EPSG:4326:2', 'EPSG:4326:3', 'EPSG:4326:4', 'EPSG:4326:5', 'EPSG:4326:6', 'EPSG:4326:7', 'EPSG:4326:8', 'EPSG:4326:9', 'EPSG:4326:10',
            'EPSG:4326:11', 'EPSG:4326:12', 'EPSG:4326:13', 'EPSG:4326:14', 'EPSG:4326:15', 'EPSG:4326:16', 'EPSG:4326:17', 'EPSG:4326:18', 'EPSG:4326:19', 'EPSG:4326:20', 'EPSG:4326:21'];
        var wmtsImageryProvider = new Cesium.WebMapTileServiceImageryProvider({
            url: `/${geoserverRoot}/gwc/service/wmts`,
            layer: layerName,
            style: '',
            format: 'image/png',
            tileMatrixSetID: 'EPSG:4326',
            rectangle: Cesium.Rectangle.fromDegrees(...extent),
            tileMatrixLabels: matrixIds,
            tilingScheme: new Cesium.GeographicTilingScheme({
                numberOfLevelZeroTilesX: 2,
                numberOfLevelZeroTilesY: 1
            }),
            // subdomains: geoserverOptions?.subdomains || []
        });
        layer = viewer.imageryLayers.addImageryProvider(wmtsImageryProvider);
    }
    return layer;
}