package me.lenajenichen.voicebot.api;

import me.lenajenichen.voicebot.mysql.MySQL;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.User;

import java.awt.*;
import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CreateDescription {

    private static long latestMessageID = 0L;

    public static void createDescriptionMessage(String message, String colorcode, TextChannel channel) {
        EmbedBuilder builder = new EmbedBuilder();
        builder.setDescription(message);
        builder.setColor(Color.decode(colorcode));
        channel.sendMessage(builder.build()).queue();
    }

    public static void createDescriptionMessageTitle(String message, String colorcode, String title,TextChannel channel) {
        EmbedBuilder builder = new EmbedBuilder();
        builder.setDescription(message);
        builder.setColor(Color.decode(colorcode));
        builder.setTitle(title);
        channel.sendMessage(builder.build()).queue();
    }

    public static void createDescriptionImageMessage(String message, String colorcode, TextChannel channel, String url,
                                                     String name) {
        EmbedBuilder builder = new EmbedBuilder();
        builder.setDescription(message);
        builder.setColor(Color.decode(colorcode));
        builder.setAuthor(name, null, url);
        channel.sendMessage(builder.build()).queue();
    }

    public static void createDescriptionImageMessageTitle(String message, String colorcode, TextChannel channel, String url,
                                                     String name, String title) {
        EmbedBuilder builder = new EmbedBuilder();
        builder.setDescription(message);
        builder.setColor(Color.decode(colorcode));
        builder.setAuthor(name, null, url);
        builder.setTitle(title);
        channel.sendMessage(builder.build()).queue();
    }

    public static void createDescriptionTitleAuthorMessage(String message, String colorcode, TextChannel channel,
                                                           String url, String Title, String name) {
        EmbedBuilder builder = new EmbedBuilder();
        builder.setDescription(message);
        builder.setColor(Color.decode(colorcode));
        builder.setTitle(Title, url);
        builder.setAuthor(name, null, url);
        channel.sendMessage(builder.build()).queue();
    }

    public static void createDescriptionTitleAuthorMessageFooterSQL(String message, String colorcode, TextChannel channel,
                                                           String url, String Title, String name, String footer) {
        EmbedBuilder builder = new EmbedBuilder();
        builder.setDescription(message);
        builder.setColor(Color.decode(colorcode));
        builder.setTitle(Title, null);
        builder.setAuthor(name, null, url);
        builder.setFooter(footer);
        channel.sendMessage(builder.build()).queue();
        MySQL.updateQuery("UPDATE createvoice_captchas SET message_id_teamchat = '" + channel.getLatestMessageId() + "'");
    }

    public static void createDescriptionTitleMessageFooterSQL(String message, String colorcode, TextChannel channel,
                                                              String Title, String name, String footer) {
        EmbedBuilder builder = new EmbedBuilder();
        builder.setDescription(message);
        builder.setColor(Color.decode(colorcode));
        builder.setTitle(Title, null);
        builder.setAuthor(name, null);
        builder.setFooter(footer);
        channel.sendMessage(builder.build()).queue();
        MySQL.updateQuery("UPDATE createvoice_captchas SET message_id_teamchat = '" + channel.getLatestMessageId() + "'");
    }

    public static void createDescriptionTitleAuthorMessageFooter(String message, String colorcode, TextChannel channel,
                                                                    String url, String Title, String name, String footer) {
        EmbedBuilder builder = new EmbedBuilder();
        builder.setDescription(message);
        builder.setColor(Color.decode(colorcode));
        builder.setTitle(Title, null);
        builder.setAuthor(name, null, url);
        builder.setFooter(footer);
        channel.sendMessage(builder.build()).queue();
    }

    public static void createDescriptionPrivateMemberURL(Member m, String message, String colorcode, String name, String url) {
        EmbedBuilder builder = new EmbedBuilder();
        builder.setDescription(message);
        builder.setColor(Color.decode(colorcode));
        builder.setAuthor(name, null, url);
        m.getUser().openPrivateChannel().queue(channel -> {
            channel.sendMessage(builder.build()).queue();
        });
    }

    public static void createDescriptionPrivateMemberMessageTitle(Member m, String message, String colorcode, String title, String imageURL) {
        EmbedBuilder builder = new EmbedBuilder();
        builder.setDescription(message);
        builder.setColor(Color.decode(colorcode));
        builder.setTitle(title);
        builder.setImage(imageURL);
        m.getUser().openPrivateChannel().queue(channel -> {
            channel.sendMessage(builder.build()).queue();
            latestMessageID = channel.getLatestMessageIdLong();
        });
        ResultSet getUserID = MySQL.queryMySQL("SELECT user_id FROM createvoice_captchas WHERE user_id = '" + m.getUser().getId() + "'");
        try {
            if (getUserID.next()) {
                ResultSet getKicks = MySQL.queryMySQL("SELECT kicks FROM createvoice_captchas WHERE user_id = '" + m.getUser().getId() + "'");
                if(getKicks.next()) {
                    int kicks = getKicks.getInt("kicks");
                }
            } else {
                MySQL.updateQuery("INSERT INTO createvoice_captchas(guild_id, message_id, message_id_teamchat, user_id, code, attempts, kicks) VALUES('" + " " + "', '" + latestMessageID + "', '" + " " + "', '" + m.getUser().getId() + "', '" + CreateCaptchaImage.getCode() + "', '" + 0 + "', '" + 0 + "')");
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    public static void createDescriptionPrivateMember(Member m, String message, String colorcode, String title) {
        EmbedBuilder builder = new EmbedBuilder();
        builder.setDescription(message);
        builder.setColor(Color.decode(colorcode));
        builder.setTitle(title);
        m.getUser().openPrivateChannel().queue(channel -> {
            channel.sendMessage(builder.build()).queue();
        });
    }

    public static void createDescriptionPrivateUser(User user, String message, String colorcode, String title) {
        EmbedBuilder builder = new EmbedBuilder();
        builder.setDescription(message);
        builder.setColor(Color.decode(colorcode));
        builder.setTitle(title);
        user.openPrivateChannel().queue(channel -> {
            channel.sendMessage(builder.build()).queue();
        });
    }

    public static void createDescriptionPrivateUserAll(User user, String message, String colorcode, String url, String name) {
        EmbedBuilder builder = new EmbedBuilder();
        builder.setDescription(message);
        builder.setColor(Color.decode(colorcode));
        builder.setAuthor(name, null, url);
        user.openPrivateChannel().queue(channel -> {
            channel.sendMessage(builder.build()).queue();
        });
    }

}
