package sbu.javafx;

import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MusicController
{
    @FXML private VBox rootPane;
    @FXML private Label titleLabel;
    @FXML private Label nowPlayingLabel;

    @FXML private Button playBtn1;
    @FXML private Button playBtn2;
    @FXML private Button playBtn3;

    private final List<String> playlist = new ArrayList<>();
    private MediaPlayer mediaPlayer;
    private boolean darkTheme = false;

    private final String song1 = "Careless Whisper";
    private final String song2 = "Adore You";
    private final String song3 = "Wildflower";

    private final String song1File = "/sbu/javafx/audio/careless_whisper.mp3";
    private final String song2File = "/sbu/javafx/audio/adore_you.mp3";
    private final String song3File = "/sbu/javafx/audio/wildflower.mp3";

    @FXML
    private void initialize() {updateNowPlaying("None");applyLightTheme();}

    @FXML
    public void handlePlay1() {playSong(song1, song1File, playBtn1);}

    @FXML
    public void handlePlay2() {playSong(song2, song2File, playBtn2);}

    @FXML
    public void handlePlay3() {playSong(song3, song3File, playBtn3);}

    @FXML
    public void handleAdd1() {addToPlaylist(song1);}

    @FXML
    public void handleAdd2() {addToPlaylist(song2);}

    @FXML
    public void handleAdd3() {addToPlaylist(song3);}

    @FXML
    public void handleViewPlaylist()
    {
        Stage stage = new Stage();

        VBox root = new VBox(10);
        root.setStyle("-fx-padding: 16; -fx-background-color: white;");

        Label title = new Label("My Playlist");
        title.setStyle("-fx-font-size: 18px; -fx-font-weight: bold;");

        ListView<String> listView = new ListView<>();
        listView.getItems().addAll(playlist);

        root.getChildren().addAll(title, listView);

        Scene scene = new Scene(root, 300, 400);
        stage.setScene(scene);
        stage.setTitle("Playlist");
        stage.show();
    }

    @FXML
    public void handleToggleTheme()
    {
        darkTheme = !darkTheme;

        if (darkTheme) {applyDarkTheme();}
        else {applyLightTheme();}
    }

    private void playSong(String songName, String resourcePath, Button playButton)
    {
        try
        {
            if (mediaPlayer != null)
            {
                mediaPlayer.stop();
                mediaPlayer.dispose();
            }

            URL url = getClass().getResource(resourcePath);
            if (url == null)
            {
                updateNowPlaying(songName + " (file not found)");
                return;
            }

            Media media = new Media(url.toExternalForm());
            mediaPlayer = new MediaPlayer(media);
            mediaPlayer.play();

            updateNowPlaying(songName);

            if (playBtn1 != null) playBtn1.setText("Play");
            if (playBtn2 != null) playBtn2.setText("Play");
            if (playBtn3 != null) playBtn3.setText("Play");

            if (playButton != null) {playButton.setText("Playing");}

        }
        catch (Exception e)
        {
            updateNowPlaying(songName + " (error)");
            e.printStackTrace();
        }
    }

    private void addToPlaylist(String songName)
    {
        if (!playlist.contains(songName)) {playlist.add(songName);}
    }

    private void updateNowPlaying(String text)
    {
        if (nowPlayingLabel != null)
        {
            nowPlayingLabel.setText("Now Playing: " + text);
        }
    }

    private void applyDarkTheme()
    {
        if (rootPane != null)
        {
            rootPane.setStyle("-fx-padding: 24;" + "-fx-background-color: #121212;");
        }

        if (titleLabel != null)
        {
            titleLabel.setStyle("-fx-font-size: 26px;" + "-fx-font-weight: bold;" + "-fx-text-fill: white;");
        }

        if (nowPlayingLabel != null)
        {
            nowPlayingLabel.setStyle("-fx-padding: 12;" + "-fx-background-color: #1E1E1E;" + "-fx-background-radius: 10;" + "-fx-border-color: #444444;" + "-fx-border-radius: 10;" + "-fx-text-fill: white;");
        }
    }

    private void applyLightTheme()
    {
        if (rootPane != null)
        {
            rootPane.setStyle("-fx-padding: 24;" + "-fx-background-color: #F8F8F8;");
        }

        if (titleLabel != null)
        {
            titleLabel.setStyle("-fx-font-size: 26px;" + "-fx-font-weight: bold;" + "-fx-text-fill: #222222;");
        }

        if (nowPlayingLabel != null)
        {
            nowPlayingLabel.setStyle("-fx-padding: 12;" + "-fx-background-color: white;" + "-fx-background-radius: 10;" + "-fx-border-color: rgba(0,0,0,0.15);" + "-fx-border-radius: 10;" + "-fx-text-fill: black;");
        }
    }
}