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 CommandListBans extends Command {
    public CommandListBans() {
        super("blistbans", "eaglercraft.command.listbans");
    }

    @Override
    public void execute(CommandSender var1, String[] var2) {
        if (var2.length != 0) {
            var1.sendMessage(new TextComponent(ChatColor.RED + "How to use: " + ChatColor.WHITE + "/blistbans"));
        } 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()) {
                    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) {
                        String checkQuery = "SELECT * FROM banList";
                        try (PreparedStatement checkStmt = conn.prepareStatement(checkQuery)) {
                            try (ResultSet rs = checkStmt.executeQuery()) {
                                while (rs.next()) {
                                    var1.sendMessage(new TextComponent(ChatColor.RED + "Player: " + ChatColor.WHITE + rs.getString("username") + ChatColor.RED + " Reason: " + ChatColor.WHITE + rs.getString("reason") + ChatColor.RED + " Expiration: " + ChatColor.WHITE + rs.getString("expiration") + ChatColor.RED + " Permanent: " + ChatColor.WHITE + rs.getString("permanent")));
                                }
                            }
                        }
                    }
                } catch (SQLException e) {
                    throw new DefaultAuthSystem.AuthSystemException("Could not list bans!", e);
                }
            } catch (Throwable e) {
                throw new DefaultAuthSystem.AuthSystemException("Could not list bans!", e);
            }
        }
    }
}
