const options = {
  url: '/arcgisApi/4.7/init.js',
};

import axios from 'axios';
import { _getLngLat } from './mercatorToLngLat';

let cancelRequest;
var cancelToken = new axios.CancelToken(function executor(c) {
  cancelRequest = c;
  console.log(c);
  // 这个参数 c 就是CancelToken构造函数里面自带的取消请求的函数，这里把该函数当参数用
});
var frgrd = ''; //报汛等级
var wintFlowWidthZoom = [
  {
    zoom: 0,
    frgrd: '1',
  },
  {
    zoom: 1,
    frgrd: '1',
  },
  {
    zoom: 2,
    frgrd: '1',
  },
  {
    zoom: 3,
    frgrd: '1',
  },
  {
    zoom: 4,
    frgrd: '1',
  },
  {
    zoom: 5,
    frgrd: '1',
  },
  {
    zoom: 6,
    frgrd: '1',
  },
  {
    zoom: 7,
    frgrd: '1',
  },
  {
    zoom: 8,
    frgrd: '1,2',
  },
  {
    zoom: 9,
    frgrd: '1,2,3,4',
  },
  {
    zoom: 10,
    frgrd: '1,2,3,4,5,6',
  },
  {
    zoom: 11,
    frgrd: '',
  },
  {
    zoom: 12,
    frgrd: '',
  },
  {
    zoom: 13,
    frgrd: '',
  },
  {
    zoom: 14,
    frgrd: '',
  },
  {
    zoom: 15,
    frgrd: '',
  },
  {
    zoom: 16,
    frgrd: '',
  },
  {
    zoom: 17,
    frgrd: '',
  },
  {
    zoom: 18,
    frgrd: '',
  },
];

import _default, * as esriLoader from 'esri-loader';
import { getSwenz } from '../../../api/zhxxAPI.js';
export function getSwenzList(_self) {
  esriLoader
    .loadModules(
      [
        'esri/Graphic',
        'esri/geometry/Point',
        'esri/geometry/SpatialReference',
        'dojo/domReady!',
      ],
      options,
    )
    .then(([Graphic, Point, SpatialReference]) => {
      var graphicLayerSwz = _self.mapObject.findLayerById('graphicLayerSwz');
      graphicLayerSwz.removeAll();
      wintFlowWidthZoom.forEach((item) => {
        if (item.zoom == _self.viewObject.zoom) {
          frgrd = item.frgrd;
        }
      });
      // var symbol = {
      //     type: "picture-marker",
      //     url: "/src/views/zongHeXinXiNew/assets/swenz.png",
      //     width: "18px",
      //     height: "22px"
      // };
      const dictSymbols = {
        symbol_RR: {
          type: 'picture-marker',
          url: '/src/views/zongHeXinXiNew/assets/station/RR.png',
          width: '12px',
          height: '12px',
        },
        symbol_BB: {
          type: 'picture-marker',
          url: '/src/views/zongHeXinXiNew/assets/station/BB.png',
          width: '12px',
          height: '12px',
        },
        symbol_DD: {
          type: 'picture-marker',
          url: '/src/views/zongHeXinXiNew/assets/station/DD.png',
          width: '12px',
          height: '12px',
        },
        symbol_DP: {
          type: 'picture-marker',
          url: '/src/views/zongHeXinXiNew/assets/station/DP.png',
          width: '12px',
          height: '12px',
        },
        symbol_MM: {
          type: 'picture-marker',
          url: '/src/views/zongHeXinXiNew/assets/station/MM.png',
          width: '12px',
          height: '12px',
        },
        symbol_PP: {
          type: 'picture-marker',
          url: '/src/views/zongHeXinXiNew/assets/station/PP.png',
          width: '12px',
          height: '12px',
        },
        symbol_SS: {
          type: 'picture-marker',
          url: '/src/views/zongHeXinXiNew/assets/station/SS.png',
          width: '12px',
          height: '12px',
        },
        symbol_TT: {
          type: 'picture-marker',
          url: '/src/views/zongHeXinXiNew/assets/station/TT.png',
          width: '12px',
          height: '12px',
        },
        symbol_ZB: {
          type: 'picture-marker',
          url: '/src/views/zongHeXinXiNew/assets/station/ZB.png',
          width: '13px',
          height: '7px',
        },
        symbol_ZQ: {
          type: 'picture-marker',
          url: '/src/views/zongHeXinXiNew/assets/station/ZQ.png',
          width: '12px',
          height: '12px',
        },
        symbol_ZZ: {
          type: 'picture-marker',
          url: '/src/views/zongHeXinXiNew/assets/station/ZZ.png',
          width: '12px',
          height: '12px',
        },
      };
      var maxLgtd = _self.viewObject.extent.xmax;
      var maxLttd = _self.viewObject.extent.ymax;
      var minLgtd = _self.viewObject.extent.xmin;
      var minLttd = _self.viewObject.extent.ymin;
      var bottomLeft = _getLngLat({
        x: minLgtd,
        y: minLttd,
      });
      var topRight = _getLngLat({
        x: maxLgtd,
        y: maxLttd,
      });
      var maxLgtd1 = topRight.lng;
      var maxLttd1 = topRight.lat;
      var minLgtd1 = bottomLeft.lng;
      var minLttd1 = bottomLeft.lat;
      var gra, pt, symbol;
      cancelRequest;
      getSwenz(
        _self.currentAdCode,
        maxLgtd1,
        maxLttd1,
        minLgtd1,
        minLttd1,
        frgrd,
      ).then((res) => {
        if (res.data) {
          res.data.forEach((item) => {
            if (item.lgtd && item.lttd && item.sttp == 'ZQ') {
              pt = new Point(item.lgtd, item.lttd, new SpatialReference(4326));
              symbol = dictSymbols['symbol_' + item.sttp];
              if (item.stazt) {
                symbol.angle = item.stazt;
              }
              if (symbol) {
                gra = new Graphic({
                  geometry: pt,
                  symbol: symbol,
                  attributes: item,
                });
                graphicLayerSwz.add(gra);
              }
            }
          });
        }
      });
    })
    .catch((err) => {
      console.error(err);
    });
}
