package net.lax1dude.eaglercraft.v1_8.plugin.gateway_bungeecord.command;

import net.lax1dude.eaglercraft.v1_8.plugin.gateway_bungeecord.EaglerXBungee;
import net.lax1dude.eaglercraft.v1_8.plugin.gateway_bungeecord.auth.DefaultAuthSystem;
import net.lax1dude.eaglercraft.v1_8.plugin.gateway_bungeecord.sqlite.EaglerDrivers;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.plugin.Command;

import java.sql.*;
import java.util.Properties;

public class CommandUnban extends Command {
    public CommandUnban() {
        super("bunban", "eaglercraft.command.unban");
    }

    @Override
    public void execute(CommandSender var1, String[] var2) {
        if (var2.length < 1) {
            var1.sendMessage(new TextComponent(ChatColor.RED + "How to use: " + ChatColor.WHITE + "/bungeeunban <player>"));
        } else {
            String databaseURI = EaglerXBungee.getEagler().getConfig().getBanURI();
            Connection conn;
            try {
                conn = EaglerDrivers.connectToDatabase(databaseURI,
                        EaglerXBungee.getEagler().getConfig().getAuthConfig().getDriverClass(),
                        EaglerXBungee.getEagler().getConfig().getAuthConfig().getDriverPath(), new Properties());
                if (conn == null) {
                    throw new IllegalStateException("Connection is null");
                }
            } catch (Throwable t) {
                throw new DefaultAuthSystem.AuthSystemException("Could not initialize '" + databaseURI + "'!", t);
            }
            try {
                try (Statement stmt = conn.createStatement()) {
                    var2[0] = var2[0].toLowerCase();
                    stmt.execute("CREATE TABLE IF NOT EXISTS \"banList\" (\"username\" TEXT PRIMARY KEY NOT NULL, \"expiration\" TIMESTAMP NOT NULL, \"reason\" TEXT, \"permanent\" INTEGER NOT NULL);");
                    synchronized (this) {
                        // check if the player is already unbanned
                        String checkQuery = "SELECT * FROM banList WHERE username = ?";
                        try (PreparedStatement checkStmt = conn.prepareStatement(checkQuery)) {
                            checkStmt.setString(1, var2[0]);
                            try (ResultSet rs = checkStmt.executeQuery()) {
                                if (!rs.next()) {
                                    var1.sendMessage(new TextComponent(ChatColor.RED + "That user isn't banned"));
                                    return;
                                }
                            }
                        }

                        String insertQuery = "DELETE FROM banList WHERE username = ?";
                        try (PreparedStatement insertStmt = conn.prepareStatement(insertQuery)) {
                            insertStmt.setString(1, var2[0]);
                            insertStmt.execute();
                            var1.sendMessage(new TextComponent(ChatColor.GREEN + "Unbanned " + ChatColor.WHITE + var2[0]));
                        }
                    }
                } catch (SQLException e) {
                    try {
                    conn.close();
                    } catch (SQLException ex) {

                    }
                    throw new DefaultAuthSystem.AuthSystemException("Could not execute SQL statement!", e);
                }
            } catch (Throwable t) {
                try {
                    conn.close();
                } catch (Throwable t2) {

                }
                throw new DefaultAuthSystem.AuthSystemException("Could not unban '" + var2[0] + "'!", t);
            }
        }
    }
}
