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

import _default, * as esriLoader from 'esri-loader';
import { getPersonTrack } from '../../../api/zhxxAPI.js';
import { dateFormat } from '../../../utils/gw_date';

var trackArr = [];
var sttm, ettm;
var contIntervalTime;
export function getPerTrack(_self, item, obj) {
  esriLoader
    .loadModules(
      [
        'esri/Graphic',
        'esri/geometry/Point',
        'esri/geometry/SpatialReference',
        'esri/geometry/Polyline',
        'dojo/domReady!',
      ],
      options,
    )
    .then(([Graphic, Point, SpatialReference, Polyline]) => {
      var trackGraphicLayer =
        _self.mapObject.findLayerById('trackGraphicLayer');
      trackGraphicLayer.removeAll();
      trackGraphicLayer.visible = true;
      var trackGraphicLayer1 =
        _self.mapObject.findLayerById('trackGraphicLayer1');
      trackGraphicLayer1.removeAll();
      trackGraphicLayer1.visible = true;

      var symbolLine = {
        type: 'simple-line',
        color: [255, 0, 0, 1],
        width: '5px',
        style: 'solid',
      };
      var symbol_start = {
        type: 'picture-marker', // autocasts as new PictureMarkerSymbol()
        url: '/src/views/zongHeXinXiNew/assets/xhstart.png',
        width: '40px',
        height: '40px',
      };
      var symbol_end = {
        type: 'picture-marker', // autocasts as new PictureMarkerSymbol()
        url: '/src/views/zongHeXinXiNew/assets/xhend.png',
        width: '40px',
        height: '40px',
      };
      var gra, pt, symbol;
      trackArr = [];
      if (obj != 'track') {
        if (!sttm && !ettm) {
          sttm = dateFormat(new Date());
          ettm = dateFormat(
            new Date(new Date().getTime() - 24 * 60 * 60 * 1000),
          );
        }
        getPersonTrack(item.guid, sttm, ettm).then((res) => {
          if (res.data) {
            for (var i = res.data.length - 1; i >= 0; i--) {
              if (res.data[i].latitude && res.data[i].longitude) {
                trackArr.push([res.data[i].longitude, res.data[i].latitude]);
              }
            }
          }
        });
      } else {
        trackArr = item.pointList;
        var ptStart = new Point(trackArr[0][0], trackArr[0][1]);
        var ptEnd = new Point(
          trackArr[trackArr.length - 1][0],
          trackArr[trackArr.length - 1][1],
        );
        var graStart = new Graphic({
          geometry: ptStart,
          symbol: symbol_start,
        });
        trackGraphicLayer.add(graStart);
        var graEnd = new Graphic({
          geometry: ptEnd,
          symbol: symbol_end,
        });
        trackGraphicLayer1.add(graEnd);
      }
      if (trackArr.length < 0) {
        _self.showInfo('track', '暂无巡河轨迹信息!');
      } else {
        var symbol = {
          type: 'simple-marker', // autocasts as new SimpleMarkerSymbol()
          style: 'circle',
          color: 'red',
          size: '5px', // pixels
          outline: {
            // autocasts as new SimpleLineSymbol()
            color: [255, 255, 0],
            width: 0, // points
          },
        };
        trackArr.forEach((item) => {
          var pt = new Point(item[0], item[1]);
          var gra = new Graphic({
            geometry: pt,
            symbol: symbol,
          });
          trackGraphicLayer1.add(gra);
        });
        // var line = new Polyline({
        //     paths: [trackArr],
        //     spatialReference: { wkid: 4326 }
        // });
        // contIntervalTime = Math.round(5000/trackArr.length);
        // var hehuTempGra = new Graphic({
        //     geometry:line,
        //     symbol:symbolLine,
        //     attributes:null
        // });
        // _self.viewObject.extent = hehuTempGra.geometry.extent;
        // var lod = Number(_self.viewObject.zoom);
        // var lod1 = Number(lod - 2);
        // _self.viewObject.goTo({
        //     target:hehuTempGra,
        //     zoom:lod1,
        // }).then(function(){
        //     let realPathArray = trackArr;
        //     console.log('realPathArray      ');
        //     realAnimateFun(realPathArray,0,_self);
        // });
      }
    })
    .catch((err) => {
      console.error(err);
    });
}

function realAnimateFun(pathsArray, startIndex, _self) {
  console.log('realAnimateFun' + startIndex);
  const currentIndex = startIndex;
  const currentPaths = [];
  if (currentIndex + 1 >= pathsArray.length) {
    return;
  }
  currentPaths.push(pathsArray[currentIndex]);
  currentPaths.push(pathsArray[currentIndex + 1]);
  esriLoader.dojoRequire(
    [
      'esri/Graphic',
      'esri/geometry/Point',
      'esri/geometry/SpatialReference',
      'esri/layers/GraphicsLayer',
      'esri/geometry/Polygon',
      'esri/geometry/Polyline',
    ],
    (Graphic, Point, SpatialReference, GraphicsLayer, Polygon, Polyline) => {
      var trackGraphicLayer =
        _self.mapObject.findLayerById('trackGraphicLayer');
      // trackGraphicLayer.removeAll();
      var symbolLine = {
        type: 'simple-line',
        color: [255, 0, 0, 1],
        width: '3px',
        style: 'solid',
      };
      const line = new Polyline({
        paths: [currentPaths],
        spatialReference: { wkid: 4326 },
      });
      const hehuTempGra = new Graphic({
        geometry: line,
        symbol: symbolLine,
        attributes: null,
      });

      trackGraphicLayer.add(hehuTempGra);
    },
  );
  setTimeout(() => {
    realAnimateFun(pathsArray, currentIndex + 1, _self);
  }, contIntervalTime);
}
