package me.lenajenichen.voicebot.mysql;

import me.lenajenichen.voicebot.LoggerInit;

import javax.swing.plaf.nimbus.State;
import java.sql.*;

public class MySQL {

    public static Connection createvoice_database;
    public static Statement stmt;

    public static void connect()
    {
        createvoice_database = null;
        if (!isConnected()) {
            try
            {
                Class.forName("com.mysql.cj.jdbc.Driver");
                createvoice_database = DriverManager.getConnection("jdbc:mysql://" + "localhost" + ":" + "3306" + "/" + "createvoice", "discordbot", "");
                stmt = createvoice_database.createStatement();
            }
            catch (SQLException | ClassNotFoundException e)
            {
                LoggerInit.logger.warning(e.toString());
            }
        } else {
            System.out.println("Es besteht bereits eine Verbindung.");
        }
    }

    public static void disconnect() {
        if(isConnected()) {
            try {
                createvoice_database.close();
            } catch (SQLException e) {
                LoggerInit.logger.warning(e.toString());
            }
        }
    }

    public static boolean isConnected()
    {
        return createvoice_database != null;
    }
    public static void createTable()
    {
        try
        {
            MySQL.connect();
            createvoice_database.prepareStatement("CREATE TABLE IF NOT EXISTS createvoice_data(channel_id LONG, guild_id LONG)").executeUpdate();
            createvoice_database.prepareStatement("CREATE TABLE IF NOT EXISTS createvoice_setup(guild_id LONG, ignored_channel_id LONG, channel_name VARCHAR(101))").executeUpdate();
            createvoice_database.prepareStatement("CREATE TABLE IF NOT EXISTS createvoice_deletedmessages(id LONG, message TEXT, guild_id LONG, text_channel_id LONG, text_channel_name TEXT, user_id LONG, user_name VARCHAR (100))").executeUpdate();
            createvoice_database.prepareStatement("CREATE TABLE IF NOT EXISTS createvoice_logchannel(guild_id LONG, channel_id LONG, channel_name VARCHAR(101))").executeUpdate();
            createvoice_database.prepareStatement("CREATE TABLE IF NOT EXISTS createvoice_createdchannels(guild_id LONG, channel_id LONG, user_id LONG)").executeUpdate();
            createvoice_database.prepareStatement("CREATE TABLE IF NOT EXISTS createvoice_messages(guild_id LONG, message_id LONG, user_id LONG, message TEXT, user VARCHAR(100), date VARCHAR(30), lastUpdateDate VARCHAR(30))").executeUpdate();
            createvoice_database.prepareStatement("CREATE TABLE IF NOT EXISTS createvoice_users(user_id LONG, user_name TEXT, user_discriminator TEXT, avatarURL TEXT)").executeUpdate();
            createvoice_database.prepareStatement("CREATE TABLE IF NOT EXISTS createvoice_captchas(guild_id LONG, message_id LONG, message_id_teamchat LONG,user_id Long, code VARCHAR(5), attempts INTEGER, kicks INTEGER)").executeUpdate();
            createvoice_database.prepareStatement("CREATE TABLE IF NOT EXISTS createvoice_teamchat(guild_id LONG, channel_id LONG, channel_name TEXT)").executeUpdate();
        }
        catch (SQLException e)
        {
            LoggerInit.logger.warning(e.toString());
        }
    }

    public static void updateQuery(String sqlCommand) {
        try {
            MySQL.connect();
            stmt.executeUpdate(sqlCommand);
            MySQL.disconnect();
        } catch (SQLException e) {
            LoggerInit.logger.warning(e.toString());
            MySQL.disconnect();
        }
    }


    public static ResultSet queryMySQL(String sqlCommand) {
        try {
            MySQL.connect();
            //stmt = message_database.createStatement();
            return stmt.executeQuery(sqlCommand);
        } catch (SQLException e) {
            MySQL.disconnect();
            LoggerInit.logger.warning(e.toString());
        }
        return null;
    }

}
