/*
* 加载自定义地图主函数，参数说明：
* url：WMS网址
* mapDiv: 实例化的OL对象
* map: 由mapDiv返回的地图对象
* view: 由mapDiv返回的视图对象
*/
import {OLMap} from "@/assets/map/OLMap.js";

export function load_wxmap(mapDiv, isVisible) {
  // 加载自定义的卫星地图，并根据isVisible参数决定是否显示
  // mapDiv: 地图的容器，负责承载地图和地图操作
  // isVisible: 是否显示微信地图

  // 将卫星地图添加到地图容器中
  mapDiv.addMapWX(isVisible);

  // 将卫星地图的标记添加到地图容器中
  mapDiv.addMapWXTag(isVisible);
}

export function load_basemap(url, mapDiv, map, view) {
  // 加载基础地图，并根据缩放级别调整地图层的可见性
  // url: WMS服务的基础URL
  // mapDiv: 地图的容器
  // map: 地图对象，用于控制地图的显示和交互
  // view: 视图对象，用于控制地图的视角（缩放级别、中心点等）

  // 如果未传入URL 则使用默认的本地服务
  // url = url != '' ? url : 'http://localhost:8080';   /***url = if url ≠ 空 返回url ；否则 返回 http://localhost:8080*/
  if(url == " "){
    url = "http://localhost:8080";
  }
  //拼接得到完整的WMS服务URL，用于加载基础地图
  var wmsUrl = url + '/geoserver/basemap/wms';

  //缩放级别和地理边界
  var ze = 8;
  var _bbox = [73, 3, 135, 60];
  //获取当前地图的缩放级别
  var z = view.getZoom();
  //定义各地图层的出事可见性（即在当前缩放级别下收否显示）
  var v_boundary = true;
  var v_nanhai   = false;
  var v_nanhaidaoyu = false;
  var v_capital    = false;
  var v_province = true;
  var v_provinceName = false;
  var v_city = false;
  var v_cityName = false;
  var v_county = false;
  var v_countyName = false;
  var v_town = false;
  var v_tielu = false;
  var v_gaosu = false;
  var v_guodao = false;
  var v_shengdao = false;
  // 动态设置地图层的可见性，基于当前的缩放级别z
  if (z <= 5.7) {
       v_province = true;
  }
  if (z >= 4.3 && z <= 4.6) {
       v_provinceName = true;
  }
  if (z >= 5.6 && z <= 7.7) {
       v_city = true;
  }
  if (z >= 5.6 && z <= 7.7) {
       v_cityName = true;
  }
  if (z >= 7.6 && z <= 10.7) {
       v_county = true;
  }
  if (z >= 7.6) {
       v_countyName = true;
  }
  if (z >= 10.6) {
       v_town = true;
  }
  if (z >= 5.6) {
       v_gaosu = true;
  }
  if (z >= 6.6) {
       v_tielu = true;
  }
  if (z >= 7.6) {
       v_guodao = true;
  }
  if (z >= 8.6) {
       v_shengdao = true;
  }
  if (z >= 4.6 && z <= 10) {
       v_capital = true;
  }
  if (z >= 5.1) {
       v_nanhai = true;
  }
  if (z >= 5.4) {
       v_nanhaidaoyu = true;
  }
  var boundary = { layers: 'basemap:boundary', visible: v_boundary };
  var nanhai = { layers: 'basemap:nanhai', visible: v_nanhai };
  var nanhaidaoyu = { layers: 'basemap:nanhaidaoyu', visible: v_nanhaidaoyu };
  var capital = { layers: 'basemap:capital', bbox: _bbox, visible: v_capital };
  var province = {
       layers: 'basemap:province, basemap:River1_4l, basemap:River1_4p',
       visible: v_province
  };
  var provinceName = { layers: 'basemap:provinceName', bbox: _bbox, visible: v_provinceName };
  var city = {
       layers: 'basemap:city, basemap:River2_4l, basemap:River2_4p', bbox: _bbox,
       visible: v_city
  };
  var cityName = { layers: 'basemap:cityName', bbox: _bbox, visible: v_cityName };
  var county = {
       layers: 'basemap:county, basemap:River2_4l, basemap:River2_4p, basemap:River4_4l',
       styles: ',me_river2l_label,me_river2p_label,', visible: v_county
  };
  var countyName = { layers: 'basemap:countyName', bbox: _bbox, visible: v_countyName };
  var town = {
       layers: 'basemap:county, basemap:River2_4l, basemap:River2_4p, basemap:River4_4l, basemap:River5_4l',
       styles: ',me_river2l_label,me_river2p_label,,', visible: v_town
  };
  //
  var tielu = { layers: 'basemap:tielu', bbox: _bbox, visible: v_tielu };
  var gaosu = { layers: 'basemap:gaosu', bbox: _bbox, visible: v_gaosu };
  var guodao = { layers: 'basemap:guodao', bbox: _bbox, visible: v_guodao };
  var shengdao = { layers: 'basemap:shengdao', bbox: _bbox, visible: v_shengdao };

  //
  mapDiv.addMapTileWMS('province', wmsUrl, province, 0, 5.7); //采用瓦片WMS，提升访问速度
  mapDiv.addMapImageWMS('provinceName', wmsUrl, provinceName, 4.3, 4.6);//采用图片WMS，防止标注遗缺或重叠
  mapDiv.addMapTileWMS('city', wmsUrl, city, 5.6, 7.7);
  mapDiv.addMapImageWMS('cityName', wmsUrl, cityName, 5.6, 7.7);
  mapDiv.addMapTileWMS('county', wmsUrl, county, 7.6, 10.7);
  mapDiv.addMapImageWMS('countyName', wmsUrl, countyName, 7.6, ze);
  mapDiv.addMapTileWMS('town', wmsUrl, town, 10.6, ze);


  //
  //mapDiv.addMapTileWMS('gaosu', wmsUrl, gaosu, 5.6, ze); //关闭高速
  //mapDiv.addMapTileWMS('tielu', wmsUrl, tielu, 6.6, ze); //关闭铁路
  //mapDiv.addMapTileWMS('guodao', wmsUrl, guodao, 7.6, ze); //关闭道路
  //mapDiv.addMapTileWMS('shengdao', wmsUrl, shengdao, 8.6, ze); //关闭道路
  //
  mapDiv.addMapImageWMS('capital', wmsUrl, capital, 4.6, 10);
  mapDiv.addMapTileWMS('boundary', wmsUrl, boundary, 0, ze);
  mapDiv.addMapTileWMS('nanhan', wmsUrl, nanhai, 5.1, ze);
  mapDiv.addMapImageWMS('nanhandaoyu', wmsUrl, nanhaidaoyu, 5.4, ze);
  mapDiv.addCapital(0, 4.5); //添加首都4.5
  view.on('change:resolution', function (evt) {
       var z = view.getZoom();
       map.getLayers().forEach(function (lyr) {
            console.log();
            if (lyr.get('id') == 'gaosu') {
                 if (z < 8.1) { //恢复默认，隐藏标注
                      lyr.getSource().updateParams({ 'STYLES': '' });
                 } else { //设置省文件样式为me_city，显示标注
                      lyr.getSource().updateParams({ 'STYLES': "me_road_gaosu_label" });
                 }
            }
            if (lyr.get('id') == 'tielu') {
                 if (z < 8.4) { //恢复默认，隐藏标注
                      lyr.getSource().updateParams({ 'STYLES': '' });
                 } else { //设置省文件样式为me_city，显示标注
                      lyr.getSource().updateParams({ 'STYLES': "me_tielu_label" });
                 }
            }
            if (lyr.get('id') == 'guodao') {
                 if (z < 9.4) { //恢复默认，隐藏标注
                      lyr.getSource().updateParams({ 'STYLES': '' });
                 } else { //设置省文件样式为me_city，显示标注
                      lyr.getSource().updateParams({ 'STYLES': "me_road_guodao_label" });
                 }
            }
       });
  });
}


