var MapLayers = (function() {
    'use strict';
    
    // 天地图 API 密钥 （fengyarnom@gmail.com）
    // 天地图的英文就是 TIANDITU 所以这里用拼音指代
    var TIANDITU_KEY = '0c260b8a094a4e0bc507808812cefdac';
    
    function createTiandituTileLoadFunction() {
        return function(imageTile, src) {
            imageTile.getImage().src = src;
            imageTile.getImage().onerror = function() {
                // 天地图加载失败时切换到高德地图
                var mapTypeSelect = document.getElementById('mapTypeSelectNew');
                if(mapTypeSelect && mapTypeSelect.value.startsWith('tianditu_')) {
                    mapTypeSelect.value = 'amap';
                    if (window.DeviceOverview && typeof window.DeviceOverview.switchMapType === 'function') {
                        window.DeviceOverview.switchMapType('amap');
                    }
                    if (window.layer && typeof window.layer.msg === 'function') {
                        window.layer.msg('天地图加载失败,已自动切换到高德地图');
                    }
                    if (window.layui && window.layui.form) {
                        window.layui.form.render('select');
                    }
                }
            };
        };
    }
    

    var mapLayers = {
        // 高德地图
        amap: new ol.layer.Tile({
            source: new ol.source.XYZ({
                url: 'https://webrd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}'
            })
        }),
        
        // 高德卫星图
        amap_satellite: new ol.layer.Group({
            layers: [
                new ol.layer.Tile({
                    source: new ol.source.XYZ({
                        url: 'https://webst0{1-4}.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}'
                    })
                }),
                new ol.layer.Tile({
                    source: new ol.source.XYZ({
                        url: 'https://webst0{1-4}.is.autonavi.com/appmaptile?style=8&x={x}&y={y}&z={z}'
                    })
                })
            ]
        }),
        
        // 谷歌卫星图
        google_satellite: new ol.layer.Tile({
            source: new ol.source.XYZ({
                url: 'https://mt{0-3}.google.com/vt/lyrs=s&x={x}&y={y}&z={z}',
                crossOrigin: 'anonymous'
            })
        }),
        
        // 谷歌地形图
        google_terrain: new ol.layer.Tile({
            source: new ol.source.XYZ({
                url: 'https://mt{0-3}.google.com/vt/lyrs=p&x={x}&y={y}&z={z}',
                crossOrigin: 'anonymous'
            })
        }),
        
        // 谷歌道路图
        google_roadmap: new ol.layer.Tile({
            source: new ol.source.XYZ({
                url: 'https://mt{0-3}.google.com/vt/lyrs=m&x={x}&y={y}&z={z}',
                crossOrigin: 'anonymous'
            })
        }),
        
        // 谷歌混合图
        google_hybrid: new ol.layer.Tile({
            source: new ol.source.XYZ({
                url: 'https://mt{0-3}.google.com/vt/lyrs=y&x={x}&y={y}&z={z}',
                crossOrigin: 'anonymous'
            })
        }),
        
        // 天地图卫星影像
        tianditu_satellite: new ol.layer.Group({
            layers: [
                new ol.layer.Tile({
                    source: new ol.source.XYZ({
                        url: 'https://t{0-7}.tianditu.gov.cn/img_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=' + TIANDITU_KEY,
                        tileLoadFunction: createTiandituTileLoadFunction()
                    })
                }),
                new ol.layer.Tile({
                    source: new ol.source.XYZ({
                        url: 'https://t{0-7}.tianditu.gov.cn/cia_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cia&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=' + TIANDITU_KEY,
                        tileLoadFunction: createTiandituTileLoadFunction()
                    })
                })
            ]
        }),
        
        // 天地图矢量图
        tianditu_normal: new ol.layer.Group({
            layers: [
                new ol.layer.Tile({
                    source: new ol.source.XYZ({
                        url: 'https://t{0-7}.tianditu.gov.cn/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=' + TIANDITU_KEY,
                        tileLoadFunction: createTiandituTileLoadFunction()
                    })
                }),
                new ol.layer.Tile({
                    source: new ol.source.XYZ({
                        url: 'https://t{0-7}.tianditu.gov.cn/cva_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cva&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=' + TIANDITU_KEY,
                        tileLoadFunction: createTiandituTileLoadFunction()
                    })
                })
            ]
        }),
        
        // 天地图地形图
        tianditu_terrain: new ol.layer.Group({
            layers: [
                new ol.layer.Tile({
                    source: new ol.source.XYZ({
                        url: 'https://t{0-7}.tianditu.gov.cn/ter_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=ter&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=' + TIANDITU_KEY,
                        tileLoadFunction: createTiandituTileLoadFunction()
                    })
                })
            ]
        }),
        
        // 天地图地形混合图
        tianditu_terrain_hybrid: new ol.layer.Group({
            layers: [
                new ol.layer.Tile({
                    source: new ol.source.XYZ({
                        url: 'https://t{0-7}.tianditu.gov.cn/ter_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=ter&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=' + TIANDITU_KEY,
                        tileLoadFunction: createTiandituTileLoadFunction()
                    })
                }),
                new ol.layer.Tile({
                    source: new ol.source.XYZ({
                        url: 'https://t{0-7}.tianditu.gov.cn/cta_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cta&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=' + TIANDITU_KEY,
                        tileLoadFunction: createTiandituTileLoadFunction()
                    })
                })
            ]
        })
    };
    

    function getLayer(mapType) {
        return mapLayers[mapType];
    }
    


    function getAllLayers() {
        return mapLayers;
    }
    

    function hasLayer(mapType) {
        return mapLayers.hasOwnProperty(mapType);
    }
    

    return {
        getLayer: getLayer,
        getAllLayers: getAllLayers,
        hasLayer: hasLayer
    };
})(); 