package sbu.javafx;

import javafx.beans.property.*;

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;

public class Track {

    private final StringProperty trackName = new SimpleStringProperty();
    private final StringProperty artistName = new SimpleStringProperty();
    private final StringProperty duration = new SimpleStringProperty();
    private final StringProperty playButton = new SimpleStringProperty();
    private final StringProperty addButton = new SimpleStringProperty();
    private String fileName;
    private MediaPlayer player;

    public Track (String trackName, String artistName, String duration, String fileName) {
        this.trackName.set(trackName);
        this.artistName.set(artistName);
        this.duration.set(duration);
        this.playButton.set("▶");
        this.addButton.set("＋");
        if (fileName.isEmpty()) this.fileName = "Yujime's Theme.mp3";
        else this.fileName = fileName;

        String audioFilePath = getClass().getResource(this.fileName).toExternalForm();
        Media media = new Media(audioFilePath);
        player = new MediaPlayer(media);
    }

    public void play() {
        if (player != null) {
            player.play();
        }
    }

    public void stop() {
        if (player != null) {
            player.stop();
        }
    }

    public StringProperty getTrackNameProperty() { return trackName; }

    public StringProperty getArtistNameProperty() { return artistName; }

    public StringProperty getDurationProperty() { return duration; }

    public String getTrackName() { return trackName.get(); }

    public String getArtistName() { return artistName.get(); }

    public String getDuration() { return duration.get(); }

    public String getFilePath() { return fileName; }

    public StringProperty getPlayButton() { return playButton; }

    public StringProperty getAddButton() { return addButton; }

    public void setPlayButton(String s) { playButton.set(s); }

    public void setAddButton(String s) { addButton.set(s); }
}
