package uk.mgrove.ac.soton.comp1206.util;

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * Class for static multimedia actions like playing sounds
 */
public class Multimedia {

    /**
     * Logger
     */
    private static final Logger logger = LogManager.getLogger(Multimedia.class);

    /**
     * Media player for game music
     */
    private static MediaPlayer musicPlayer;
    /**
     * Media player for sound effects
     */
    private static MediaPlayer audioPlayer;

    /**
     * Play sound effect from file
     * @param filePath file path of audio file
     */
    public static void playAudio(String filePath) {
        logger.info("Playing sound effect from file: {}", filePath);

        if (audioPlayer != null) audioPlayer.stop();
        var media = new Media(Multimedia.class.getResource("/" + filePath).toExternalForm());
        audioPlayer = new MediaPlayer(media);
        audioPlayer.play();
    }

    /**
     * Play background music from file
     * @param filePath file path of audio file
     */
    public static void playMusic(String filePath) {
        logger.info("Playing music from file: {}", filePath);

        if (musicPlayer != null) musicPlayer.stop();
        var media = new Media(Multimedia.class.getResource("/" + filePath).toExternalForm());
        musicPlayer = new MediaPlayer(media);
        musicPlayer.setAutoPlay(true);
        musicPlayer.setCycleCount(MediaPlayer.INDEFINITE);
    }

}
