import { FontAwesome } from "@expo/vector-icons"
import { format, parseISO } from "date-fns"
import React from "react"
import { TouchableOpacity, View, Text } from "react-native"
import { LMComment } from "../../../../hooks/useComments"
import { authStore } from "../../../../stores/AuthStore"

/**
 * Props for the {@link Comment} component.
 */
 export interface CommentProps {
    /**
     * The [comment]{@link LMComment} object being displayed by the {@link Comment} component.
     */
    comment: LMComment
    /**
     * Whether or not this comment is selected and should be highlighted
     */
    selected: boolean
    focusComment: (id: string) => void
    startEditingComment: (comment: LMComment) => void
    deleteComment: (id: string) => void
}

/**
 * Component that displays a {@link LMComment} object in a clean format.
 * @component
 */
 export const CommentView: React.FC<CommentProps> = ({comment, selected, focusComment: selectComment, startEditingComment: startEditingComment, deleteComment}) => {
    return (
        <TouchableOpacity style={[{paddingHorizontal: 10}, selected ? {backgroundColor: '#E8E8E8'}: null]} onPress={() => selectComment(comment.id)}>
            <View style={{paddingTop: 10,  flexDirection: 'row', justifyContent: 'space-between'}}>
                <Text style={{fontWeight: 'bold'}}>{comment.poster_name}:</Text>
                <Text style={{fontSize: 12, color: 'gray'}}>{format(parseISO(comment.timestamp.toString()), "MMMM do, yyyy h:mma")}</Text>
            </View>
            <View style={{marginVertical: 10}}>
                <Text style={{paddingBottom: 10}} >{comment.content}</Text>
                <View style={{flexDirection: 'row', alignSelf: 'flex-end'}}>
                    {comment.edited ? <Text style={{color: 'grey', alignSelf: 'flex-end'}}>Edited</Text> : null}
                    {selected && comment.poster == authStore.userId ?
                    <View style={{marginTop: 10, flexDirection: 'row', alignSelf: 'flex-end'}}>
                        <FontAwesome size={25} name="edit" style={{paddingTop: 1, marginLeft: 20}} onPress={() => startEditingComment(comment)}/>
                        <FontAwesome color="red" size={25} style={{marginLeft: 15}} name="trash" onPress={() => deleteComment(comment.id)}/>
                    </View> : null}
                </View>
            </View>
        </TouchableOpacity>
    )}