package sbu.javafx;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
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 javafx.stage.Stage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class MusicController {

    // TODO: Define your UI components using the @FXML annotation.
    @FXML private ListView<String> lvSongs;
    @FXML private Label lblStatus;
    @FXML private Button btnPlayStop;
    @FXML private Button btnViewPlaylist;

    private MediaPlayer mediaPlayer;

    private ArrayList<String> playlist = new ArrayList<>();

    private String[] songsArray = {"Arcade                          Duncan Laurence           3.07",
                                   "How i love you             Engelbert                        4.20",
                                   "Memories                     Conan Gray                     4.09",
                                   "Night Changes             One Direction                 3.47",
                                   "Careless Whisper         George Michael              5.00"
    };

    @FXML public void initialize() { lvSongs.getItems().setAll(songsArray); }

    //TODO: Implement the logic to play a song. This method should update the UI to show which song is currently playing.

    public String songName(int a)
    {
        String songName = "";
        if (a == 0) songName = "Arcade";
        else if (a == 1) songName = "How i love you";
        else if (a == 2) songName = "Memmories";
        else if (a == 3) songName = "Night Changes";
        else songName = "Careless Whisper";

        return songName;
    }

    @FXML
    public void handlePlayAction()
    {
        String selectedSong = lvSongs.getSelectionModel().getSelectedItem();
        if (selectedSong == null)
        {
            lblStatus.setText("Please choose a song");
            return;
        }

        String filePath = "src/main/resources/sbu.javafx/songs/" + songName(lvSongs.getSelectionModel().getSelectedIndex()) + ".mp3";
        File file = new File(filePath);

        boolean isPlayingOrPaused = (mediaPlayer != null &&
                (mediaPlayer.getStatus() == MediaPlayer.Status.PLAYING ||
                        mediaPlayer.getStatus() == MediaPlayer.Status.PAUSED));

        if (isPlayingOrPaused)
        {
            if (mediaPlayer.getStatus() == MediaPlayer.Status.PLAYING)
            {
                mediaPlayer.pause();
                btnPlayStop.setText("Paused ⏸");
                lblStatus.setText("Playing : " + songName(lvSongs.getSelectionModel().getSelectedIndex()));
                return;
            }
            else if (mediaPlayer.getStatus() == MediaPlayer.Status.PAUSED)
            {
                mediaPlayer.play();
                btnPlayStop.setText("Play ▶");
                lblStatus.setText("Paused");
                return;
            }
        }

        if (mediaPlayer != null)
        {
            mediaPlayer.dispose();
            mediaPlayer = null;
        }

        try {
            Media media = new Media(file.toURI().toString());
            mediaPlayer = new MediaPlayer(media);

            mediaPlayer.setOnEndOfMedia(new Runnable() {
                @Override
                public void run() {
                    mediaPlayer.stop();
                    btnPlayStop.setText("Play ▶");
                }
            });

            mediaPlayer.play();
            btnPlayStop.setText("Paused ⏸");
            lblStatus.setText("Playing : " + songName(lvSongs.getSelectionModel().getSelectedIndex()));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //TODO: Logic to add a specific song to the user's playlist.

    public void addToPlaylist()
    {
        String selectedSong = lvSongs.getSelectionModel().getSelectedItem();
        if (selectedSong == null)
        {
            lblStatus.setText("Please choose a song");
            return;
        }

        if (!playlist.contains(selectedSong))
        {
            playlist.add(selectedSong);
            lblStatus.setText(songName(lvSongs.getSelectionModel().getSelectedIndex()) + " added to playList");
        }
        else lblStatus.setText(songName(lvSongs.getSelectionModel().getSelectedIndex()) + " already exists");
    }

    // TODO:This method should open a new window or change the current scene  to display the "Playlist" page.

    public void openPlaylistWindow()
    {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("Playlist-view.fxml"));
        Scene scene = null;
        try {
            scene = new Scene(loader.load());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        PlayListController controller = loader.getController();
        controller.setPlaylist(playlist);

        Stage stage = new Stage();
        stage.setTitle("Playlist");
        stage.setScene(scene);
        stage.show();
    }
}