import { getBaseMap, getBusinessMap, getHighAltitudeRoadMap } from '@/views/cockpit/api/commonHttp.js'
import * as Cesium from 'cesium'


// 当前页面的最基础地图服务
// 主要是加载地图底图
export const useMapBase = (mapStore) => {

    let hightAltitudeRoadMapId

    // 加载当前业务的底图, 类似于天地图，但是没有使用天地图作为底图，有大的地块的地形纹理，但是缩小范围很小，属于比较粗的图
    const loadBaseMap = async () => {
        const layerService = mapStore.services().layer
        const res = await getBaseMap()
        const data = [...res.data]
        mapStore.baseMapGroups = data
        for (const item of data) {
            const layers = mapStore.getBaseMapLayersForGroup(item.Attribute?.rid || item.Rid)
            for (const layerConfig of layers) {
                const layer = {
                    id: layerConfig.id,
                    type: layerConfig.type,
                    url: layerConfig.url,
                    meta: layerConfig.meta,
                    options: {
                        // 瓦片方案，必传，由于cesium版本较老，必传
                        tilingScheme: new Cesium.WebMercatorTilingScheme(),
                        maximumLevel: 18, // 限制最大级别以匹配 GWC 缓存尺度，避免分辨率不匹配错误
                        extraParameters: {
                            srs: 'EPSG:3857',
                        }
                    }

                }
                await layerService.addLayer(layer)
            }
        }
    }

    // 处理启动加载的图层
    const collectBootLoadLayers = (nodes, layers = [], parent = null) => {
        nodes.forEach(node => {
            if (node.Attribute?.servicePath && node.Attribute.bootLoad === 1) {
                // 确保 bootLoad 图层包含正确的 parentSortIndex
                const layerWithParentSort = {
                    ...node,
                    parentSortIndex: parent?.Attribute?.sortValue
                };
                layers.push(layerWithParentSort);
            }
            if (node.Children) {
                collectBootLoadLayers(node.Children, layers, node);
            }
        });
        return layers;
    };

    // 加载业务地图，业务地图主要是高亮当前业务下的地区的区县与管线图，边界都会有高亮线条
    const loadBusinessMap = async () => {
        const layerService = mapStore.services().layer
        const res = await getBusinessMap()
        const resData = res.data
        const data = collectBootLoadLayers(resData)
        resData[0].Children = data
        mapStore.baseMapGroups = resData
        for (const item of resData) {
            const layers = mapStore.getBaseMapLayersForGroup(item.Attribute?.rid || item.Rid)
            for (const layerConfig of layers) {
                const layer = {
                    id: layerConfig.id,
                    type: layerConfig.type,
                    url: layerConfig.url,
                    meta: layerConfig.meta,
                }
                await layerService.addLayer(layer)
            }
        }
    }

    // 加载高海拔底图
    const toggleHighAltitudeRoadMap = async (flag) => {
        const layerService = mapStore.services().layer

        if(!flag && hightAltitudeRoadMapId) {
            layerService.removeLayer(hightAltitudeRoadMapId)
            return
        }


        const res = await getHighAltitudeRoadMap()
        const resData = res.data
        resData[0].Children = resData
        mapStore.baseMapGroups = resData
        for (const item of resData) {
            const layers = mapStore.getBaseMapLayersForGroup(item.Attribute?.rid || item.Rid)
            for (const layerConfig of layers) {
                const layer = {
                    id: layerConfig.id,
                    type: layerConfig.type,
                    url: layerConfig.url,
                    meta: layerConfig.meta,
                }
                hightAltitudeRoadMapId = await layerService.addLayer(layer)
            }
        }
    }

    const loadBaseData = () => {
        setTimeout(() => {
            loadBaseMap()
            loadBusinessMap()
        }, 0)
    }

    return {
        loadBaseData,
        toggleHighAltitudeRoadMap
    }
}