/**
 * 首页地图组件-使用ClusteringMapView和VbeClusterMap
 * @邠心vbe on 2021/03/18
 */
import React, { useEffect, useRef } from 'react';
import { PROVIDER_GOOGLE } from 'react-native-maps';
import MapView from "react-native-map-clustering";
import { MyCluster, MyMarker } from './Cluster';
import VbeClusterMap from 'vbe-cluster-map'

export default Maps3 = ({ region, onMapReady, stopList, showUserLocation=true, onMarkerPress }) => {
  const mapRef = useRef();
  const superClusterRef = useRef();

  useEffect(() => {
  if (isIOS) {
    //mapRef.current.animateToRegion(region, 500);
    mapRef.current.animateCamera({ center: region, zoom: 17 }, { duration: 500 });
    //mapRef.current.animateCamera({ center: region, zoom: 17 }, 500); //移动地图到当前位置并放大
  } else {
    if (mapRef.current.moveCamera) {
      mapRef.current.moveCamera(region, 17);
      //mapRef.current.showUserLocation();
    }
  }
  }, [region])

  /*componentDidMount() {
    this.mapRef = this.ref.current.mapRef ? this.ref.current.mapRef : this.ref.current;
  }*/

  const renderCluster = (props) => {
    //console.log("renderCluster", props);
    let hasAvailableConnector = false;
    if (props.geometry.hasAvailableConnector === undefined) {
      if (superClusterRef) {
        const points = superClusterRef.current.getLeaves(props.id, Infinity, 0);
        if (points) {
          for (let index = 0; index < points.length; index++) {
            const point = points[index];
            if (point.properties.coordinate?.available) {
              hasAvailableConnector = true;
              break;
            }
          }
        }
      }
    } else {
      hasAvailableConnector = props.geometry.hasAvailableConnector
    }
    props.geometry.hasAvailableConnector = hasAvailableConnector;
    const latlng = {latitude: props.geometry.coordinates[1], longitude: props.geometry.coordinates[0]}
    //InteractionManager.runAfterInteractions();
    return (
      <MyCluster
        {...props}
        key={props.id}
        location={latlng}
        available={hasAvailableConnector}
        onOpen1={latlng => openClusterMarker(latlng)}/>
    );
  }

  const openClusterMarker = async (latlng) => {
    var camera = await mapRef.current.getCamera();
    camera.center = latlng
    camera.zoom = camera.zoom + 2
    //console.log('camera', camera);
    mapRef.current.animateCamera(camera, 300);
  }

  return (
    isIOS
    ? <MapView
      ref={mapRef}
      superClusterRef={superClusterRef}
      style={ui.flex1}
      minZoom={10}
      maxZoom={15}
      radius={45}
      extent={512}
      nodeSize={64}
      provider={PROVIDER_GOOGLE}
      onMapReady={onMapReady}
      showsUserLocation={showUserLocation}
      initialRegion={region}
      renderCluster={(info) => renderCluster(info)}>
      { stopList.map((marker, index) => {
        return (
          <MyMarker
            key={index}
            {...marker}
            coordinate={marker}
            onPress={() => onMarkerPress(marker.id)}
          />
        );
      })}
    </MapView>
  : <VbeClusterMap
      ref={mapRef}
      style={ui.flex1}
      region={region}
      data={stopList}
      animation={true}
      onMapReady={onMapReady}
      onMarkerPress={e => onMarkerPress(e.id)}
      showUserLocation={showUserLocation}
      moveOnMarkerPress={true}
    />
  );
}