package sbu.javafx;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;

import java.io.File;

public class MusicController {

    // TODO: Define your UI components using the @FXML annotation.
    @FXML private Label nowPlayingLabel;
    private Button currentlyPlayingButton = null;
    private ObservableList<String> playlistItems = FXCollections.observableArrayList();
    private ListView<String> playlistView = new ListView<>(playlistItems);
    private Stage playlistStage = null;
    private MediaPlayer mediaPlayer = null;
    //TODO: Implement the logic to play a song. This method should update the UI to show which song is currently playing.

    @FXML
    public void handlePlayAction(ActionEvent event) {
        //Update labels, change button icons, etc.
        Button clickedButton = (Button) event.getSource();
        if(clickedButton == currentlyPlayingButton)
        {
            currentlyPlayingButton.setText("Play");
            clickedButton.setStyle("-fx-background-color: #3498db; -fx-text-fill: white; -fx-background-radius: 3;"); // برگشت به رنگ آبی
            nowPlayingLabel.setText("-");
            currentlyPlayingButton = null;
            if (mediaPlayer != null) {
                mediaPlayer.stop();
            }
        }
        else
        {
            if (currentlyPlayingButton != null)
            {
                currentlyPlayingButton.setText("Play");
                currentlyPlayingButton.setStyle("-fx-background-color: #3498db; -fx-text-fill: white; -fx-background-radius: 3;");
                if (mediaPlayer != null) {
                    mediaPlayer.stop();
                }
            }

            clickedButton.setText("Stop");
            clickedButton.setStyle("-fx-background-color: #e74c3c; -fx-text-fill: white; -fx-background-radius: 3;");
            currentlyPlayingButton = clickedButton;
            HBox hBox = (HBox) clickedButton.getParent();
            String title = ((Label)hBox.getChildren().getFirst()).getText();
            nowPlayingLabel.setText(title);
            try {
                String fileURI = getMusicFilePath(title); // گرفتن آدرس استاندارد فایل
                Media media = new Media(fileURI);            // گذاشتن نوار کاست
                mediaPlayer = new MediaPlayer(media);        // قراردادن در دستگاه پخش
                mediaPlayer.play();                          // 🚀 شروع پخش!
            } catch (Exception e) {
                System.out.println("Error playing file: " + e.getMessage());
                // اگر فایلی پیدا نشد یا خطایی رخ داد، این‌جا پرینت می‌شود تا برنامه کرش نکند
            }
        }

    }

    //TODO: Logic to add a specific song to the user's playlist.
    @FXML
    public void addToPlaylist(ActionEvent event) {
        //Add the song to a list or update a ListView.
        Button clickedButton = (Button) event.getSource();
        HBox row = (HBox) clickedButton.getParent();
        Label songTitleLabel = (Label) row.getChildren().getFirst();
        String songName = songTitleLabel.getText();
        if(!playlistItems.contains(songName))
        {
            playlistItems.add(songName);
        }
    }

    // TODO:This method should open a new window or change the current scene  to display the "Playlist" page.
    @FXML
    public void openPlaylistWindow() {
        //Create a new stage and show the playlist UI.
        if (playlistStage != null && playlistStage.isShowing())
        {
            playlistStage.toFront();
            return;
        }
        playlistStage = new Stage();
        playlistStage.setTitle("My Playlist");
        playlistView.setStyle(
                "-fx-background-color: #ffffff; " +
                        "-fx-background-radius: 5; " +
                        "-fx-border-color: #dcdde1; " +
                        "-fx-border-width: 1; " +
                        "-fx-font-size: 14px; " +
                        "-fx-padding: 5;"
        );
        VBox root = new VBox(playlistView);
        root.setStyle("-fx-background-color: #f5f5f5; -fx-padding: 15;");
        VBox.setVgrow(playlistView, javafx.scene.layout.Priority.ALWAYS);
        Scene scene = new Scene(root, 300, 400);
        playlistStage.setScene(scene);
        playlistStage.show();
    }

    private String getMusicFilePath(String songName) {
        String fileName = switch (songName) {
            case "Jana" -> "src\\main\\resources\\music\\Jana.mp3";
            case "Jadoo" -> "src\\main\\resources\\music\\@RapRelease - Jadoo - Pidar.mp3";
            case "we never dated" -> "src\\main\\resources\\music\\we never dated.mp3";
            default -> "";
        };
        File file = new File(fileName);
        return file.toURI().toString();
    }

}
