package seedu.todo.ui.components;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import javafx.fxml.FXML;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import seedu.todo.commons.EphemeralDB;
import seedu.todo.commons.util.DateUtil;
import seedu.todo.models.Task;
import seedu.todo.models.Event;
import seedu.todo.ui.UiPartLoader;

public class TaskListDateItem extends MultiComponent {

    private static final String FXML_PATH = "components/TaskListDateItem.fxml";
    private static EphemeralDB ephemeralDb = EphemeralDB.getInstance();
    private static final String NO_DATE_STRING = "No Deadline";

    // Props
    public LocalDateTime dateTime;
    public List<Task> tasks = new ArrayList<>();
    public List<Event> events = new ArrayList<>();

    // FXML
    @FXML
    private Text dateHeader;
    @FXML
    private Text dateLabel;
    @FXML
    private VBox dateCalendarItemsPlaceholder;

    public static TaskListDateItem load(Stage primaryStage, Pane placeholderPane) {
        return UiPartLoader.loadUiPart(primaryStage, placeholderPane, new TaskListDateItem());
    }

    @Override
    public String getFxmlPath() {
        return FXML_PATH;
    }

    @Override
    public void componentDidMount() {
        String dateHeaderString;
        
        // Set header for DateItem
        if (dateTime == TaskList.NO_DATE_VALUE) {
            dateHeaderString = NO_DATE_STRING;
        } else {
            dateHeaderString = DateUtil.formatDay(dateTime);
        }
        
        dateHeader.setText(dateHeaderString);
        
        // Set date label
        String dateLabelString = DateUtil.formatShortDate(dateTime);
        dateLabel.setText(dateLabelString);

        // Clear the TaskList of its items
        TaskListTaskItem.reset(dateCalendarItemsPlaceholder);

        // Load task and event items
        loadEventItems();
        loadTaskItems();
    }

    private void loadTaskItems() {
        for (Task task : tasks) {
            TaskListTaskItem item = TaskListTaskItem.load(primaryStage, dateCalendarItemsPlaceholder);

            // Add to EphemeralDB and get the index.
            int displayIndex = ephemeralDb.addToDisplayedCalendarItems(task);

            // Set the props and render the TaskListTaskItem.
            item.task = task;
            item.displayIndex = displayIndex;
            item.render();
        }
    }
    
    private void loadEventItems() {
        for (Event event : events) {
            TaskListEventItem item = TaskListEventItem.load(primaryStage, dateCalendarItemsPlaceholder);

            // Add to EphemeralDB and get the index.
            int displayIndex = ephemeralDb.addToDisplayedCalendarItems(event);

            // Set the props and render the TaskListTaskItem.
            item.event = event;
            item.displayIndex = displayIndex;
            item.render();
        }
    }

}

@@ -1332,41 +1332,8 @@
 ) %7B%0A
-        String dateHeaderString;%0A
     
@@ -1371,16 +1371,79 @@
 DateItem
+ using the %22x days ago%22 format%0A        String dateHeaderString;
 %0A       
@@ -1620,25 +1620,17 @@
       %7D%0A
-        
 %0A
+
         
@@ -1701,17 +1701,117 @@
 te label
-%0A
+ using the short date format (e.g. Fri 14 Oct)%0A        if (dateTime != TaskList.NO_DATE_VALUE) %7B%0A    
         
@@ -1875,24 +1875,28 @@
 e);%0A        
+    
 dateLabel.se
@@ -1918,16 +1918,26 @@
 String);
+%0A        %7D
 %0A%0A      

Fix null date exception in TaskListDateItem