export function load_basemap_ori(url = '', mapDiv, map, view) {
  //
  url = url != '' ? url : 'http://localhost:8080';
  var wmsUrl = url + '/geoserver/basemap/wms';
  var ze = 20;
  var _bbox = [73.50, 3.83, 135.10, 53.57];
  var boundary = { layers: 'basemap:boundary', visible: true };
  var nanhai = { layers: 'basemap:nanhai', visible: false };
  var nanhaidaoyu = { layers: 'basemap:nanhaidaoyu', visible: false };
  var capital = { layers: 'basemap:capital', bbox: _bbox, visible: false };
  var province = { layers: 'basemap:province, basemap:River1_4l, basemap:River1_4p', visible: true };
  var provinceName = { layers: 'basemap:provinceName', bbox: _bbox, visible: false };
  var city = { layers: 'basemap:city, basemap:River2_4l, basemap:River2_4p', bbox: _bbox, visible: false };
  var cityName = { layers: 'basemap:cityName', bbox: _bbox, visible: false };
  var county = {
       layers: 'basemap:county, basemap:River2_4l, basemap:River2_4p, basemap:River4_4l',
       styles: ',me_river2l_label,me_river2p_label,', visible: false
  };
  var countyName = { layers: 'basemap:countyName', bbox: _bbox, visible: false };
  var town = {
       layers: 'basemap:county, basemap:River2_4l, basemap:River2_4p, basemap:River4_4l, basemap:River5_4l',
       styles: ',me_river2l_label,me_river2p_label,,', visible: false
  };
  //
  var tielu = { layers: 'basemap:tielu', bbox: _bbox, visible: false };
  var gaosu = { layers: 'basemap:gaosu', bbox: _bbox, visible: false };
  var guodao = { layers: 'basemap:guodao', bbox: _bbox, visible: false };
  var shengdao = { layers: 'basemap:shengdao', bbox: _bbox, visible: false };
  //
  mapDiv.addMapTileWMS('province', wmsUrl, province, 0, 5.7); //采用瓦片WMS，提升访问速度
  mapDiv.addMapImageWMS('provinceName', wmsUrl, provinceName, 4.3, 4.6);//采用图片WMS，防止标注遗缺或重叠
  mapDiv.addMapTileWMS('city', wmsUrl, city, 5.6, 7.7);
  mapDiv.addMapImageWMS('cityName', wmsUrl, cityName, 5.6, 7.7);
  mapDiv.addMapTileWMS('county', wmsUrl, county, 7.6, 10.7);
  mapDiv.addMapImageWMS('countyName', wmsUrl, countyName, 7.6, ze);
  mapDiv.addMapTileWMS('town', wmsUrl, town, 10.6, ze);
  //
  //mapDiv.addMapTileWMS('gaosu', wmsUrl, gaosu, 5.6, ze); //关闭高速
  //mapDiv.addMapTileWMS('tielu', wmsUrl, tielu, 6.6, ze); //关闭铁路
  //mapDiv.addMapTileWMS('guodao', wmsUrl, guodao, 7.6, ze); //关闭道路
  //mapDiv.addMapTileWMS('shengdao', wmsUrl, shengdao, 8.6, ze); //关闭道路
  //
  mapDiv.addMapImageWMS('capital', wmsUrl, capital, 4.6, 10);
  //
  mapDiv.addMapTileWMS('boundary', wmsUrl, boundary, 0, ze);
  mapDiv.addMapTileWMS('nanhan', wmsUrl, nanhai, 5.1, ze);
  mapDiv.addMapImageWMS('nanhandaoyu', wmsUrl, nanhaidaoyu, 5.4, ze);
  mapDiv.addCapital(0, 4.5); //添加首都
  view.on('change:resolution', function (evt) {
       var z = view.getZoom();
       map.getLayers().forEach(function (lyr) {
            if (lyr.get('id') == 'gaosu') {
                 if (z < 8.1) { //恢复默认，隐藏标注
                      lyr.getSource().updateParams({ 'STYLES': '' });
                 } else { //设置省文件样式为me_city，显示标注
                      lyr.getSource().updateParams({ 'STYLES': "me_road_gaosu_label" });
                 }
            }
            if (lyr.get('id') == 'tielu') {
                 if (z < 8.4) { //恢复默认，隐藏标注
                      lyr.getSource().updateParams({ 'STYLES': '' });
                 } else { //设置省文件样式为me_city，显示标注
                      lyr.getSource().updateParams({ 'STYLES': "me_tielu_label" });
                 }
            }
            if (lyr.get('id') == 'guodao') {
                 if (z < 9.4) { //恢复默认，隐藏标注
                      lyr.getSource().updateParams({ 'STYLES': '' });
                 } else { //设置省文件样式为me_city，显示标注
                      lyr.getSource().updateParams({ 'STYLES': "me_road_guodao_label" });
                 }
            }
       });
  });
}

