/** @@author A0142130A **/
package seedu.taskell.ui;

import javafx.scene.Node;
import javafx.scene.control.TextArea;
import javafx.scene.layout.AnchorPane;
import jfxtras.scene.control.agenda.Agenda;
import seedu.taskell.commons.core.LogsCenter;
import seedu.taskell.commons.util.FxViewUtil;
import seedu.taskell.logic.commands.AddCommand;
import seedu.taskell.logic.commands.HelpCommand;
import seedu.taskell.logic.commands.ViewCalendarCommand;
import seedu.taskell.logic.commands.ViewHistoryCommand;

import java.util.ArrayList;
import java.util.logging.Logger;

/**
 * The Display Panel of the App.
 */
public class DisplayPanel extends UiPart {

    public static final String MESSAGE_NO_HISTORY = "No commands available for undo.";
    public static final String MESSAGE_DISPLAY_HISTORY = "List of command history available for undo:\n";

    private static final String WELCOME_MESSAGE = "Welcome to Taskell!\n"
            + "Enter '" + AddCommand.COMMAND_WORD + "' in command box to add a task.\n"
            + "Enter '" + ViewHistoryCommand.COMMAND_WORD_1 + "' for a list of commands to undo.\n"
            + "Enter '" + ViewCalendarCommand.COMMAND_WORD_1 + "' to view calendar.\n"
            + "Enter '" + HelpCommand.COMMAND_WORD + "' for more information about commands.";

    private static Logger logger = LogsCenter.getLogger(DisplayPanel.class);
    
    public static final String DISPLAY_PANEL_ID = "displayPanel";
    private static final String STATUS_BAR_STYLE_SHEET = "result-display";
    
    private TextArea display;
    private CalendarView calendarView;

    /**
     * Constructor is kept private as {@link #load(AnchorPane)} is the only way to create a DisplayPanel.
     */
    private DisplayPanel() {
        calendarView = new CalendarView();
        display = new TextArea();
    }

    @Override
    public void setNode(Node node) {
        //not applicable
    }

    @Override
    public String getFxmlPath() {
        return null; //not applicable
    }

    /**
     * This method should be called after the FX runtime is initialized and in FX application thread.
     * @param placeholder The AnchorPane where the DisplayPanel must be inserted
     */
    public static DisplayPanel load(AnchorPane placeholder){
        logger.info("Initializing display panel");
        DisplayPanel displayPanel = new DisplayPanel();
        
        displayPanel.display.setEditable(false);
        displayPanel.display.setId(DISPLAY_PANEL_ID);
        displayPanel.display.getStyleClass().removeAll();
        displayPanel.display.getStyleClass().add(STATUS_BAR_STYLE_SHEET);
        
        FxViewUtil.applyAnchorBoundaryParameters(displayPanel.display, 0.0, 0.0, 0.0, 0.0);
        placeholder.getChildren().add(displayPanel.display);
        
        displayPanel.display.setText(WELCOME_MESSAGE);
       
        return displayPanel;
    }
    
    public void loadList(AnchorPane placeholder, ArrayList<String> list) {
        placeholder.getChildren().clear();
        placeholder.getChildren().add(display);
        
        display.setText("");
        if (list.isEmpty()) {
            display.setText(MESSAGE_NO_HISTORY);
        }
        else {
            display.setText(MESSAGE_DISPLAY_HISTORY);
            for (int i=0; i<list.size(); i++) {
                int index = i+1;
                display.appendText(index + ". " + list.get(i) + "\n");
            }
        }
    }
    
    public void loadCalendar(AnchorPane placeholder) {
        if (placeholder.getChildren().contains(calendarView.getAgenda())) {
            calendarView.getAgenda().refresh();
            return;
        }
        
        placeholder.getChildren().clear();
        
        Agenda agenda = calendarView.getAgenda();
        
        FxViewUtil.applyAnchorBoundaryParameters(agenda, 0.0, 0.0, 0.0, 0.0);
        
        placeholder.getChildren().add(agenda);
    }
    
    /** @@author **/

}

@@ -1539,16 +1539,24 @@
  display
+TextArea
 ;%0A    pr
@@ -1798,16 +1798,24 @@
  display
+TextArea
  = new T
@@ -2309,16 +2309,24 @@
  display
+TextArea
  panel%22)
@@ -2416,24 +2416,32 @@
 anel.display
+TextArea
 .setEditable
@@ -2473,24 +2473,32 @@
 anel.display
+TextArea
 .setId(DISPL
@@ -2531,32 +2531,40 @@
 layPanel.display
+TextArea
 .getStyleClass()
@@ -2605,16 +2605,24 @@
 .display
+TextArea
 .getStyl
@@ -2737,16 +2737,24 @@
 .display
+TextArea
 , 0.0, 0
@@ -2822,24 +2822,32 @@
 anel.display
+TextArea
 );%0A        %0A
@@ -2870,24 +2870,32 @@
 anel.display
+TextArea
 .setText(WEL
@@ -3120,16 +3120,24 @@
 (display
+TextArea
 );%0A     
@@ -3147,32 +3147,40 @@
 %0A        display
+TextArea
 .setText(%22%22);%0A  
@@ -3218,32 +3218,40 @@
          display
+TextArea
 .setText(MESSAGE
@@ -3308,16 +3308,24 @@
  display
+TextArea
 .setText
@@ -3455,16 +3455,24 @@
  display
+TextArea
 .appendT
@@ -3605,162 +3605,8 @@
 ) %7B%0A
-        if (placeholder.getChildren().contains(calendarView.getAgenda())) %7B%0A            calendarView.getAgenda().refresh();%0A            return;%0A        %7D%0A
     

refactor display to more clear name removed logic block