package me.lenajenichen.voicebot.listener;

import jdk.nashorn.internal.runtime.regexp.joni.CodeRangeBuffer;
import me.lenajenichen.voicebot.LoggerInit;
import me.lenajenichen.voicebot.api.CreateDescription;
import me.lenajenichen.voicebot.mysql.MySQL;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.message.MessageDeleteEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageDeleteEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class MessageDelete extends ListenerAdapter {

    @Override
    public void onMessageDelete(MessageDeleteEvent e) {
        HashMap<Integer, Long> getID = new HashMap<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        Date date = new Date();
        String messageid = e.getMessageId();
        ResultSet getLogChannel = MySQL.queryMySQL("SELECT channel_id FROM createvoice_logchannel WHERE guild_id = '" + Long.valueOf(e.getGuild().getId()) + "'");
        try {
            if (getLogChannel.next()) {
                Long logChannel_id = getLogChannel.getLong("channel_id");
                ResultSet getGuildID = MySQL.queryMySQL("SELECT guild_id FROM createvoice_logchannel WHERE channel_id ='" + logChannel_id + "'");
                if (getGuildID.next()) {
                    Long guildid = getGuildID.getLong("guild_id");
                    ResultSet getIgnoredChannels = MySQL.queryMySQL("SELECT ignored_channel_id FROM createvoice_setup WHERE guild_id = '" + Long.valueOf(e.getGuild().getId()) + "'");
                    if (getIgnoredChannels.next()) {
                        List<Long> channel_id = new ArrayList<>();
                        do {
                            Long channelids = getIgnoredChannels.getLong("ignored_channel_id");
                            channel_id.add(channelids);
                        } while (getIgnoredChannels.next());
                        if (!(channel_id.contains(e.getChannel().getId()))) {
                            ResultSet getOldMessage = MySQL.queryMySQL("SELECT message FROM createvoice_messages WHERE message_id = '" + messageid + "'");
                            if (getOldMessage.next()) {
                                ResultSet getUserID = MySQL.queryMySQL("SELECT user_id FROM createvoice_messages WHERE message_id = '" + messageid + "'");
                                if (getUserID.next()) {
                                    Long user_id = getUserID.getLong("user_id");
                                    ResultSet getAvatarURL = MySQL.queryMySQL("SELECT avatarURL FROM createvoice_users WHERE user_id = '" + user_id + "'");
                                    if(getAvatarURL.next()) {
                                        String url = getAvatarURL.getString("avatarURL");
                                        ResultSet getUsername = MySQL.queryMySQL("SELECT user_name FROM createvoice_users WHERE user_id = '" + user_id + "'");
                                        if(getUsername.next()) {
                                            String username = getUsername.getString("user_name");
                                            String message = getOldMessage.getString("message");
                                            TextChannel logchannel = e.getJDA().getGuildById(guildid).getTextChannelById(logChannel_id);
                                            CreateDescription.createDescriptionTitleAuthorMessageFooter("**" + message.replace("/*/", "'") + "**", "#FF0000", logchannel, url, "Deleted Message", username, simpleDateFormat.format(date) + " URL: https://discord.com/channels/" + e.getGuild().getId() + "/" + e.getChannel().getId() + "/" + e.getMessageId());
                                        } else {
                                            System.out.println("1");
                                        }
                                    } else {
                                        System.out.println("2");
                                    }
                                } else {
                                    System.out.println("3");
                                }
                            } else {
                                System.out.println("4");
                            }
                        } else {
                            System.out.println("5");
                        }
                    } else {
                        System.out.println("6");
                    }
                } else {
                    System.out.println("7");
                }
            } else {
                System.out.println("8");
            }
        } catch (SQLException e1) {
            LoggerInit.logger.warning(e1.toString());
        }
    }

}
