import React from "react";
import { View, Text, Image, TouchableOpacity, StyleSheet } from "react-native";
import { globalStyles } from "@Styles/global";
import { windowWidth } from "@Utils/Dimensions";
import { useTheme } from "@Styles";
import { useTranslation } from "react-i18next";

const ListItem = ({ photo, title, onPress, publishedAt }) => {
  const { colors } = useTheme();
  const { t } = useTranslation();
  const formatDate = (date) => {
    const tempDate = new Date(date);
    const fDate =
      tempDate.getDate() +
      "/" +
      (tempDate.getMonth() + 1) +
      "/" +
      tempDate.getFullYear();

      return fDate;
  };

  return (
    <View style={styles.cardContainer}>
      <View style={styles.imageContainer}>
        <Image
          style={styles.image}
          source={{ uri: `http://localhost:1337${photo.small.url}` }}
        />
        <View style={{ width: windowWidth - 220 }}>
          <Text
            style={{
              color: colors.textPrimary,
              fontFamily: "poppins-regular",
              fontSize: 14,
              textTransform: "uppercase",
            }}
          >
            {title}
          </Text>
          <Text
            style={{
              color: colors.textPrimary,
              fontFamily: "poppins-regular",
              fontSize: 14,
              textTransform: "uppercase",
            }}
          >
            Published At {formatDate(publishedAt)}
          </Text>
        </View>
      </View>
      <TouchableOpacity style={globalStyles.button} onPress={onPress}>
        <Text style={styles.buttonText}>{t('common.readMore')}</Text>
      </TouchableOpacity>
    </View>
  );
};

const styles = StyleSheet.create({
  cardContainer: {
    flexDirection: "row",
    justifyContent: "space-between",
    alignItems: "center",
    marginBottom: 20,
  },
  imageContainer: {
    flexDirection: "row",
    alignItems: "center",
    flex: 1,
  },
  image: {
    width: 55,
    height: 55,
    borderRadius: 10,
    marginRight: 8,
  },
  buttonText: {
    color: "#fff",
    textAlign: "center",
    fontFamily: "poppins-regular",
    fontSize: 14,
  },
});

export default ListItem;
