package fr.monkhanny.dorfromantik.utils;

import fr.monkhanny.dorfromantik.enums.Musics;
import fr.monkhanny.dorfromantik.enums.Sounds;
import fr.monkhanny.dorfromantik.Options;

import javax.sound.sampled.Clip;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.FloatControl;

public class MusicPlayer {
    private static Clip musicClip;
    private static Clip soundClip;  // Clip séparé pour les bruitages
    private static boolean isPlayingMusic = false;
    private static boolean isPlayingSound = false;

    public static void loadMusic(Musics music) {
        if (music == Musics.MAIN_MENU_MUSIC) {
            musicClip = SoundLoader.loadMusic(Musics.MAIN_MENU_MUSIC.getSoundsPath());
            if (musicClip != null) {
                setVolume(musicClip, Options.MUSIC_VOLUME);
            }
        }
    }

    public static void loadSound(Sounds sound) {
        if (sound == Sounds.SOUNDS1) {
            soundClip = SoundLoader.loadMusic(Sounds.SOUNDS1.getSoundsPath());  // Utilise soundClip pour les bruitages
            if (soundClip != null) {
                setVolume(soundClip, Options.SOUNDS_VOLUME);
            }
        }
    }

    public static void playMusic() {
        if (musicClip != null && !isPlayingMusic && !Options.MUSIC_MUTED) {
            musicClip.start();
            isPlayingMusic = true;
        }
    }

    public static void playSound() {
        if (soundClip != null && !isPlayingSound && !Options.SOUNDS_MUTED) {
            soundClip.start();
            isPlayingSound = true;
            soundClip.addLineListener(event -> {  // Réinitialiser isPlayingSound à la fin du son
                if (event.getType() == LineEvent.Type.STOP) {
                    soundClip.setFramePosition(0);  // Retour au début du son pour rejouer si nécessaire
                    isPlayingSound = false;
                }
            });
        }
    }

    public static void pauseMusic() {
        if (musicClip != null && isPlayingMusic) {
            musicClip.stop();
            isPlayingMusic = false;
        }
    }

    public static void stopMusic() {
        if (musicClip != null) {
            musicClip.stop();
            musicClip.setFramePosition(0);
            isPlayingMusic = false;
        }
    }

    public static void setVolume(Clip clip, int volume) {
        if (clip != null) {
            FloatControl volumeControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
            float range = volumeControl.getMaximum() - volumeControl.getMinimum();
            float gain = (range * volume / 100f) + volumeControl.getMinimum();
            volumeControl.setValue(gain);
        }
    }

    public static boolean isPlayingMusic() {
        return isPlayingMusic;
    }

    public static boolean isPlayingSound() {
        return isPlayingSound;
    }

    public static Clip getMusicClip() {
        return musicClip;
    }

    public static Clip getSoundClip() {
        return soundClip;
    }
}