package kr.co.swh.lecture.opensource.project.discode.music; 

import java.util.HashMap;
import java.util.Map;

import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers;
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;

import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.TextChannel;

/**
 * <pre>
 * kr.co.swh.lecture.opensource.project.discode.music 
 * PlayerManager.java
 *
 * 설명 :
 * </pre>
 * 
 * @since : 2021. 6. 13.
 * @author : tobby48
 * @version : v1.0
 */
public class PlayerManager {
    private static PlayerManager INSTANCE;
    private final AudioPlayerManager playerManager;
    private final Map<Long, GuildMusicManager> musicManagers;

    private PlayerManager() {
        this.musicManagers = new HashMap<>();

        this.playerManager = new DefaultAudioPlayerManager();
        AudioSourceManagers.registerRemoteSources(playerManager);
        AudioSourceManagers.registerLocalSource(playerManager);
    }

    public synchronized GuildMusicManager getGuildMusicManager(Guild guild) {
        long guildId = guild.getIdLong();
        GuildMusicManager musicManager = musicManagers.get(guildId);

        if (musicManager == null) {
            musicManager = new GuildMusicManager(playerManager);
            musicManagers.put(guildId,musicManager);
        }

        guild.getAudioManager().setSendingHandler(musicManager.getSendHandler());

        return musicManager;

    }

    public void loadAndPlay(TextChannel channel,String trackURL) {
        String prefix = "[MusicBotInfo]";
        GuildMusicManager musicManager = getGuildMusicManager(channel.getGuild());

        playerManager.loadItemOrdered(musicManager, trackURL, new AudioLoadResultHandler() {
            @Override
            public void trackLoaded(AudioTrack track) {
//                Main.sendMessage(Main.channel, prefix + "次の音楽を再生します:" + track.getInfo());
//            	channel.sendMessage(Main.channel, prefix + "次の音楽を再生します:" + track.getInfo());

                play(musicManager,track);
            }

            @Override
            public void playlistLoaded(AudioPlaylist playlist) {
                AudioTrack firstTrack = playlist.getSelectedTrack();

                if (firstTrack == null) {
                    firstTrack = playlist.getTracks().get(0);
                }

//                Main.sendMessage(Main.channel, prefix + "次の音楽を再生します:" + firstTrack.getInfo());

                play(musicManager,firstTrack);
            }

            @Override
            public void noMatches() {
                //見つからなかった
                channel.sendMessage(prefix + "指定されたURLの音楽が見つかりませんでした。");
            }

            @Override
            public void loadFailed(FriendlyException e) {
                //ロード時にエラー発生
                channel.sendMessage(prefix + "再生のロードでエラーが発生しました。");
            }
        });

    }

    private void play(GuildMusicManager musicManager,AudioTrack track) {
        musicManager.scheduler.queue(track);
    }

    public static synchronized PlayerManager getINSTANCE() {
        if (INSTANCE == null) {
            INSTANCE = new PlayerManager();
        }

        return INSTANCE;
    }
}