package tars.ui;

import com.google.common.eventbus.Subscribe;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import org.controlsfx.control.StatusBar;

import tars.commons.core.LogsCenter;
import tars.commons.events.model.TarsChangedEvent;
import tars.commons.events.storage.TarsStorageDirectoryChangedEvent;
import tars.commons.util.FxViewUtil;

import java.util.Date;
import java.util.logging.Logger;

/**
 * A ui for the status bar that is displayed at the footer of the application.
 */
public class StatusBarFooter extends UiPart {
    private static final Logger logger =
            LogsCenter.getLogger(StatusBarFooter.class);
    private StatusBar syncStatus;
    private StatusBar saveLocationStatus;

    private GridPane mainPane;

    @FXML
    private AnchorPane saveLocStatusBarPane;

    @FXML
    private AnchorPane syncStatusBarPane;

    private AnchorPane placeHolder;

    private Text locationText = new Text();



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

    public static StatusBarFooter load(Stage stage, AnchorPane placeHolder,
            String saveLocation) {
        StatusBarFooter statusBarFooter = UiPartLoader.loadUiPart(stage,
                placeHolder, new StatusBarFooter());
        statusBarFooter.configure(saveLocation);
        return statusBarFooter;
    }

    public void configure(String saveLocation) {
        addMainPane();
        addSyncStatus();
        setSyncStatus("Not updated yet in this session");
        addSaveLocation();
        setSaveLocation("Storage Directory: ./" + saveLocation);
        registerAsAnEventHandler(this);
    }

    private void addMainPane() {
        FxViewUtil.applyAnchorBoundaryParameters(mainPane, 0.0, 0.0, 0.0, 0.0);
        placeHolder.getChildren().add(mainPane);
    }

    private void setSaveLocation(String location) {
        locationText.setText(location);
        locationText.setFill(Color.WHITE);
        this.saveLocationStatus.setText("");


    }

    private void addSaveLocation() {
        this.saveLocationStatus = new StatusBar();
        this.saveLocationStatus.getRightItems().add(locationText);
        FxViewUtil.applyAnchorBoundaryParameters(saveLocationStatus, 0.0, 0.0,
                0.0, 0.0);
        saveLocStatusBarPane.getChildren().add(saveLocationStatus);
    }

    private void setSyncStatus(String status) {
        this.syncStatus.setText(status);
    }

    private void addSyncStatus() {
        this.syncStatus = new StatusBar();
        FxViewUtil.applyAnchorBoundaryParameters(syncStatus, 0.0, 0.0, 0.0,
                0.0);
        syncStatusBarPane.getChildren().add(syncStatus);
    }

    @Override
    public void setNode(Node node) {
        mainPane = (GridPane) node;
    }

    @Override
    public void setPlaceholder(AnchorPane placeholder) {
        this.placeHolder = placeholder;
    }

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

    @Subscribe
    public void handleTarsChangedEvent(TarsChangedEvent event) {
        String lastUpdated = (new Date()).toString();
        logger.info(LogsCenter.getEventHandlingLogMessage(event,
                "Setting last updated status to " + lastUpdated));
        setSyncStatus("Last Updated: " + lastUpdated);
    }

    // @@author A0124333U
    @Subscribe
    private void handleTarsStorageChangeDirectoryEvent(
            TarsStorageDirectoryChangedEvent event) {
        logger.info(LogsCenter.getEventHandlingLogMessage(event));
        setSaveLocation(
                "Storage Location Changed: " + event.getNewFilePath());
    }
}

@@ -11,16 +11,115 @@
 rs.ui;%0A%0A
+import java.util.Date;%0Aimport java.util.logging.Logger;%0A%0Aimport org.controlsfx.control.StatusBar;%0A%0A
 import c
@@ -155,16 +155,17 @@
 scribe;%0A
+%0A
 import j
@@ -207,16 +207,51 @@
 e.Node;%0A
+import javafx.scene.control.Label;%0A
 import j
@@ -337,125 +337,19 @@
 fx.s
-cene.paint.Color;%0Aimport javafx.scene.text.Text;%0Aimport javafx.stage.Stage;%0Aimport org.controlsfx.control.StatusBar;%0A
+tage.Stage;
 %0Aimp
@@ -543,63 +543,43 @@
 il;%0A
-%0A
 import 
-java.util.Date;%0Aimport java.util.logging.Logger
+tars.commons.util.StringUtil
 ;%0A%0A/
@@ -1079,41 +1079,67 @@
 ate 
-Text locationText = new Text();%0A%0A
+Label saveLocationLabel;%0A    private Label syncStatusLabel;
 %0A%0A  
@@ -1755,10 +1755,8 @@
 ry: 
-./
 %22 + 
@@ -1978,32 +1978,58 @@
 inPane);%0A    %7D%0A%0A
+    // @@author A0139924W%0A
     private void
@@ -2068,36 +2068,46 @@
 ) %7B%0A        
-l
+this.saveL
 ocation
-Text
+Label
 .setText(loc
@@ -2122,46 +2122,47 @@
     
+%7D%0A%0A
     
-locationText.setFill(Color.WHITE);
+private void addSaveLocation() %7B
 %0A   
@@ -2193,116 +2193,137 @@
 atus
-.setText(%22%22
+ = new StatusBar(
 );%0A
-%0A%0A
     
-%7D%0A%0A
     
-private void addSaveLocation() %7B%0A        this.saveLocationStatus = new StatusBar(
+this.saveLocationLabel = new Label();%0A        this.saveLocationStatus.setText(StringUtil.EMPTY_STRING
 );%0A 
@@ -2377,20 +2377,25 @@
 add(
-l
+saveL
 ocation
-Text
+Label
 );%0A 
@@ -2641,24 +2641,29 @@
 s.syncStatus
+Label
 .setText(sta
@@ -2745,32 +2745,195 @@
 ew StatusBar();%0A
+        this.syncStatusLabel = new Label();%0A        this.syncStatus.setText(StringUtil.EMPTY_STRING);%0A        this.syncStatus.getLeftItems().add(syncStatusLabel);%0A
         FxViewUt
@@ -3072,24 +3072,44 @@
 atus);%0A    %7D
+%0A    %0A    //@@author
 %0A%0A    @Overr

Update status bar footer