package seedu.jimi.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.stage.Stage;
import seedu.jimi.commons.core.LogsCenter;
import seedu.jimi.commons.events.model.AddressBookChangedEvent;
import seedu.jimi.commons.util.FxViewUtil;

import org.controlsfx.control.StatusBar;

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 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("./" + 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) {
        this.saveLocationStatus.setText(location);
    }

    private void addSaveLocation() {
        this.saveLocationStatus = new StatusBar();
        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 handleAddressBookChangedEvent(AddressBookChangedEvent abce) {
        String lastUpdated = (new Date()).toString();
        logger.info(LogsCenter.getEventHandlingLogMessage(abce, "Setting last updated status to " + lastUpdated));
        setSyncStatus("Last Updated: " + lastUpdated);
    }
}

@@ -216,16 +216,55 @@
 .Stage;%0A
+import seedu.jimi.commons.core.Config;%0A
 import s
@@ -362,16 +362,187 @@
 dEvent;%0A
+import seedu.jimi.commons.events.storage.StoragePathChangedEvent;%0Aimport seedu.jimi.commons.exceptions.DataConversionException;%0Aimport seedu.jimi.commons.util.ConfigUtil;%0A
 import s
@@ -3248,11 +3248,669 @@
 );%0A    %7D
+%0A%0A    @Subscribe%0A    public void handleStoragePathChangedEvent(StoragePathChangedEvent spce) %7B%0A        try %7B%0A            String configFilePathUsed = Config.DEFAULT_CONFIG_FILE;%0A            Config config = ConfigUtil.readConfig(configFilePathUsed).orElse(new Config());%0A            setSaveLocation(config.getTaskBookFilePath());%0A        %7D catch (DataConversionException e) %7B%0A            logger.warning(%22Config file at %22 + Config.DEFAULT_CONFIG_FILE + %22 is not in the correct format. %22%0A                    + %22Using default config properties%22);%0A            Config config = new Config();%0A            setSaveLocation(config.getTaskBookFilePath());%0A        %7D%0A    %7D
 %0A%7D%0A

Modify Status Bar Footer to update itself whenever Storage Path is Changed