package me.lenajenichen.voicebot.listener;

import me.lenajenichen.voicebot.api.CreateDescription;
import me.lenajenichen.voicebot.main.BotMain;
import me.lenajenichen.voicebot.mysql.MySQL;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.message.priv.PrivateMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CaptchaPrivateMessageEvent extends ListenerAdapter {

    @Override
    public void onPrivateMessageReceived(PrivateMessageReceivedEvent e) {
        Date date = new Date();
        String now = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(new Date());
        if (!e.getAuthor().isBot()) {
            if (!(e.getAuthor().isBot())) {
                System.out.println(e.getAuthor().getId());
                ResultSet getCode = MySQL.queryMySQL("SELECT code FROM createvoice_captchas WHERE user_id = '" + e.getAuthor().getId() + "'");
                try {
                    if (getCode.next()) {
                        String code = getCode.getString("code");
                        ResultSet getAttempts = MySQL.queryMySQL("SELECT attempts FROM createvoice_captchas WHERE user_id = '" + e.getAuthor().getId() + "'");
                        if (getAttempts.next()) {
                            int attempts = getAttempts.getInt("attempts");
                            if (e.getMessage().getContentDisplay().equals(code)) {
                                ResultSet getGuildID = MySQL.queryMySQL("SELECT guild_id FROM createvoice_captchas WHERE user_id = '" + e.getAuthor().getId() + "'");
                                if (getGuildID.next()) {
                                    long guildid = getGuildID.getLong("guild_id");
                                    System.out.println(guildid);
                                    CreateDescription.createDescriptionPrivateUser(e.getMessage().getPrivateChannel().getUser(), "Du wurdest erfolgreich verifiziert!\nDu kannst nun schreiben und hast zugriff auf alle Channel.", "#00FF00", "Erfolgreich verifiziert!");
                                    ResultSet getTeamchat = MySQL.queryMySQL("SELECT channel_id FROM createvoice_teamchat WHERE guild_id = '" + guildid + "'");
                                    System.out.println("1");
                                    if(getTeamchat.next()) {
                                        System.out.println("1");
                                        long teamchatid = getTeamchat.getLong("channel_id");
                                        ResultSet getAvatarURL = MySQL.queryMySQL("SELECT avatarURL FROM createvoice_users WHERE user_id = '" + e.getMessage().getPrivateChannel().getUser().getId() + "'");
                                        if (getAvatarURL.next()) {
                                            System.out.println("1");
                                            String URL = getAvatarURL.getString("avatarURL");
                                            if(URL.equalsIgnoreCase("null")) {
                                                System.out.println("1");
                                                CreateDescription.createDescriptionTitleAuthorMessageFooter("Der nutzer " + e.getAuthor().getName() + " hat sich erfolgreich Verifiziert!", "#00FF00", e.getJDA().getGuildById(guildid).getTextChannelById(teamchatid), "http://195.90.201.89/images/default_discord_pfp.jpg", "Erfolgreiche verifizierung!", e.getAuthor().getName(), now);
                                            } else {
                                                System.out.println("2");
                                                CreateDescription.createDescriptionTitleAuthorMessageFooter("Der nutzer " + e.getAuthor().getName() + " hat sich erfolgreich Verifiziert!", "#00FF00", e.getJDA().getGuildById(guildid).getTextChannelById(teamchatid), URL, "Erfolgreiche verifizierung!", e.getAuthor().getName(), now);
                                            }
                                            System.out.println("1");
                                            MySQL.updateQuery("DELETE FROM createvoice_captchas WHERE user_id = '" + e.getAuthor().getId() + "'");
                                        }
                                    } else {
                                        System.out.println("4");
                                    }
                                }
                            } else {
                                MySQL.updateQuery("UPDATE createvoice_captchas SET attempts = '" + attempts + 1 + "'");
                                if (attempts == 3) {
                                    ResultSet getGuildID = MySQL.queryMySQL("SELECT guild_id FROM createvoice_captchas WHERE user_id = '" + e.getAuthor().getId() + "'");
                                    if (getGuildID.next()) {
                                        long guild_id = getGuildID.getLong("guild_id");
                                        BotMain.INSTATNCE.shardManager.getGuildById(guild_id).kick(e.getAuthor().getId());
                                        ResultSet getTeamchat = MySQL.queryMySQL("SELECT channel_id FROM createvoice_teamchat WHERE guild_id = '" + guild_id + "'");
                                        if (getTeamchat.next()) {
                                            long teamchat = getTeamchat.getLong("channel_id");
                                            CreateDescription.createDescriptionMessageTitle("Der Nutzer " + e.getAuthor().getName() + " hat es nicht geschafft sich nach 3 Versuchen zu verifizieren. Der Nutzer wurde nun gekickt und hat einen weiteren Versuch", "#FF0000", "Verifizierung gescheitert!", BotMain.INSTATNCE.shardManager.getGuildById(guild_id).getTextChannelById(teamchat));
                                            ResultSet getKicks = MySQL.queryMySQL("SELECT kicks FROM createvoice_captchas WHERE user_id = '" + e.getAuthor().getId() + "'");
                                            if (getKicks.next()) {
                                                int kicks = getKicks.getInt("kicks");
                                                MySQL.updateQuery("UDPATE createvoice_captchas SET kicks = '" + kicks+1 + "'");
                                                if (kicks == 3) {
                                                    BotMain.INSTATNCE.shardManager.getGuildById(guild_id).ban(e.getAuthor().getId(), 7, "Du wurdest gebannt da du nach 3 kicks nicht die Verifikation geschafft hast. Wenn etwas schief gelaufen ist dann schreibe uns bitte hier ein Entbannungsantrag: ").queue();
                                                }
                                            }
                                        }
                                    }
                                } else {
                                    ResultSet getGuildID = MySQL.queryMySQL("SELECT guild_id FROM createvoice_captchas WHERE user_id = '" + e.getAuthor().getId() + "'");
                                    if (getGuildID.next()) {
                                        long guild_id = getGuildID.getLong("guild_id");
                                        ResultSet getTeamchat = MySQL.queryMySQL("SELECT channel_id FROM createvoice_teamchat WHERE guild_id = '" + guild_id + "'");
                                        if (getTeamchat.next()) {
                                            ResultSet getKicks = MySQL.queryMySQL("SELECT kicks FROM createvoice_captchas WHERE user_id = '" + e.getAuthor().getId() + "'");
                                            if (getKicks.next()) {
                                                ResultSet getTeamChatID = MySQL.queryMySQL("SELECT message_id_teamchat FROM createvoice_captchas WHERE user_id = '" + e.getAuthor().getId() + "'");
                                                if (getTeamChatID.next()) {
                                                    long teamchat_id = getTeamChatID.getLong("message_id_teamchat");
                                                    int kicks = getKicks.getInt("kicks");
                                                    long teamchat = getTeamchat.getLong("channel_id");
                                                    MySQL.updateQuery("UPDATE createvoice_captchas SET kicks = '" + kicks+1 + "' WHERE user_id = '" + e.getAuthor().getId() + "'");
                                                    BotMain.INSTATNCE.shardManager.getGuildById(guild_id).getTextChannelById(teamchat).editMessageById(teamchat_id, "Es wurde ein neuer Account gefunden. Die Person hat nun eine recaptcha erhalten und wird diese Ausfüllen müssen.\n\nVersuche: " + attempts + "/3 | Kicks: " + kicks + "/3");
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

}
