package sbu.javafx;

import javafx.beans.Observable;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
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.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class AppController {

    @FXML private Button playlistOrTracks;
    @FXML private Button themeToggle;

    @FXML private Label playingTrackName;
    @FXML private Label playingTrackArtistName;
    @FXML private Label playingTrackDuration;

    @FXML private VBox tracksContainer;

    private static boolean isDark = true;
    private static boolean isPlaylist;
    private static Track playingTrack = new Track("___", "___", "00:00", "");

    private static List<Track> tracks = new ArrayList<>();
    private static List<Track> playlist = new ArrayList<>();

    public static List<Track> getTracks() { return tracks; }

    public static List<Track> getPlaylist() { return playlist; }

    public static boolean isPlaylist() { return isPlaylist; }

    public static void setPlayingTrack(Track pTrack) {

        //updating playingTrack box
        playingTrack.getTrackNameProperty().set(pTrack.getTrackName());
        playingTrack.getArtistNameProperty().set(pTrack.getArtistName());
        playingTrack.getDurationProperty().set(pTrack.getDuration());

        if (isPlaylist) {
            //updating all play & add buttons
            for (Track t : playlist) {
                if (t.getTrackName().equals(pTrack.getTrackName()) &&
                        t.getArtistName().equals(pTrack.getArtistName())) {
                    t.setPlayButton("⏸");
                    t.play();
                } else {
                    t.setPlayButton("▶");
                    t.stop();
                }
            }
        }
        else {
            //updating all play & add buttons
            //updating each media action
            for (Track t : tracks) {
                if (t.getTrackName().equals(pTrack.getTrackName()) &&
                        t.getArtistName().equals(pTrack.getArtistName())) {
                    t.setPlayButton("⏸");
                    t.play();
                } else {
                    t.setPlayButton("▶");
                    t.stop();
                }
            }
        }
    }

    public static void addOrRemove(Track track) {

        boolean isInPlaylist = false;
        for (Track t : playlist) {
            if (t.getTrackName().equals(track.getTrackName()) &&
                    t.getArtistName().equals(track.getArtistName())) {

                isInPlaylist = true;
                break;
            }
        }
        if (isInPlaylist) playlist.remove(track);
        else playlist.add(track);


        for (Track t : tracks) {
            t.setAddButton("＋");
            for (Track p : playlist) {
                if (t.equals(p)) {
                    t.setAddButton("－");
                }
            }
        }
    }

    @FXML
    public void toggleTheme() {
        isDark = !isDark;

        themeToggle.getScene().getStylesheets().clear();
        if (!isDark) {
            //Switch to Light mode
            themeToggle.getScene().getStylesheets().add(getClass().getResource("light.css").toExternalForm());
            themeToggle.setText("☀");
        }
        else {
            //Switch to Dark mode
            themeToggle.getScene().getStylesheets().add(getClass().getResource("dark.css").toExternalForm());
            themeToggle.setText("☽");
        }
    }

    // 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.
        isPlaylist = !isPlaylist;
        try {
            Stage stage = (Stage) playingTrackName.getScene().getWindow();
            FXMLLoader fxmlLoader = new FXMLLoader(MusicApp.class.getResource("App-view.fxml"));
            Scene newScene = new Scene(fxmlLoader.load(), 700, 700);
            if (isDark) newScene.getStylesheets().add(getClass().getResource("dark.css").toExternalForm());
            else newScene.getStylesheets().add(getClass().getResource("light.css").toExternalForm());

            stage.setScene(newScene);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    @FXML
    public void initialize() {

        playingTrackName.textProperty().bind(playingTrack.getTrackNameProperty());
        playingTrackArtistName.textProperty().bind(playingTrack.getArtistNameProperty());
        playingTrackDuration.textProperty().bind(playingTrack.getDurationProperty());

        tracks.add(new Track("End of Beginning", "Djo", "02:39", "End of Beginning.mp3"));
        tracks.add(new Track("Heathens", "Twenty One Pilots", "03:20", "Heathens.mp3"));
        tracks.add(new Track("Mask Off (Instrumental)", "Future", "03:30", "Mask Off (Instrumental).mp3"));
        tracks.add(new Track("California Dreamin'", "The Mamas & the Papas", "02:45", "California Dreamin'.mp3"));
        tracks.add(new Track("Let Me Down slowly", "Alec Benjamin", "02:49", "Let Me Down Slowly.mp3"));
        tracks.add(new Track("Attention", "Charlie Puth", "03:51", "Attention.mp3"));
        tracks.add(new Track("Talking to the Moon", "Bruno Mars", "03:35", "Talking to the Moon.mp3"));
        tracks.add(new Track("Believer", "Imagine Dragons", "03:23", "Believer.mp3"));
        tracks.add(new Track("Yumeji's Theme", "Shigeru Umebayashi", "02:30", "Yujime's Theme.mp3"));
        tracks.add(new Track("In This Shirt", "The Irrepressibles", "05:35", "In This Shirt.mp3"));
        tracks.add(new Track("GOSSIP", "Måneskin (ft. Tom Morello)", "02:48", "GOSSIP.mp3"));
        tracks.add(new Track("They Don't Care About Us", "Michael Jackson", "04:44", "They Don't Care About Us.mp3"));

        if (isPlaylist) {
            playlistOrTracks.setText("Tracks");
            for (Track t : playlist) {
                try {
                    FXMLLoader loader = new FXMLLoader(getClass().getResource("Track-boxes.fxml"));

                    HBox trackBox = loader.load();

                    TrackController controller = loader.getController();
                    controller.setTrack(t);

                    tracksContainer.getChildren().add(trackBox);

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        else {
            playlistOrTracks.setText("Playlist");
            for (Track t : tracks) {
                try {
                    FXMLLoader loader = new FXMLLoader(getClass().getResource("Track-boxes.fxml"));

                    HBox trackBox = loader.load();

                    TrackController controller = loader.getController();
                    controller.setTrack(t);

                    tracksContainer.getChildren().add(trackBox);

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
