/**
 * 通知消息渲染项目组件
 * @邠心vbe on 2023/08/17
 */
import React from 'react';
import { Pressable, StyleSheet, Text, View } from 'react-native';
import TextView from '../../components/TextView';

const IconType = ({type, style, size=32, color=colorAccent}) => {
  switch (type) {
    case "Applications":
      return (
        <MaterialIcons
          name="admin-panel-settings"
          size={size}
          style={style}
          color={color}
        />
      )
    case "Admin":
      return (
        <MaterialCommunityIcons
          name="message-draw"
          size={size}
          style={style}
          color={color}
        />
      )
    case "Charging":
      return (
        <MaterialIcons
          name="offline-bolt"
          size={size}
          style={style}
          color={color}
        />
      )
    case "Wallet":
      return (
        <MaterialCommunityIcons
          name="cash-multiple"
          size={size}
          style={style}
          color={color}
        />
      )
    case "Announcement":
      return (
        <MaterialCommunityIcons
          name="message-alert"
          size={size}
          style={style}
          color={color}
        />
      )
    case "Review":
      return (
        <MaterialCommunityIcons
          name="message-draw"
          size={size}
          style={style}
          color={color}
        />
      )
    case "Promotion":
      return (
        <MaterialCommunityIcons
          name="bullhorn-variant"
          size={size}
          style={style}
          color={color}
        />
      )
    default:
      return (
        <MaterialCommunityIcons
          name="message-alert"
          size={size}
          style={style}
          color={color}
        />
      )
  }
}

export default ItemView = ({item, index, separators, onPress, onLongPress}) => {
  const getDotColor = () => {
    if (item.readStatus) {
      if (item?.notificationTitle.indexOf("Low") >= 0/* || item.notificationType == "Announcement"*/) {
        return "#F09327";
      } else {
        return colorAccent;
      }
    } else {
      return "#FF3B30";
    }
  }
  return (
    <Pressable
      style={styles.notyItemView}
      onPress={onPress}
      android_ripple={ripple}
      onLongPress={onLongPress}>
      <IconType
        style={styles.iconType}
        type={item.notificationType}
        color={getDotColor()}
      />
      <View style={styles.itemContent}>
        <View style={ui.flexc}>
          <TextView
            style={[styles.textTitle, (!item.readStatus && styles.unread)]}
            numberOfLines={1}
            ellipsizeMode="tail">
            {item.notificationTitle}
          </TextView>
          <TextView
            style={[styles.textDate, (!item.readStatus && styles.unread)]}
            numberOfLines={1}>
            {item.createTime}
          </TextView>
        </View>
        <TextView
          style={styles.textMessage}
          numberOfLines={2}>
          {item.notificationText}
        </TextView>
      </View>
    </Pressable>
  )
}

const styles = StyleSheet.create({
  notyItemView: {
    padding: 16,
    alignItems: 'center',
    flexDirection: 'row'
  },
  iconType: {
    marginRight: 12
  },
  readIcon: {
    top: 2,
    left: -5,
    width: 7,
    height: 6,
    position: 'absolute'
  },
  itemContent: {
    flex: 1
  },
  textTitle: {
    flex: 1,
    color: textPrimary,
    fontSize: 15
  },
  textDate: {
    color: textPrimary,
    fontSize: 12
  },
  unread: {
    fontWeight: 'bold'
  },
  textMessage: {
    color: textPrimary,
    fontSize: 12
  }
})