package sbu.javafx;

import javafx.fxml.FXML;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;


import javax.imageio.IIOException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MusicController {

    @FXML private Label nowPlayingSong;
    @FXML private Button playBtn1;
    @FXML private Button playBtn2;
    @FXML private Button playBtn3;
    @FXML private Button playBtn4;
    @FXML private Button add1;
    @FXML private Button add2;
    @FXML private Button add3;
    @FXML private Button add4;

    private List<Song> playlist = new ArrayList<>();
    private Song currentSong = null;
    private MediaPlayer mediaPlayer;
    private Song song1 = new Song("The Comfort of a Laugh Track", "Roar", "04:52", "songs/song1.mp3");
    private Song song2 = new Song("Perfect Day", "Lou Reed", "03:44", "songs/song2.mp3");
    private Song song3 = new Song("Wish You Were Here", "Pink Floyd", "05:34", "songs/song3.mp3");
    private Song song4 = new Song("No Surprises", "Radiohead", "03:49", "songs/song4.mp3");


    @FXML
    public void handlePlayAction(ActionEvent event) {

        Button clickedBotton = (Button) event.getSource();
        resetPlayButtons();
        String buttonId = clickedBotton.getId();
        switch (buttonId)
        {
            case "playBtn1":
                playSong(song1);
                playBtn1.setText("Playing");
                playBtn1.setStyle("-fx-pref-width: 120; -fx-font-size: 20px; -fx-background-color: lightBlue;");
                break;

            case "playBtn2":
                playSong(song2);
                playBtn2.setText("Playing");
                playBtn2.setStyle("-fx-pref-width: 120; -fx-font-size: 20px; -fx-background-color: lightBlue;");
                break;

            case "playBtn3":
                playSong(song3);
                playBtn3.setText("Playing");
                playBtn3.setStyle("-fx-pref-width: 120; -fx-font-size: 20px; -fx-background-color: lightBlue;");
                break;

            case "playBtn4":
                playSong(song4);
                playBtn4.setText("Playing");
                playBtn4.setStyle("-fx-pref-width: 120; -fx-font-size: 20px; -fx-background-color: lightBlue;");
                break;
        }


    }

    @FXML
    public void addToPlaylist(ActionEvent event) {

        Button clickedButton = (Button) event.getSource();
        String btnId = clickedButton.getId();
        Song selectedSong = null;

        switch (btnId)
        {
            case "add1":
                selectedSong = song1;
                break;
            case "add2":
                selectedSong = song2;
                break;
            case "add3":
                selectedSong = song3;
                break;
            case "add4":
                selectedSong = song4;
                break;
        }
        if (selectedSong != null)
        {
            playlist.add(selectedSong);
        }
    }

    @FXML
    public void openPlaylistWindow() {
        try
        {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("playlist.fxml"));
            Parent root = loader.load();

            PlaylistController playlistController = loader.getController();
            playlistController.setPlaylist(playlist, this);

            Stage playlistStage = new Stage();
            playlistStage.setTitle("Playlist");
            playlistStage.setScene(new Scene(root, 500, 400));
            playlistStage.show();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }




    public void playSong(Song song) {
        if (mediaPlayer != null)
        {
            mediaPlayer.stop();
            mediaPlayer.dispose();
        }

        try {
            File musicFile = new File(song.getFilePath());
            if (!musicFile.exists()) {
                nowPlayingSong.setText("Error: File not found - " + song.getFilePath());
                System.out.println("Not found: " + musicFile.getAbsolutePath());
                return;
            }
            Media media = new Media(musicFile.toURI().toString());
            mediaPlayer = new MediaPlayer(media);
            mediaPlayer.setOnEndOfMedia(new Runnable() {
                @Override
                public void run() {
                    resetPlayButtons();
                    nowPlayingSong.setStyle("-fx-font-weight: bold; -fx-font-size: 20px;");
                    nowPlayingSong.setText("-");
                    if (mediaPlayer != null)
                    {
                        mediaPlayer.dispose();
                        mediaPlayer = null;
                    }
                }
            });
            mediaPlayer.play();
            currentSong = song;
            nowPlayingSong.setStyle("-fx-font-size: 20px;");
            nowPlayingSong.setText(song.getTitle() + " - " + song.getArtist());
        } catch (Exception e) {
            nowPlayingSong.setText("Playback error");
            e.printStackTrace();
        }
    }

    public void resetPlayButtons()
    {
        playBtn1.setText("Play");
        playBtn1.setStyle("-fx-pref-width: 120; -fx-font-size: 20px;");
        playBtn2.setText("Play");
        playBtn2.setStyle("-fx-pref-width: 120; -fx-font-size: 20px;");
        playBtn3.setText("Play");
        playBtn3.setStyle("-fx-pref-width: 120; -fx-font-size: 20px;");
        playBtn4.setText("Play");
        playBtn4.setStyle("-fx-pref-width: 120; -fx-font-size: 20px;");
    }

}

