package view;

import controller.Logic;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import model.TaskList;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.util.logging.Logger;
import java.util.logging.Level;

/**
 * Controller for the GUI
 * 
 * @author Wang Zhipeng
 *
 */

public class MainViewController extends VBox {
	
	private static final String MESSAGE_NO_TASK = "Good! All tasks are clear!";
	private static final String MESSAGE_TASKS_EXIST = "Oops! %s to be completed!";
	
	final static Logger logForMainViewController = Logger.getLogger(MainViewController.class.getName());
	
	final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	final Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>() {
		@Override
		public void handle(ActionEvent event) {
			final Calendar cal = Calendar.getInstance();
			date.setText(dateFormat.format(cal.getTime()));
		}
	}));
	
	
    @FXML 
    private TextField textField;
    
    @FXML
    private Label date;
    
    @FXML
    private VBox toBeCompleted;
    
    @FXML
    private VBox toDo;
    
    @FXML
    private Label popMessage;
    
	Label displayTaskList = new Label();
	TaskList taskList;
	int countTasks;
	String tasks;

    public MainViewController() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("MainView.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);

        try {
            fxmlLoader.load();
        } catch (IOException exception) {
        	logForMainViewController.log(Level.SEVERE, "Time out! Run time exception!");
            throw new RuntimeException(exception);
        }
        
        logForMainViewController.log(Level.INFO, "Load MainView.fxml successfully!");
        
        Logic.initialize();
        
        logForMainViewController.log(Level.INFO, "Initialize successfully!");
        
        toBeCompleted.getChildren().add(displayTaskList);
        
        setTaskListView();
		determinePopMessage();
    }

    public String getText() {
        return textProperty().get();
    }

    public void setText(String value) {
        textProperty().set(value);
    }
    
    public String getInput() {
    	return textField.getText();
    }
    
    public void display() {
    	
    	String input = getInput();
    	
    	if (input.equals("exit")) {
    		Stage stage = (Stage) getScene().getWindow();
    		stage.close();
    	} else {
    	
//    		String feedback = Logic.readAndExecuteCommands(input);
    		Logic.readAndExecuteCommands(input);
    		logForMainViewController.log(Level.INFO, "Execute command complete!");
    	
    		setTaskListView();
    		determinePopMessage();
    		
    		logForMainViewController.log(Level.INFO, "Set the view successfully!");
    		
    		Logic.saveTaskList();
    	}
    	
    }
    
    private void setTaskListView() {
    	taskList = Logic.getTaskList();
		countTasks = taskList.count();
		tasks = taskList.toString();
	
		displayTaskList.setText(tasks);
    }
    
    private void determinePopMessage() {
		if(tasks.equals("")) {
			popMessage.setText(MESSAGE_NO_TASK);
			
			logForMainViewController.log(Level.INFO, "No existing task!");	
		} else {
			popMessage.setText(String.format(MESSAGE_TASKS_EXIST, countTasks));
			
			logForMainViewController.log(Level.INFO, "Load existing task!");
		}
    }
    
    
    @FXML
    public void onEnter() {
    	display();
    	textField.setText("");
    }
    
    public String getDateLabel() {
    	return date.getText();
    }
    
    public void setDateLabel() {
    	timeline.setCycleCount(Animation.INDEFINITE);
    	timeline.play();
    }

    public StringProperty textProperty() {
        return textField.textProperty();
    }
    
}



@@ -179,24 +179,64 @@
 .Animation;%0A
+import javafx.animation.FadeTransition;%0A
 import javaf
@@ -2956,10 +2956,8 @@
   %09%0A
-//
     
@@ -3017,51 +3017,8 @@
 t);%0A
-    %09%09Logic.readAndExecuteCommands(input);%0A
     
@@ -3089,22 +3089,59 @@
 lete!%22);
+    %09%09
 %0A    %09
+%09
+%0A    %09%09fadeFeedback(feedback);
 %0A    %09%09s
@@ -3155,16 +3155,24 @@
 istView(
+feedback
 );%0A    %09
@@ -3330,33 +3330,205 @@
     %09%0A    %7D%0A
-    %0A    
+%0A%09private void setTaskListView() %7B%0A    %09taskList = Logic.getTaskList();%0A%09%09countTasks = taskList.count();%0A%09%09tasks = taskList.toString();%0A%09%0A%09%09displayTaskList.setText(tasks);%0A    %7D%0A%09%0A%09
 private void
@@ -3536,32 +3536,47 @@
 setTaskListView(
+String feedback
 ) %7B%0A    %09taskLis
@@ -3656,32 +3656,70 @@
 ist.toString();%0A
+%09%09%0A%09%09tasks = tasks + %22%5Cn%22 + feedback;%0A
 %09%0A%09%09displayTaskL
@@ -3799,24 +3799,23 @@
 %09if(
-tasks.equals(%22%22)
+countTasks == 0
 ) %7B%0A
@@ -4474,12 +4474,316 @@
  %7D%0A    %0A
+    public void fadeFeedback(String feedback) %7B%0A    %09popMessage.setText(feedback);%0A    %09%0A    %09FadeTransition ft = new FadeTransition(Duration.millis(3000), popMessage);%0A    %09ft.setFromValue(1.0);%0A    %09ft.setToValue(0.1);%0A    %09ft.setCycleCount(2);%0A    %09ft.setAutoReverse(true);%0A    %09ft.play();%0A    %7D%0A    %0A
 %7D%0A%0A%0A

Create fade effect for popping message; fix a bug for popping message. Close #71 and #72