package me.lenajenichen.voicebot.listener;

import me.lenajenichen.voicebot.LoggerInit;
import me.lenajenichen.voicebot.api.CreateCaptchaImage;
import me.lenajenichen.voicebot.api.CreateDescription;
import me.lenajenichen.voicebot.mysql.MySQL;
import net.dv8tion.jda.api.entities.ChannelType;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.message.MessageUpdateEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageUpdateEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.time.Year;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MessageEvent extends ListenerAdapter {

    @Override
    public void onGuildMessageReceived(GuildMessageReceivedEvent e) {
        String messageContent = e.getMessage().getContentDisplay().replace("'", "/*/");
        Long guild_id = e.getGuild().getIdLong();
        Long message_id = e.getMessageIdLong();
        String author = e.getMessage().getAuthor().getName();
        User user = e.getMessage().getMember().getUser();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        Date date = new Date();
        //Role role = e.getGuild().getRoleById(849635686385975364L);
        //e.getGuild().removeRoleFromMember(285452141332660226L, role).queue();
        MySQL.updateQuery("INSERT INTO createvoice_messages(guild_id, message_id, user_id, message, user, date, lastUpdateDate) VALUES('" + guild_id + "', '" + message_id + "', '" + Long.valueOf(e.getMessage().getMember().getId()) + "', '" + messageContent + "', '" + author + "', '" + date + "', '" + " " + "')");
        ResultSet getUser = MySQL.queryMySQL("SELECT user_id FROM createvoice_users WHERE user_id = '" + user.getId() + "'");
        try {
            if (!(getUser.next())) {
                MySQL.updateQuery("INSERT INTO createvoice_users(user_id, user_name, user_discriminator, avatarURL) VALUES('" + user.getId() + "', '" + user.getName() + "', '" + user.getDiscriminator() + "', '" + user.getAvatarUrl() + "')");
            }
        } catch (SQLException ex) {
            LoggerInit.logger.warning(ex.toString());
        }
    }

    @Override
    public void onGuildMessageUpdate(GuildMessageUpdateEvent e) {
        String newMessageContent = e.getMessage().getContentDisplay().replace("'", "/*/");
        Long guild_id = e.getGuild().getIdLong();
        Long message_id = e.getMessageIdLong();
        String author = e.getMessage().getAuthor().getName();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        Date date = new Date();
        ResultSet getLogChannel = MySQL.queryMySQL("SELECT channel_id FROM createvoice_logchannel WHERE guild_id = '" + Long.valueOf(e.getGuild().getId()) + "'");
        if (!e.getAuthor().isBot()) {
            try {
                if (getLogChannel.next()) {
                    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 oldMessageContent = MySQL.queryMySQL("SELECT message FROM createvoice_messages WHERE message_id = '" + message_id + "'");
                            if (oldMessageContent.next()) {
                                String message = oldMessageContent.getString("message");
                                Long logChannel_id = getLogChannel.getLong("channel_id");
                                TextChannel logchannel = e.getJDA().getTextChannelById(logChannel_id);
                                MySQL.updateQuery("UPDATE createvoice_messages SET message ='" + newMessageContent + "', lastUpdateDate = '" + date + "' WHERE message_id ='" + message_id + "'");
                                CreateDescription.createDescriptionTitleAuthorMessageFooter("Before: **" + message.replace("/*/", "'") + "**\n\n After: **" + newMessageContent.replace("/*/", "'") + "**", "#00FFFF", logchannel, e.getMessage().getMember().getUser().getAvatarUrl(), "Message Edited", author, simpleDateFormat.format(date) + " \nhttps://discord.com/channels/" + e.getGuild().getId() + "/" + e.getChannel().getId() + "/" + e.getMessageId());
                            }
                        }
                    }
                }
            } catch (SQLException e1) {
                LoggerInit.logger.warning(e1.toString());
            }
        }
    }
}