package me.lenajenichen.voicebot.listener;

import me.lenajenichen.voicebot.LoggerInit;
import me.lenajenichen.voicebot.api.CreateDescription;
import me.lenajenichen.voicebot.main.BotMain;
import me.lenajenichen.voicebot.mysql.MySQL;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.events.Event;
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceJoinEvent;
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceLeaveEvent;
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceMoveEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

public class VoiceListener extends ListenerAdapter {

    public static Map<Long, Long> tempchannels;

    public VoiceListener() {
        this.tempchannels = new HashMap<>();
    }

    @Override
    public void onGuildVoiceJoin(GuildVoiceJoinEvent e) {
        onJoin(e.getChannelJoined(), e.getEntity(), e);
    }

    @Override
    public void onGuildVoiceLeave(GuildVoiceLeaveEvent e) {
        onLeave(e.getChannelLeft());
    }

    @Override
    public void onGuildVoiceMove(GuildVoiceMoveEvent e) {
        onLeave(e.getChannelLeft());
        onJoin(e.getChannelJoined(), e.getEntity(), e);
    }

    public void onJoin(VoiceChannel joined, Member member, Event e) {
        ResultSet createchannel_exists = MySQL.queryMySQL("SELECT channel_id FROM createvoice_data WHERE guild_id = '" + Long.valueOf(joined.getGuild().getId()) + "'");
        try {
            if(createchannel_exists.next()) {
                Long channel_id = createchannel_exists.getLong("channel_id");
                if(joined.getIdLong() == channel_id) {
                    Category cat = joined.getParent();
                    VoiceChannel vc = cat.createVoiceChannel(member.getEffectiveName() + "'s Channel").complete();
                    Guild guild = member.getGuild();
                    vc.putPermissionOverride(guild.getPublicRole()).setDeny(Permission.VIEW_CHANNEL).queue();
                    vc.putPermissionOverride(member).setAllow(Permission.MANAGE_CHANNEL).queue();
                    guild.moveVoiceMember(member, vc).queue();
                    MySQL.updateQuery("INSERT INTO createvoice_createdchannels(guild_id, channel_id, user_id) VALUES('" + Long.valueOf(joined.getGuild().getId()) + "', '" + vc.getIdLong() + "', '" + member.getIdLong() + "')");
                }
            }
        } catch (SQLException ex) {
            LoggerInit.logger.warning(ex.toString());
        }
    }

    public void onLeave(VoiceChannel channel) {
        if(channel.getMembers().size() <= 0) {
            ResultSet getChannel = MySQL.queryMySQL("SELECT channel_id FROM createvoice_createdchannels WHERE channel_id ='" + channel.getIdLong() + "'");
            try {
                if(getChannel.next()) {
                    Long channel_id = getChannel.getLong("channel_id");
                    MySQL.updateQuery("DELETE FROM createvoice_createdchannels WHERE channel_id = '" + channel_id + "'");
                    channel.delete().queue();
                }
            } catch (SQLException exception) {
                LoggerInit.logger.warning(exception.toString());
            }
        }
    }


}
