package seedu.address.ui;

import java.text.ParseException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.logging.Logger;

import javax.swing.text.AbstractDocument.Content;

import com.jfoenix.controls.JFXDatePicker;
import com.jfoenix.controls.JFXTextField;
import com.jfoenix.validation.RequiredFieldValidator;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.util.DateTimeUtil;
import seedu.address.logic.Logic;
import seedu.address.logic.commands.CommandResult;
import seedu.address.model.person.ReadOnlyTask;

public class TaskDetail extends UiPart {
    private final Logger logger = LogsCenter.getLogger(TaskDetail.class);
    private static final String FXML = "TaskDetail.fxml";
    private Logic logic;
    private String newContent;
    private int index;
    private LocalDate newDate;
    private ResultDisplay resultDisplay;
    private CommandResult mostRecentResult;

    private AnchorPane placeHolderPane;
    private AnchorPane taskDetailPane;

    @FXML
    private DatePicker datePicker;
    @FXML
    private VBox detailView;
    @FXML
    private JFXTextField content;
    @FXML
    private Label tags;
    @FXML
    private JFXDatePicker timePicker;

    public TaskDetail() {
    }

    public static TaskDetail load(Stage primaryStage, AnchorPane placeHolder, ResultDisplay resultDisplay,
            Logic logic) {
        TaskDetail detail = UiPartLoader.loadUiPart(primaryStage, placeHolder, new TaskDetail());
        detail.configure(resultDisplay, logic);
        detail.addToPlaceHolder();
        detail.initializeTextField();
        return detail;
    }

    private void configure(ResultDisplay resultDisplay, Logic logic) {
        this.logic = logic;

        this.resultDisplay = resultDisplay;
    }

    private void initializeTextField() {
        RequiredFieldValidator validator = new RequiredFieldValidator();
        content.getValidators().add(validator);
        validator.setMessage("No Input Given");
        content.focusedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                if (!newValue) {
                    content.validate();
                }
            }
        });
    }

    public void loadTaskDetail(ReadOnlyTask task, int index) {
        this.index = index + 1;
        content.setText(task.getContent().toString());
        if (task.getDate().getValue() != null) {
            datePicker.setValue(DateTimeUtil.changeDateToLocalDate(task.getDate().getValue()));
        } else {
            datePicker.setValue(null);
        }
        if (task.getTime().getValue() != null) {

            timePicker.setTime(DateTimeUtil.changeDateToLocalTime(task.getTime().getValue()));
        } else {
            timePicker.setValue(null);
        }
        tags.setText(task.tagsString());
    }

    @FXML
    private void handleContentChanged() throws ParseException {
        newContent = content.getText();
        mostRecentResult = logic.execute("edit " + index + " c/" + newContent);
        resultDisplay.postMessage(mostRecentResult.feedbackToUser);
        logger.info("Result: " + mostRecentResult.feedbackToUser);
    }
    
    @FXML
    private void handleDateChanged() throws ParseException {
        newDate = datePicker.getValue();
        mostRecentResult = logic.execute("edit " + index + " d/" + DateTimeUtil.changeLocalDateToFormattedString(newDate));
        resultDisplay.postMessage(mostRecentResult.feedbackToUser);
        logger.info("Result: " + mostRecentResult.feedbackToUser);
    }

    private void addToPlaceHolder() {
        placeHolderPane.getChildren().add(detailView);
    }

    @Override
    public void setNode(Node node) {
        taskDetailPane = (AnchorPane) node;
    }

    @Override
    public void setPlaceholder(AnchorPane pane) {
        this.placeHolderPane = pane;
    }

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

}

@@ -74,255 +74,50 @@
 ime.
-Instant;%0Aimport java.time.LocalDate;%0Aimport java.time.LocalTime;%0Aimport java.time.ZoneId;%0Aimport java.time.ZonedDateTime;%0Aimport java.time.format.DateTimeFormatter;%0Aimport java.util.logging.Logger;%0A%0Aimport javax.swing.text.AbstractDocument.Content
+LocalDate;%0Aimport java.util.logging.Logger
 ;%0A%0Ai
@@ -791,16 +791,56 @@
 nlyTask;
+%0Aimport seedu.address.model.person.Task;
 %0A%0Apublic
@@ -1074,25 +1074,92 @@
 private 
-int index
+String formattedString;%0A    private int index;%0A    private ReadOnlyTask task
 ;%0A    pr
@@ -2773,16 +2773,42 @@
 ex + 1;%0A
+        this.task = task;%0A
         
@@ -3659,20 +3659,16 @@
 ;%0A    %7D%0A
-    
 %0A    @FX
@@ -3768,24 +3768,183 @@
 getValue();%0A
+        formattedString = DateTimeUtil.changeLocalDateToFormattedString(newDate);%0A        if (formattedString.compareTo(task.getDate().toString()) != 0) %7B%0A    
         most
@@ -4002,39 +4002,9 @@
 %22 + 
-DateTimeUtil.changeLocalDateToF
+f
 orma
@@ -4017,20 +4017,15 @@
 ring
-(newDate));%0A
+);%0A    
     
@@ -4088,32 +4088,36 @@
 oUser);%0A        
+ 
+   
 logger.info(%22Res
@@ -4151,32 +4151,42 @@
 eedbackToUser);%0A
+        %7D%0A
     %7D%0A%0A    priva

edit date successful without bug