package seedu.tasklist.ui;

import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import seedu.tasklist.model.task.ReadOnlyTask;

public class TaskCard extends UiPart{

    private static final String FXML = "TaskListCard.fxml";
    
    private static final String FX_BACKGROUND_PALE_GREEN = "-fx-background-color: #98FB98;";
    private static final String FX_BACKGROUND_PALE_YELLOW = "-fx-background-color: #FFFFCC;";
    private static final String FX_BACKGROUND_PALE_RED = "-fx-background-color: #ff9999;";
    private static final String FX_BACKGROUND_WHITE = "-fx-background-color: white;";
    
    private static final int NOT_REQUIRED = -1;
    
    @FXML
    private HBox cardPane;
    @FXML
    private Label name;
    @FXML
    private Label id;
    @FXML
    private Label startDateTime;
    @FXML
    private Label endDateTime;
    @FXML
    private Label description;
    @FXML
    private Label tags;

    private ReadOnlyTask task;
    private int displayedIndex;
    private boolean isNotRequired;

    public TaskCard() {

    }

    public static TaskCard load(ReadOnlyTask task, int displayedIndex){
        TaskCard card = new TaskCard();
        card.task = task;
        card.displayedIndex = displayedIndex;
        if (displayedIndex == NOT_REQUIRED) card.isNotRequired = true;
        return UiPartLoader.loadUiPart(card);
    }

    @FXML
    public void initialize() {
        initializeId();
        initializeTitle();
        initializeDateTime();
        initializeDescription();
        initializeTags();
        setStyle();
    }
    
    private void initializeId() {
        if (isNotRequired) {
            id.setText(null);
        } else {
            id.setText(displayedIndex + ". ");
        }
    }
    
    private void initializeTitle() {
        name.setText(task.getTitle().fullTitle);
    }
    
    private void initializeDescription() {
        if (task.getDescription().description.equals("") || isNotRequired) {
            description.setManaged(false);
        } else {
            description.setManaged(true);
            description.setText(task.getDescription().description);
        }
    }

    private void initializeDateTime() {
        if (task.getStartDateTime().toString().isEmpty() || isNotRequired) {
            startDateTime.setManaged(false);
        } else {
            startDateTime.setManaged(true);
            startDateTime.setText("Start:  " + task.getStartDateTime().toString().replaceAll(" ", "    Time:  "));
        }
        
        if(task.getEndDateTime().toString().isEmpty()){
            endDateTime.setManaged(false);
        } else {
            endDateTime.setManaged(true);
            endDateTime.setText("End:    " + task.getEndDateTime().toString().replaceAll(" ", "    Time:  "));
        }
    }
    
    private void initializeTags() {
        tags.setText(task.tagsString());
    }
    
    public void setStyle() {
        if (task.isCompleted()) {
            cardPane.setStyle(FX_BACKGROUND_PALE_GREEN);
        } else if (task.isFloating()) {
            cardPane.setStyle(FX_BACKGROUND_PALE_YELLOW);
        } else if (task.isOverdue()){
            cardPane.setStyle(FX_BACKGROUND_PALE_RED);
        } else {
            cardPane.setStyle(FX_BACKGROUND_WHITE);
        }
    }
    
    public HBox getLayout() {
        return cardPane;
    }

    @Override
    public void setNode(Node node) {
        cardPane = (HBox)node;
    }

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

@@ -1942,24 +1942,97 @@
 ription() %7B%0A
+        description.setText(task.getDescription().description);%0A        %0A
         if (
@@ -2210,124 +2210,176 @@
     
+%7D%0A
     
-description.setText(task.getDescription().description);%0A        %7D
+%7D%0A%0A    private void initializeDateTime() %7B%0A        startDateTime.setText(%22Start:  %22 + task.getStartDateTime().toString().replaceAll(%22 %22, %22    Time:  %22));
 %0A    
-%7D%0A%0A
     
-private void initializeDateTime() %7B
 %0A   
@@ -2431,24 +2431,25 @@
 sEmpty() %7C%7C 
+(
 isNotRequire
@@ -2441,32 +2441,55 @@
 %7C (isNotRequired
+ && task.isCompleted())
 ) %7B%0A            
@@ -2586,33 +2586,46 @@
 e);%0A        
-    start
+%7D%0A        %0A        end
 DateTime.set
@@ -2622,38 +2622,38 @@
 teTime.setText(%22
-Start:
+End:  
   %22 + task.getSt
@@ -2642,37 +2642,35 @@
     %22 + task.get
-Start
+End
 DateTime().toStr
@@ -2704,34 +2704,24 @@
 Time:  %22));%0A
-        %7D%0A
         %0A   
@@ -2770,16 +2770,57 @@
 sEmpty()
+ %7C%7C (isNotRequired && task.isCompleted())
 )%7B%0A     
@@ -2928,110 +2928,131 @@
     
+%7D%0A
     
-endDateTime.setText(%22End:    %22 + task.getEndDateTime().toString().replaceAll(%22 %22, %22    Time:  %22));
+%7D%0A    %0A    private void initializeTags() %7B%0A        tags.setText(task.tagsString());%0A        if (task.isCompleted()) %7B
 %0A   
@@ -3060,54 +3060,50 @@
     
-%7D%0A
     
-%7D
+tags.setManaged(false);
 %0A    
-%0A
     
-private void initializeTags()
+%7D else
  %7B%0A 
@@ -3105,32 +3105,36 @@
 e %7B%0A        
+  
+  
 tags.set
 Text(task.ta
@@ -3125,32 +3125,32 @@
 .set
-Text(task.tagsString());
+Managed(true);%0A        %7D
 %0A   

update to taskcard