// CctvCommand.kt
package com.example.cctv

import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.command.TabCompleter
import org.bukkit.entity.Player
import com.example.cctv.menus.MainMenu
import com.example.cctv.menus.ViewCamerasMenu
import com.example.cctv.menus.ManageCamerasMenu
import com.example.cctv.menus.PlaceCameraMenu

class CctvCommand(private val plugin: CctvPlugin) : CommandExecutor, TabCompleter {
    override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean {
        if (sender !is Player) {
            sender.sendMessage("§cThis command can only be used by players.")
            return true
        }

        if (!sender.hasPermission("cctv.use")) {
            sender.sendMessage("§cYou don't have permission to use this command.")
            return true
        }

        if (args.isEmpty()) {
            MainMenu(plugin, sender).open()
            return true
        }

        when (args[0].lowercase()) {
            "place", "create", "add" -> {
                if (!sender.hasPermission("cctv.place")) {
                    sender.sendMessage("§cYou don't have permission to place cameras.")
                    return true
                }

                if (args.size < 2) {
                    sender.sendMessage("§cUsage: /cctv place <name>")
                    return true
                }

                val name = args.drop(1).joinToString(" ")
                if (plugin.createCamera(sender, name)) {
                    sender.sendMessage("§aCamera '$name' placed successfully!")
                } else {
                    sender.sendMessage("§cFailed to place camera.")
                }
            }

            "list", "view" -> {
                ViewCamerasMenu(plugin, sender).open()
            }

            "remove", "delete" -> {
                if (args.size < 2) {
                    ManageCamerasMenu(plugin, sender).open()
                    return true
                }

                val id = args[1]
                if (plugin.removeCamera(id, sender)) {
                    sender.sendMessage("§aCamera removed successfully!")
                } else {
                    sender.sendMessage("§cFailed to remove camera. Make sure you own it or have admin permissions.")
                }
            }

            "help" -> {
                sender.sendMessage("§6=== CCTV Plugin Help ===")
                sender.sendMessage("§e/cctv §7- Open main menu")
                sender.sendMessage("§e/cctv place <name> §7- Place a camera at your location")
                sender.sendMessage("§e/cctv list §7- View available cameras")
                sender.sendMessage("§e/cctv remove [id] §7- Remove a camera")
                sender.sendMessage("§e/cctv help §7- Show this help message")
            }

            else -> {
                sender.sendMessage("§cUnknown subcommand. Use '/cctv help' for help.")
            }
        }

        return true
    }

    override fun onTabComplete(sender: CommandSender, command: Command, alias: String, args: Array<out String>): List<String> {
        if (sender !is Player) return emptyList()

        if (args.isEmpty()) {
            return emptyList()
        }

        if (args.size == 1) {
            val subCommands = mutableListOf<String>()
            if (sender.hasPermission("cctv.use")) {
                subCommands.addAll(listOf("help", "list", "view"))
            }
            if (sender.hasPermission("cctv.place")) {
                subCommands.addAll(listOf("place", "create", "add"))
            }
            if (sender.hasPermission("cctv.remove") || sender.hasPermission("cctv.admin")) {
                subCommands.addAll(listOf("remove", "delete"))
            }

            return subCommands.filter { it.startsWith(args[0].lowercase()) }
        }

        if (args.size == 2 && (args[0].equals("remove", ignoreCase = true) || args[0].equals("delete", ignoreCase = true))) {
            val cameras = plugin.getCameras()
            return cameras.keys.filter { it.startsWith(args[1]) }
        }

        return emptyList()
    }
}