/**
 * 地图聚合组件
 * @邠心vbe on 2021/08/13
 */
import React from 'react';
import { Image, ImageBackground, StyleSheet, Text } from 'react-native';
import { Marker } from 'react-native-maps';


export const MyMarker = ({id, coordinate, onPress}) => {
  if (coordinate.upcoming) {
    return (
      <Marker
        key={id}
        zIndex={1}
        coordinate={coordinate}
        onPress={onPress}>
        <Image
          style={styles.marker}
          source={require('../../../images/maps/ic_marker_upcoming.png')}/>
      </Marker>
    )
  } else {
    return (
      <Marker
        key={id}
        zIndex={1}
        coordinate={coordinate}
        onPress={onPress}
        style={ui.center}>
        { coordinate.hasLabel &&
          <Image
            style={styles.markerTop}
            source={require('../../../images/maps/ic_marker_additional.png')}/>
        }
        { coordinate.favorite
          ? coordinate.available
            ? <Image
                style={styles.marker}
                source={require('../../../images/maps/ic_marker_star.png')}/>
            : <Image
                style={styles.marker}
                source={require('../../../images/maps/ic_marker_unstar.png')}/>
          : coordinate.available
            ? <Image
                style={styles.marker}
                source={require('../../../images/maps/ic_marker.png')}/>
            : <Image
                style={styles.marker}
                source={require('../../../images/maps/ic_marker_un.png')}/>
        }
      </Marker>
    )
  }
}

export const MyCluster = (props) => {
  const {id, location, properties, onPress, available=false, onOpen} = props;
  const pointCount = props.pointCount ? props.pointCount : properties.point_count
  //console.log('renderCluster', props);

  return (
    <Marker
      key={id}
      style={{ zIndex: pointCount + 1 }}
      coordinate={location}
      zIndex={pointCount + 1}
      onPress={() => onOpen ? onOpen(location) : onPress()}>
      <ClusterView pointCount={pointCount} available={available}/>
    </Marker>
  );
}

export const ClusterView = ({pointCount, available=false}) => {
  const getStyle = (count) => {
    if (count >= 100) {
      return [ui.flexcc, styles.large];
    } else if (count >= 10) {
      return [ui.flexcc, styles.middle];
    } else {
      return [ui.flexcc, styles.small];
    }
  }
  return (
    <ImageBackground
      style={getStyle(pointCount)}
      source={available
            ? require('../../../images/maps/ic_cluster.png')
            : require('../../../images/maps/ic_cluster_un.png')}>
      <Text style={available ? styles.textAvailable : styles.textUnavailable}>{pointCount}</Text>
    </ImageBackground>
  );
}

const styles = StyleSheet.create({
  marker: {
    width: 48,
    height: 48
  },
  markerTop: {
    width: 24,
    height: 24,
    marginBottom: -2
  },
  small: {
    width: 48,
    height: 48
  },
  middle: {
    width: 50,
    height: 50
  },
  large: {
    width: 52,
    height: 52
  },
  textAvailable: {
    color: textLight,
    fontSize: 18,
    fontWeight: 'bold',
    paddingBottom: 10
  },
  textUnavailable: {
    color: '#999',
    fontSize: 18,
    fontWeight: 'bold',
    paddingBottom: 10
  }
})