import axios from "axios"

import service from "@/config/service"
import {
    OSM,
    ImageArcGISRest,
    XYZ,
    TileArcGISRest,
    Vector as VectorSource,
    ImageWMS,
    TileWMS,
} from "ol/source";
import {
    Tile as TileLayer,
    Image as ImageLayer,
    Vector as VectorLayer,
} from "ol/layer";
import TileGrid from "ol/tilegrid/TileGrid";
import WMSGetFeatureInfo from "ol/format/WMSGetFeatureInfo";
import GeoJSON from "ol/format/GeoJSON";
import { WFS } from "ol/format"
import { Fill, Stroke, Style, Circle, Icon } from "ol/style";
export function getService(type, params) {
    let url = service[type];
    if (type == 'tms') {
        return axios.get(url + "?f=pjson").then(res => {
            let data = res.data
            console.log(data)
            let extent = [data.fullExtent.xmin, data.fullExtent.ymin, data.fullExtent.xmax, data.fullExtent.ymax]
            let origin = [data.tileInfo.origin.x, data.tileInfo.origin.y];
            let resolutions = [];
            data.tileInfo.lods.forEach(item => {
                resolutions.push(item.resolution)
            })
            let tileGrid = new TileGrid({
                tileSize: data.tileInfo.rows,
                resolutions,
                origin,
                extent
            })
            let source = new XYZ({ // 切片服务
                crossOrigin: "Anonymous",
                tileUrlFunction: function (coordinate) {
                    return (
                        url + '/tile' +
                        coordinate[0] +
                        "/" +
                        coordinate[2] +
                        "/" +
                        coordinate[1]
                    );
                },
                tileGrid: tileGrid,
            });
            let layer = new TileLayer({
                maxZoom: 13,
                source,
            })
            return { layer }
        })
    } else if (type == 'wfs') { // 矢量图层
        let style = params.style || new Style({
            fill: new Fill({
                color: "rgba(1 ,145, 255,0.8)"
            }),
            circle: new Circle({
                fill: new Fill({
                    color: 'red',
                }),
                radius: 10000,
                stroke: new Stroke({
                    color: "blue"
                })
            }),
            stroke: new Stroke({
                color: "red"
            }),
        })

        let source = new VectorSource();
        let layer = new VectorLayer({
            source,
            style,
            opacity: params.opacity || 1
        });
        var featureRequest = new WFS().writeGetFeature({
            srsName: "EPSG:3857", // 坐标系
            featureNS: "http://192.168.1.82:8888//demo", // 命名空间
            featurePrefix: "demo", // 工作区
            featureTypes: params.layers, // 图层
            outputFormat: "application/json", // 输出格式
            filter: params.filter || null // 过滤条件
        })
        return fetch(url, {
            body: new XMLSerializer().serializeToString(featureRequest),
            method: "POST"
        }).then(res => {
            return res.json()
        }).then(res => {
            let features = new GeoJSON().readFeatures(res)
            source.addFeatures(features);
            return { features, layer }
        })
    } else if (type == 'ImageArcGIS') { // 反演图层
        return new Promise((resolve, reject) => {
            let source = new ImageArcGISRest({
                ratio: 1,
                url,
                // params: { // 设置显示的图层
                //     layers: "show:" + params.layers
                // }
            })
            let layer = new ImageLayer({
                opacity: 0.8,
                source: source,
                visible: true,
            });
            resolve({ layer })
        })
    }

}