package seedu.taskmanager.ui;

import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import seedu.taskmanager.model.item.ReadOnlyItem;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.ocpsoft.prettytime.PrettyTime;

public class ItemCard extends UiPart{

    private static final String FXML = "ItemListCard.fxml";

    @FXML
    private HBox cardPane;
    @FXML
    private Label name;
    @FXML
    private Label id;
    @FXML
    private Label itemType;
    @FXML
    private Label endTime;
    @FXML
    private Label endDate;
    @FXML
    private Label endFromNow;
    @FXML
    private Label startTime;
    @FXML
    private Label startDate;
    @FXML
    private Label tags;

    private ReadOnlyItem item;
    private int displayedIndex;

    public ItemCard(){

    }

    public static ItemCard load(ReadOnlyItem item, int displayedIndex){
        ItemCard card = new ItemCard();
        card.item = item;
        card.displayedIndex = displayedIndex;
        return UiPartLoader.loadUiPart(card);
    }

    @FXML
    public void initialize() {
        name.setText(item.getName().value);
        id.setText(displayedIndex + ". ");
        itemType.setText(item.getItemType().value);
        endTime.setText(item.getEndTime().value);
        endDate.setText(item.getEndDate().value);
        String endDateString = item.getEndDate().value + " " + item.getEndTime().value;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm"); 
        Date endFromNowDate;
        Date currentDate = new Date();
        String endFromNowText = "";
        try {
            endFromNowDate = df.parse(endDateString);
            PrettyTime p = new PrettyTime();
            endFromNowText = p.format(endFromNowDate);
            if (currentDate.before(endFromNowDate)) { // Future Deadline
                endFromNow.setText("Ends " + endFromNowText);
            } else { // Past Deadline
            	endFromNow.setText("Ended " + endFromNowText);
            }
        } catch (ParseException e) {
            endFromNow.setText(endFromNowText);
            e.printStackTrace();
        }
        startTime.setText(item.getStartTime().value);
        startDate.setText(item.getStartDate().value);
        tags.setText(item.tagsString());
    }

    public HBox getLayout() {
        return cardPane;
    }

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

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

@@ -143,16 +143,62 @@
 t.HBox;%0A
+import seedu.taskmanager.model.item.ItemType;%0A
 import s
@@ -1743,16 +1743,147 @@
 t = %22%22;%0A
+        if (item.getItemType().value.equals(ItemType.DEADLINE_WORD) %7C%7C item.getItemType().value.equals(ItemType.EVENT_WORD)) %7B%0A    
         
@@ -1888,16 +1888,20 @@
   try %7B%0A
+    
         
@@ -1958,16 +1958,20 @@
         
+    
 PrettyTi
@@ -1999,32 +1999,36 @@
 ();%0A            
+   
+ 
 endFromNowText =
@@ -2050,24 +2050,28 @@
 omNowDate);%0A
+    
             
@@ -2139,32 +2139,36 @@
                 
+   
+ 
 endFromNow.setTe
@@ -2209,16 +2209,20 @@
         
+    
 %7D else %7B
@@ -2252,16 +2252,20 @@
        %09
+    
 endFromN
@@ -2315,26 +2315,33 @@
         
-%7D%0A
+    %7D%0A   
         
 %7D catch 
@@ -2332,16 +2332,17 @@
         
+ 
 %7D catch 
@@ -2358,24 +2358,28 @@
 eption e) %7B%0A
+    
             
@@ -2426,16 +2426,20 @@
         
+   
+ 
 e.printS
@@ -2447,24 +2447,100 @@
 ackTrace();%0A
+            %7D%0A        %7D else %7B%0A        %09endFromNow.setText(endFromNowText);%0A
         %7D%0A  

fix date parsing bug for task