package ctbrec.ui.controls;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.util.Callback;

public class DateTimeCellFactory<T> implements Callback<TableColumn<T, Instant>, TableCell<T, Instant>> {
    @Override
    public TableCell<T, Instant> call(TableColumn<T, Instant> param) {
        return new TableCell<T, Instant>() {
            @Override
            protected void updateItem(Instant item, boolean empty) {
                if (empty || item == null) {
                    setText("");
                } else {
                    LocalDateTime dateTime = LocalDateTime.ofInstant(item, ZoneId.systemDefault());
                    DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
                    String formattedDateTime = formatter.format(dateTime);
                    setText(item.equals(Instant.EPOCH) ? "" : formattedDateTime);
                }
            }
        };
    }
}
