package fr.insa.clavardator.client.ui.chat;

import fr.insa.clavardator.lib.message.Message;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.control.ListCell;

import java.io.IOException;

public class MessageListItemCell extends ListCell<Message> {

	private MessageListItemController messageListItemController;
	private Node view;

	public MessageListItemCell() {
		setStyle("-fx-padding: 0px");
		FXMLLoader cellLoader = new FXMLLoader(getClass().getResource("messageListItem.fxml"));
		try {
			view = cellLoader.load();
			messageListItemController = cellLoader.getController();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void updateItem(Message item, boolean empty) {
		super.updateItem(item, empty);
		if (item == null || empty) {
			setGraphic(null);
		} else {
			setGraphic(view);
			messageListItemController.setMessage(item);
		}
	}
}