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.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.fxml.FXML;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.control.ListView;
import javafx.stage.Stage;
import javafx.scene.control.Slider;
import javafx.scene.control.Label;
import javafx.util.Duration;
import javafx.scene.control.ToggleButton;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Objects;


public class MusicController {

    ArrayList<String> playls = new ArrayList<>() ;
    @FXML
    private ListView<String> playlistview ;
    @FXML
    private Label nowPlayingLabel ;
    @FXML
    private Slider timeSlider ;
    @FXML
    private Label timeLabel ;
    @FXML
    private Slider volumeSlider ;
    @FXML
    private ToggleButton themeButton ;
    @FXML
    public void toggleTheme() {

        Scene scene = themeButton.getScene();

        scene.getStylesheets().clear();

        if(themeButton.isSelected()) {

            scene.getStylesheets().add(
                    getClass()
                            .getResource("dark-theme.css")
                            .toExternalForm()
            );

            themeButton.setText("Dark Mode");
        }
        else {

            scene.getStylesheets().add(
                    getClass()
                            .getResource("light-theme.css")
                            .toExternalForm()
            );

            themeButton.setText("Light Mode");
        }
    }

    // TODO: Define your UI components using the @FXML annotation.

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

    private Button PlayButton ;
    private MediaPlayer mediaPlayer;
    private boolean IsPlay = false ;
    @FXML
    public void Play(  Button btn)
    {
        if (!IsPlay)
        {
            btn.setText("stop");

            IsPlay = true ;

        }
        else if (IsPlay)
        {
            btn.setText("play");

            IsPlay = false ;
        }
    }
    @FXML
    public void handlePlayAction(ActionEvent event) {
        String songPath = "";
        Button button =
                (Button) event.getSource();
        //Update labels, change button icons, etc.
        if(button.getId().equals("1")) {
           if (!IsPlay) {
               songPath = "/1.mp3";
               Play( button);
               nowPlayingLabel.setText("Rahe Meykhaneh");

           }
           else
           {
               mediaPlayer.stop();
               Play( button);
               nowPlayingLabel.setText("Stop");



           }
        }

        else if(button.getId().equals("2")) {

            if (!IsPlay) {
                songPath = "/2.mp3";
                Play( button);
                nowPlayingLabel.setText("Saghi Bia");

            }
            else
            {
                mediaPlayer.stop();
                Play( button);
                nowPlayingLabel.setText("Stop");

            }
        }

        else if(button.getId().equals("3")) {

            if (!IsPlay) {
                songPath = "/3.mp3";
                Play( button);

                nowPlayingLabel.setText("Mara Be Hich Bedady");

            }
            else
            {
                mediaPlayer.stop();
                Play(button);
                nowPlayingLabel.setText("Stop");

            }
        }

        Media media = new Media(
                getClass().getResource(songPath)
                        .toExternalForm()
        );

        mediaPlayer = new MediaPlayer(media);
        mediaPlayer.setVolume(
                volumeSlider.getValue() / 100.0
        );
        volumeSlider.valueProperty().addListener(
                (obs, oldVal, newVal) -> {

                    if(mediaPlayer != null) {

                        mediaPlayer.setVolume(
                                newVal.doubleValue() / 100.0
                        );

                    }
                }
        );
        mediaPlayer.setOnReady(() -> {

            timeSlider.setMax(mediaPlayer.getTotalDuration().toSeconds());
        });
        mediaPlayer.currentTimeProperty().addListener(
                (obs, oldTime, newTime) -> {

                    timeSlider.setValue(
                            newTime.toSeconds()
                    );
                    timeLabel.setText(
                            (int)newTime.toMinutes()
                                    + ":"
                                    + (int)(newTime.toSeconds() % 60)
                    );

                }
        );
        timeSlider.setOnMouseReleased(event2 -> {

            mediaPlayer.seek(
                    Duration.seconds(
                            timeSlider.getValue()
                    )
            );

        });

        mediaPlayer.play();
        }

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

    public void addToPlaylist( ActionEvent event) {
        //Add the song to a list or update a ListView.
        Button button =
                (Button) event.getSource();
        if(!playls.contains(button.getUserData().toString())) {
            playls.add(button.getUserData().toString());
        }
        System.out.println(playls);

    }

    // TODO:This method should open a new window or change the current scene  to display the "Playlist" page.
    @FXML
    public void openPlaylistWindow () throws IOException {
        //Create a new stage and show the playlist UI.
        System.out.println("Button Clicked");
        System.out.println(
                getClass().getResource("App-view2.fxml")
        );
        FXMLLoader loader = new FXMLLoader(
                getClass().getResource("App-view2.fxml")
        );
        Parent root = loader.load();
        MusicController controller = loader.getController();

        controller.playlistview.getItems().addAll(playls);

        Stage stage = new Stage() ;


        stage.setTitle("Playlist");

        stage.setScene(new Scene(root));

        stage.show();


        playlistview.getItems().addAll(playls) ;
    }
}
