package me.lenajenichen.voicebot.commands;

import me.lenajenichen.voicebot.LoggerInit;
import me.lenajenichen.voicebot.api.CreateDescription;
import me.lenajenichen.voicebot.listener.VoiceListener;
import me.lenajenichen.voicebot.mysql.MySQL;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.*;

import java.sql.ResultSet;
import java.sql.SQLException;

public class UnlockCommand implements ServerCommand{

    @Override
    public void performCommand(Member m, TextChannel channel, Message message) {
        String[] args = message.getContentDisplay().split(" ");

        //.voice 1
        if(args.length == 2) {
            Guild guild = m.getGuild();
            VoiceChannel vc = m.getVoiceState().getChannel();
            ResultSet getChannelID = MySQL.queryMySQL("SELECT channel_id FROM createvoice_createdchannels WHERE channel_id = '" + vc.getIdLong() + "'");
            try {
                if(getChannelID.next()) {
                    ResultSet getMemberID = MySQL.queryMySQL("SELECT user_id FROM createvoice_createdchannels WHERE channel_id = '" + vc.getIdLong() + "'");
                    if(getMemberID.next()) {
                        vc.putPermissionOverride(guild.getPublicRole()).setAllow(Permission.VIEW_CHANNEL).queue();
                        CreateDescription.createDescriptionMessage(m.getAsMention() + " dein channel wurde entsperrt!", "#00FF00", channel);
                    } else {
                        CreateDescription.createDescriptionMessage(m.getAsMention() + " das ist nicht dein Channel!","#FF0000", channel);
                    }
                } else {
                    CreateDescription.createDescriptionMessage(m.getAsMention() + " du besitzt momentan keinen Channel!", "#FF0000", channel);
                }
            } catch (SQLException e) {
                LoggerInit.logger.warning(e.toString());
            }
        }
    }
}
