package org.madhatters.mediaplayer.controllers;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import org.madhatters.mediaplayer.media.AudioFile;
import org.madhatters.mediaplayer.models.Audio;

import java.util.Collection;
import java.util.stream.Collectors;

public class MainController {
    public Button previousButton;
    public Button pauseButton;
    public Button nextButton;
    public Label songInfoLabel;
    public Slider seekSlider;
    public Label songDurationLabel;
    public TextField searchField;
    @FXML private TableView<Audio> mediaTable;
    @FXML private TableColumn<Audio, String> filePathColumn;
    @FXML private TableColumn<Audio, String> artistColumn;
    @FXML private TableColumn<Audio, String> titleColumn;

    ObservableList<Audio> masterFiles = FXCollections.observableArrayList();

    public void initialize() {
        filePathColumn.setCellValueFactory(new PropertyValueFactory<>("path"));
        artistColumn.setCellValueFactory(new PropertyValueFactory<>("artist"));
        titleColumn.setCellValueFactory(new PropertyValueFactory<>("title"));

        // The predicate here displays all files
        FilteredList<Audio> filteredFiles = new FilteredList<>(masterFiles, mp3 -> true);

        searchField.textProperty().addListener((observable, oldValue, newValue) -> {
            filteredFiles.setPredicate(audio -> {
                if (newValue == null || newValue.isEmpty()) {
                    return true;
                }

                String lowercaseFilter = newValue.toLowerCase();
                String[] filterFields = new String[] {
                        audio.getTitle(), audio.getAlbum(), audio.getArtist(),
                };

                for (String field : filterFields) {
                    if (field == null) {
                        continue;
                    }
                    
                    if (field.toLowerCase().contains(lowercaseFilter)) {
                        return true;
                    }
                }

                return false;
            });
        });

        SortedList<Audio> sortedData = new SortedList<>(filteredFiles);
        sortedData.comparatorProperty().bind(mediaTable.comparatorProperty());

        mediaTable.setItems(sortedData);
    }

    public void setFiles(Collection<AudioFile> audioFiles) {
        masterFiles.setAll(audioFiles
                .stream()
                .map(f -> new Audio(f.getFilePath(), f.getArtistName(), f.getSongTitle(), f.getAlbum()))
                .collect(Collectors.toList())
        );
    }
}

@@ -443,16 +443,137 @@
 .Audio;%0A
+import javafx.scene.media.Media;%0Aimport javafx.scene.media.MediaPlayer;%0A%0Aimport java.io.File;%0Aimport java.net.URLEncoder;
 %0Aimport 
@@ -1114,16 +1114,53 @@
 olumn;%0A%0A
+    MediaPlayer currentMediaPlayer;%0A%0A
     Obse
@@ -1256,24 +1256,811 @@
 tialize() %7B%0A
+        mediaTable.setOnMousePressed(e -%3E %7B%0A            if (e.isPrimaryButtonDown() && e.getClickCount() == 2) %7B%0A                Audio selectedAudioFile = mediaTable.getSelectionModel().getSelectedItem();%0A%0A                if (currentMediaPlayer != null) %7B%0A                    currentMediaPlayer.stop();%0A                %7D%0A%0A                try %7B%0A                    Media newMedia = new Media(new File(selectedAudioFile.getPath()).toURI().toString());%0A                    currentMediaPlayer = new MediaPlayer(newMedia);%0A                    currentMediaPlayer.play();%0A                %7D catch (Exception ex) %7B%0A                    System.err.println(ex.getMessage());%0A                %7D%0A            %7D%0A        %7D);%0A%0A        initializeTableView();%0A    %7D%0A%0A    private void initializeTableView() %7B%0A
         file
@@ -3043,36 +3043,16 @@
       %7D%0A
-                    
 %0A       

Add playing music