package sbu.javafx;

import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.stage.Stage;

public class PlaylistController
{

    @FXML
    private ListView<String> playlistListView;

    private ObservableList<String> playlist;
    private Scene previousScene;

    /**
     * Called from MusicController after loading Playlist-view.fxml
     */
    public void setPlaylist(ObservableList<String> playlist)
    {
        this.playlist = playlist;

        if (playlistListView != null)
        {
            playlistListView.setItems(playlist);
        }
    }

    /**
     * Save the previous scene so we can go back to it.
     */
    public void setPreviousScene(Scene previousScene)
    {
        this.previousScene = previousScene;
    }

    @FXML
    private void removeSelected(ActionEvent e)
    {
        if (playlistListView == null) return;

        String selected = playlistListView.getSelectionModel().getSelectedItem();
        if (selected == null) return;

        if (playlist != null)
        {
            playlist.remove(selected);
        } else
        {
            playlistListView.getItems().remove(selected);
        }
    }

    @FXML
    private void goBack(ActionEvent e)
    {
        if (playlistListView == null) return;

        Stage stage = (Stage) playlistListView.getScene().getWindow();
        if (previousScene != null)
        {
            stage.setScene(previousScene);
            stage.setTitle("Music Player");
            stage.show();
        }
    }
}