import * as Cesium from "cesium";

export function modifyCesiumMouseWheel(viewer) {
    // 地图缩放改为按下 Ctrl + 鼠标滚轮
    viewer.scene.screenSpaceCameraController.zoomEventTypes = [{ eventType: Cesium.CameraEventType.WHEEL, modifier: Cesium.KeyboardEventModifier.CTRL }];

    // 改写 Cesium 原生鼠标滚轮事件
    const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    handler.setInputAction((event) => {
        if (event < 0) {
            window.scrollBy({ left: 0, top: 15, behavior: 'auto' });
        } else {
            window.scrollBy({ left: 0, top: -15, behavior: 'auto' });
        }
    }, Cesium.ScreenSpaceEventType.WHEEL);

    // 鼠标移动时显示提示信息
    handler.setInputAction((event) => {
        document.getElementById('mouseWheelTip').style.display = '';
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
}

let imageryViewModels = [];
export function setImageryViewModels(cesiumHostAddr) {
    if (imageryViewModels.length == 0) {
        // 世界影像地图
        const world_imagery = new Cesium.WebMapTileServiceImageryProvider(
            {
                url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer'
                // url: 'https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer'
                // url: 'https://services.arcgisonline.com/ArcGIS/rest/services/Ocean/World_Ocean_Base/MapServer'
            }
        );
        // 彩色地形底图
        const colorMap = new Cesium.WebMapTileServiceImageryProvider(
            {
                url: cesiumHostAddr + 'geoserver/gwc/service/wmts/rest/ougp:world/{TileMatrixSet}/{TileMatrixSet}:{TileMatrix}/{TileRow}/{TileCol}?format=image/jpeg',
                layer: 'ougp:world',
                style: 'default',
                format: 'image/jpeg',
                tileMatrixSetID: 'EPSG:4326',
                tilingScheme: new Cesium.GeographicTilingScheme()
            }
        );
        // 灰度地形底图
        const grayMap = new Cesium.WebMapTileServiceImageryProvider(
            {
                url: cesiumHostAddr + 'geoserver/gwc/service/wmts/rest/ougp:world_gray/{TileMatrixSet}/{TileMatrixSet}:{TileMatrix}/{TileRow}/{TileCol}?format=image/jpeg',
                layer: 'ougp:world_gray',
                style: 'default',
                format: 'image/jpeg',
                tileMatrixSetID: 'EPSG:4326',
                tilingScheme: new Cesium.GeographicTilingScheme()
            }
        );
        // 自定义底图
        imageryViewModels.push(new Cesium.ProviderViewModel({
            name: "世界影像地图",
            iconUrl: "static/images/world_imagery.png",
            tooltip: "世界影像地图",
            creationFunction: function () {
                return world_imagery;
            }
        }));
        imageryViewModels.push(new Cesium.ProviderViewModel({
            name: "地形地图",
            iconUrl: "static/images/world_color.png",
            tooltip: "彩色地形底图",
            creationFunction: function () {
                return colorMap;
            }
        }));
        imageryViewModels.push(new Cesium.ProviderViewModel({
            name: "灰度地图",
            iconUrl: "static/images/world_gray.png",
            tooltip: "灰度地形底图",
            creationFunction: function () {
                return grayMap;
            }
        }));
    }
    return imageryViewModels;
}
