package com.yourname.premiumah.command;

import com.yourname.premiumah.PremiumAHPlugin;
import com.yourname.premiumah.config.MessageManager;
import com.yourname.premiumah.gui.GuiManager;
import com.yourname.premiumah.manager.AuctionHouseManager;
import com.yourname.premiumah.manager.ListingManager;
import com.yourname.premiumah.model.ActionResult;
import com.yourname.premiumah.model.Listing;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public final class AhAdminCommand implements TabExecutor {
    private final PremiumAHPlugin plugin;
    private final GuiManager guiManager;
    private final AuctionHouseManager auctionHouse;
    private final ListingManager listingManager;
    private final MessageManager messages;

    public AhAdminCommand(PremiumAHPlugin plugin,
                          GuiManager guiManager,
                          AuctionHouseManager auctionHouse,
                          ListingManager listingManager,
                          MessageManager messages) {
        this.plugin = plugin;
        this.guiManager = guiManager;
        this.auctionHouse = auctionHouse;
        this.listingManager = listingManager;
        this.messages = messages;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (args.length == 0 || args[0].equalsIgnoreCase("help")) {
            messages.send(sender, "admin-help");
            return true;
        }
        String sub = args[0].toLowerCase(Locale.ROOT);
        switch (sub) {
            case "reload" -> reload(sender);
            case "remove" -> remove(sender, args);
            case "forceexpire" -> forceExpire(sender, args);
            case "view" -> view(sender, args);
            default -> messages.send(sender, "admin-help");
        }
        return true;
    }

    private void reload(CommandSender sender) {
        if (!sender.hasPermission("premiumah.admin.reload")) {
            messages.send(sender, "no-permission");
            return;
        }
        plugin.reloadPluginState();
        messages.send(sender, "reload-complete");
    }

    private void remove(CommandSender sender, String[] args) {
        if (!sender.hasPermission("premiumah.admin.remove")) {
            messages.send(sender, "no-permission");
            return;
        }
        if (args.length < 2) {
            messages.send(sender, "admin-help");
            return;
        }
        ActionResult result = auctionHouse.adminRemoveListing(args[1]);
        messages.send(sender, result.messageKey(), result.placeholders());
    }

    private void forceExpire(CommandSender sender, String[] args) {
        if (!sender.hasPermission("premiumah.admin.forceexpire")) {
            messages.send(sender, "no-permission");
            return;
        }
        if (args.length < 2) {
            messages.send(sender, "admin-help");
            return;
        }
        ActionResult result = auctionHouse.forceExpireListing(args[1]);
        messages.send(sender, result.messageKey(), result.placeholders());
    }

    private void view(CommandSender sender, String[] args) {
        if (!sender.hasPermission("premiumah.admin.view")) {
            messages.send(sender, "no-permission");
            return;
        }
        if (!(sender instanceof Player player)) {
            messages.send(sender, "player-only");
            return;
        }
        if (args.length < 2) {
            guiManager.openAdmin(player, 0);
            return;
        }
        guiManager.openAdminForSeller(player, args[1], 0);
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
        if (args.length == 1) {
            return filter(List.of("reload", "remove", "view", "forceexpire", "help"), args[0]);
        }
        if (args.length == 2 && (args[0].equalsIgnoreCase("remove") || args[0].equalsIgnoreCase("forceexpire"))) {
            return filter(listingManager.all().stream().map(Listing::id).limit(50).toList(), args[1]);
        }
        if (args.length == 2 && args[0].equalsIgnoreCase("view")) {
            return filter(Bukkit.getOnlinePlayers().stream().map(Player::getName).toList(), args[1]);
        }
        return List.of();
    }

    private List<String> filter(List<String> values, String prefix) {
        String normalized = prefix.toLowerCase(Locale.ROOT);
        List<String> result = new ArrayList<>();
        for (String value : values) {
            if (value.toLowerCase(Locale.ROOT).startsWith(normalized)) {
                result.add(value);
            }
        }
        return result;
    }
}
