package sbu.javafx;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;

import java.net.URL;

public class MusicController {

    @FXML
    private Button pausePlay;

    @FXML
    private ListView<Song> songListView;

    @FXML
    private ListView<Song> playlist;

    @FXML
    private Label nowPlayingLabel;

    private MediaPlayer mediaPlayer;
    private Song currentSong;
    private boolean isPlaying = false;

    @FXML
    public void initialize() {
        loadSongs();
    }

    // Adds songs to ListView
    private void loadSongs() {
        // Songs paths (used URL so the songs can stay in the resources folder so in case of building to jar the songs remain in the app)
        URL comeAsYouAre = getClass().getResource("/songs/comeAsYouAre.mp3");
        URL childIWillHurtYou = getClass().getResource("/songs/childIWillHurtYou.mp3");
        URL lucky = getClass().getResource("/songs/lucky.mp3");
        URL fitterHappier = getClass().getResource("/songs/fitterHappier.mp3");

        songListView.getItems().addAll(
            new Song("Come As You Are", "Nirvana", "03:38", comeAsYouAre),
            new Song("Child I Will Hurt You", "Crystal Castles", "03:33", childIWillHurtYou),
            new Song("Lucky", "Radiohead","04:18", lucky),
            new Song("Fitter Happier","Radiohead","01:57",fitterHappier)
        );
    }

    private void playSelectedSong(Song song) {
        try {
            if (mediaPlayer != null) {
                mediaPlayer.stop();
                mediaPlayer.dispose();
            }

            currentSong = song;

            mediaPlayer = new MediaPlayer(
                    new Media(song.getUrl().toString())
            );

            nowPlayingLabel.setText("Now Playing: " + song.getTitle() + " - " + song.getArtist());
            pausePlay.setText("Pause");
            isPlaying = true;
            mediaPlayer.play();

        } catch (Exception e) {
            nowPlayingLabel.setText("Error: Cannot play file - " + e.getMessage());
        }
    }

    @FXML
    public void togglePlayPause() {
        if (mediaPlayer == null) return;

        if (isPlaying) {
            mediaPlayer.pause();
            pausePlay.setText("Play");
            isPlaying = false;
        } else {
            mediaPlayer.play();
            isPlaying = true;
            pausePlay.setText("Pause");
        }
    }

    @FXML
    public void addCurrentToPlaylist() {
        if (currentSong != null) {
            boolean alreadyExists = false;
            for (Song song : playlist.getItems()) {
                if (song.getUrl().toString().equals(currentSong.getUrl().toString())) {
                    alreadyExists = true;
                    break;
                }
            }

            if (!alreadyExists) {
                playlist.getItems().add(currentSong);
                System.out.println("Added to playlist : " + currentSong.getTitle());
                nowPlayingLabel.setText("Now Playing : " + currentSong.getTitle() + " - Added to playlist!");
            } else {
                System.out.println("Song already in playlist");
                nowPlayingLabel.setText("Now Playing : " + currentSong.getTitle() + " - Already in playlist!");
            }
        }
    }

    @FXML
    public void clearPlaylist() {
        playlist.getItems().clear();
        System.out.println("Playlist cleared");
    }

    @FXML
    public void removeSelectedFromPlaylist() {
        Song selectedSong = playlist.getSelectionModel().getSelectedItem();
        if (selectedSong != null) {
            playlist.getItems().remove(selectedSong);
            System.out.println("Removed from playlist : " + selectedSong.getTitle());
        }
    }

    @FXML
    public void handleSongClick() {
        // Get the selected song from the song ListView
        Song selectedSong = songListView.getSelectionModel().getSelectedItem();
        if (selectedSong != null) {
            playSelectedSong(selectedSong);
        }
    }

    @FXML
    public void handleSongClickPlaylist(){
        // Get the selected song from the playlist ListView
        Song selectedSong = playlist.getSelectionModel().getSelectedItem();
        if (selectedSong != null) {
            playSelectedSong(selectedSong);
        }
    }
}