package de.viper.survivalplus.commands;

import de.viper.survivalplus.SurvivalPlus;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.configuration.file.FileConfiguration;

import java.util.UUID;
import java.util.logging.Level;

public class InventoryCommand implements CommandExecutor, Listener {

    private final SurvivalPlus plugin;

    public InventoryCommand(SurvivalPlus plugin) {
        this.plugin = plugin;
        Bukkit.getPluginManager().registerEvents(this, plugin);
    }

    public static class SnapshotHolder implements InventoryHolder {
        private final UUID targetUuid;
        private final boolean readOnly;

        public SnapshotHolder(UUID targetUuid, boolean readOnly) {
            this.targetUuid = targetUuid;
            this.readOnly = readOnly;
        }

        public UUID getTargetUuid() {
            return targetUuid;
        }

        public boolean isReadOnly() {
            return readOnly;
        }

        @Override
        public Inventory getInventory() {
            return null; 
        }
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        FileConfiguration lang = plugin.getLangConfig();

        if (!(sender instanceof Player)) {
            sender.sendMessage(lang.getString("player-only"));
            return true;
        }

        Player player = (Player) sender;

        if (!player.hasPermission("survivalplus.inventory.own")) {
            player.sendMessage(lang.getString("no-permission"));
            return true;
        }

        if (args.length != 1) {
            player.sendMessage(lang.getString("inventory.usage"));
            return true;
        }

        String targetName = args[0];
        OfflinePlayer target = Bukkit.getOfflinePlayer(targetName);

        Player onlineTarget = Bukkit.getPlayerExact(targetName);
        if (onlineTarget != null && onlineTarget.isOnline()) {
            if (!player.getName().equalsIgnoreCase(targetName) && !player.hasPermission("survivalplus.inventory.others")) {
                player.sendMessage(lang.getString("no-permission-others-inv"));
                return true;
            }

            PlayerInventory targetInv = onlineTarget.getInventory();
            player.openInventory(targetInv);
            player.sendMessage(lang.getString("inventory.opened").replace("%player%", targetName));
            plugin.getLogger().log(Level.INFO, "Inventar von " + targetName + " (online) wurde von " + player.getName() + " geöffnet.");
            return true;
        }

        if (target.getName() == null || !target.hasPlayedBefore()) {
            player.sendMessage(lang.getString("player-not-found"));
            return true;
        }

        if (!player.getName().equalsIgnoreCase(targetName) && !player.hasPermission("survivalplus.inventory.others")) {
            player.sendMessage(lang.getString("no-permission-others-inv"));
            return true;
        }

        // Offline-Inventare nicht editierbar
        player.sendMessage(lang.getString("inventory.offline-not-editable"));
        plugin.getLogger().log(Level.INFO, "Inventar von " + targetName + " ist offline — Live-Bearbeitung nicht möglich.");

        return true;
    }

    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
        InventoryHolder holder = event.getInventory().getHolder();
        if (holder instanceof SnapshotHolder) {
            SnapshotHolder sh = (SnapshotHolder) holder;
            if (sh.isReadOnly()) {
                if (event.getWhoClicked() instanceof Player) {
                    Player p = (Player) event.getWhoClicked();
                    FileConfiguration lang = plugin.getLangConfig();
                    p.sendMessage(lang.getString("inventory.read-only"));
                }
                event.setCancelled(true);
            }
        }
    }

    @EventHandler
    public void onInventoryDrag(InventoryDragEvent event) {
        InventoryHolder holder = event.getInventory().getHolder();
        if (holder instanceof SnapshotHolder) {
            SnapshotHolder sh = (SnapshotHolder) holder;
            if (sh.isReadOnly()) {
                if (event.getWhoClicked() instanceof Player) {
                    Player p = (Player) event.getWhoClicked();
                    FileConfiguration lang = plugin.getLangConfig();
                    p.sendMessage(lang.getString("inventory.read-only"));
                }
                event.setCancelled(true);
            }
        }
    }
}
