package org.plughide.plughide

import org.bukkit.Bukkit
import org.bukkit.ChatColor
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerCommandPreprocessEvent
import org.bukkit.event.server.TabCompleteEvent
import org.bukkit.plugin.java.JavaPlugin

class PlugHide : JavaPlugin(), Listener {

    private val blockedCommands = listOf(
        "/pl", "/plugins", "/plugin", "/bukkit:pl", "/bukkit:plugins", "/bukkit:plugin",
        "/minecraft:pl", "/minecraft:plugins", "/minecraft:plugin",
        "/ver", "/version", "/bukkit:ver", "/bukkit:version",
        "/minecraft:ver", "/minecraft:version",
        "/about", "/bukkit:about", "/minecraft:about",
        "/help", "/bukkit:help", "/minecraft:help",
        "/?", "/bukkit:?", "/minecraft:?"
    )

    override fun onEnable() {
        logger.info("PlugHide has been enabled!")
        server.pluginManager.registerEvents(this, this)
    }

    override fun onDisable() {
        logger.info("PlugHide has been disabled!")
    }

    private fun notifyOps(message: String) {
        val opPlayersExist = Bukkit.getOnlinePlayers().any { it.isOp }

        if (opPlayersExist) {
            // Send notification to all online OP players
            Bukkit.getOnlinePlayers()
                .filter { it.isOp }
                .forEach {
                    it.sendMessage("${ChatColor.DARK_RED}[PlugHide] ${ChatColor.YELLOW}$message")
                }
        }

        // Always log to console
        logger.info(message)
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    fun onPlayerCommand(event: PlayerCommandPreprocessEvent) {
        val player = event.player
        val command = event.message.split(" ")[0].lowercase()

        // Allow OPs to use any command
        if (player.isOp) return

        // Block restricted commands for non-OPs
        if (blockedCommands.contains(command)) {
            event.isCancelled = true
            player.sendMessage("${ChatColor.RED}Insufficient Permissions: ${ChatColor.RED}$command")

            // Notify all online OP players
            notifyOps("Player ${player.name} attempted to use blocked command: $command")
        }
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    fun onTabComplete(event: TabCompleteEvent) {
        val sender = event.sender
        val buffer = event.buffer.lowercase()

        // Don't filter tab completion for OPs
        if (sender.isOp) return

        // Block tab completion for sensitive commands
        if (blockedCommands.any { buffer.startsWith(it) } ||
            buffer == "/" ||
            buffer.startsWith("/bukkit:") ||
            buffer.startsWith("/minecraft:")) {

            // Filter out any plugin-related suggestions
            val filtered = event.completions.filter { completion ->
                val completionLower = completion.lowercase()
                !completionLower.contains("plugin") &&
                !completionLower.contains("version") &&
                !completionLower.contains("about") &&
                !completionLower.contains("bukkit") &&
                !completionLower.contains("spigot") &&
                !completionLower.contains("paper")
            }

            event.completions = filtered

            // If this appears to be an attempt to discover plugins, notify OPs
            if (sender is Player && (buffer.contains("plugin") || buffer.contains("pl"))) {
                notifyOps("Player ${sender.name} may be attempting to discover plugins via tab completion: $buffer")
            }
        }
    }
}