import { creatInfo } from './info';
import { Dict } from '@zenview/micro-basic-library';
const { zenmap } = window;
const map = zenmap;
const allType = Dict.device.deviceType
  .filter((v) => v.value !== '-1')
  .map(function (item, index) {
    return item.value;
  });

export const creatClusterMarker = (point, clusterOpt) => {
  const { ZenMap } = window;
  // eslint-disable-next-line no-redeclare
  var clusterOpt = clusterOpt
    ? clusterOpt
    : {
        markType: point.cameraType, //唯一的key,状态加功能类型
        image: point.deviceType * 1 === 100616 ? point.deviceType + point.type : point.deviceType + (point.deviceStatus * 1 === 0 ? 'outline' : 'online'), //图标
        title: point.deviceName, //标题
        extendImage: point.deviceStatus * 1 === 0 ? 'outLine' : point.deviceType + 'view',
        extendIconOffset: [0, -32],
        rotation: Math.random() * 180,
      };
  point.lon = parseFloat(point.longitude);
  point.lat = parseFloat(point.latitude);

  let marker = new ZenMap.Symbols.ClusterMarker(point, clusterOpt);
  marker.deviceStatus = point.deviceStatus;
  return marker;
};

export const creatSingleMarker = ({ point, url, urlName, iconSize = 1, layers, labelName, callback, groupName, map, context }) => {
  const { ZenMap } = window;
  if (!point.lon || !point.lat) {
    return;
  }
  var marker;
  var pt = new ZenMap.Geometry.Point(point.lon, point.lat);
  //图片
  var icon = new ZenMap.Symbols.Icon(urlName, iconSize, {
    anchor: new ZenMap.Geometry.Size(0, -16),
  });
  //图像标注
  marker = new ZenMap.Symbols.Marker(pt);
  if (labelName) {
    //创建小标签，带小三角的冒泡标签
    creatInfoForMarker(map, marker, labelName);
  }
  if (context) {
    var label = new ZenMap.Symbols.Label(context); //一个图像标注
    //设置样式
    label.setStyle({
      fontSize: 14, //文字大小
      color: '#FFFFFF', //文字前景色
    });
    label.setOffset(new ZenMap.Geometry.Size(0, -18));
    marker.setLabel(label);
  }
  icon && marker.setIcon(icon);
  if (layers) {
    if (groupName) {
      //已有图层，对图层进行分组添加数据
      let group = layers.addGroup(groupName);
      group.addOverlay(marker);
    } else {
      layers.addOverlay(marker);
    }
  } else {
    map && map.addOverlay(marker);
  }
  callback && callback(marker, icon);
  return marker;
};

export const creatInfoForMarker = (map, marker, content) => {
  creatInfo(
    map,
    marker.getPosition(),
    `${content}`,
    {
      isCommon: false,
      closeButton: false,
      offset: [0, -40], //信息窗位置偏移值
    },
    (infoWindow) => {
      infoWindow.hide(); //默认收起
      marker.addEventListener('mouseover', () => {
        infoWindow.show(); //划过展示
      });
      marker.addEventListener('mouseout', () => {
        infoWindow.hide(); //滑出隐藏
      });
    }
  );
};

export const createMarkerNoCluster = (points, layers, clusterOpt) => {
  const { ZenMap } = window;
  // eslint-disable-next-line no-unused-vars
  let opt = clusterOpt
    ? clusterOpt
    : {
        markerMinZoom: 16,
        markerMaxZoom: 22,
        circleMinZoom: 6,
        circleMaxZoom: 16,
      };

  //获取图标___________
  let cameraIcon = [];
  allType
    .filter((v) => v.value !== '-1')
    .forEach((v) => {
      cameraIcon.push([v.value + 'online', `/micro-apps/micro-dep-librarys/image/zenMap/online-${v.value}.png`]);
      cameraIcon.push([v.value + 'outline', `/micro-apps/micro-dep-librarys/image/zenMap/outline-${v.value}.png`]);
      return;
    });
  map.addImages(
    [
      //地图添加图标
      ...cameraIcon,
    ],
    () => {
      var deviceLayersNoCluster;
      deviceLayersNoCluster = layers ? layers : map; //新建图层
      for (let i = 0, l = points.length; i < l; i++) {
        let point = points[i];
        if (!point.latitude || !point.longitude) {
          continue;
        }
        var icon = new ZenMap.Symbols.Icon(point.deviceType + (point.deviceStatus * 1 === 1 ? 'online' : 'outline'), 1, {
          anchor: new ZenMap.Geometry.Size(0, -16),
        });
        let marker = new ZenMap.Symbols.Marker(new ZenMap.Geometry.Point(point.longitude, point.latitude), clusterOpt);
        if (point.deviceName) {
          //创建小标签，带小三角的冒泡标签
          creatInfoForMarker(marker, point.deviceName);
        }
        marker.setIcon(icon);
        marker.title = point.deviceName;
        marker.enableEditing();
        deviceLayersNoCluster.addOverlay(marker);
      }
    }
  );
};

export const creatSinglePoint = (device) => {
  const { ZenMap } = window;
  var point = new ZenMap.Geometry.Point(device.longitude || device.lon, device.latitude || device.lat);
  point.title = device.deviceName;
  point.location = device;
  return point;
};
