package net.voltexstudios.npcConversations.commands;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.voltexstudios.npcConversations.NPCConversations;
import net.voltexstudios.npcConversations.npc.NPC;
import net.voltexstudios.npcConversations.npc.NPCService;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class ConversationCommand implements CommandExecutor, TabCompleter {

    private static final int INTERACTION_ENTITY_RANGE = 5;

    @Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, @NotNull String[] args) {

        if (!sender.hasPermission("npcconversation.command")) {
            sender.sendMessage(
                    NPCConversations.PREFIX.append(
                            Component.text("§cYou do not have permission to use this command."))
            );
            return false;
        }

        if (args.length != 2) {
            sender.sendMessage(NPCConversations.PREFIX.append(
                    Component.text("§cUsage: /conversation <create|delete> <name>"))
            );
            return false;
        }

        if (!(sender instanceof Player player)) {
            sender.sendMessage(
                    NPCConversations.PREFIX.append(
                            Component.text("§cOnly players can use this command."))
            );
            return false;
        }

        if (args[0].equalsIgnoreCase("delete")) {
            NPC npc = NPCService.getInstance().getNPC(args[1]);
            if (npc == null) {
                sender.sendMessage(
                        NPCConversations.PREFIX.append(
                                Component.text("§cNPC with given name not found."))
                );
                return false;
            }
            NPCService.getInstance().removeNPC(npc);
            NPCService.saveNPCData();

            sender.sendMessage(
                    NPCConversations.PREFIX.append(
                            Component.text("§aNPC deleted successfully."))
            );
            return true;
        }
        if (args[0].equalsIgnoreCase("create")) {

            Entity entity = player.getTargetEntity(INTERACTION_ENTITY_RANGE);
            if (entity == null) {
                player.sendMessage(
                        NPCConversations.PREFIX.append(
                                Component.text("§cYou must be looking at an entity to use this command."))
                );
                return false;
            }

            NPC npc = new NPC(args[1], entity.getUniqueId());

            NPCService.getInstance().addNPC(npc);
            NPCService.saveNPCData();

            sender.sendMessage(
                    NPCConversations.PREFIX.append(
                            Component.text("§aNPC created successfully.")
                                    .appendNewline()
                                    .append(Component.text("§7Name: ").append(Component.text(npc.getName()).color(NamedTextColor.BLUE)))
                                    .appendNewline()
                                    .append(Component.text("To edit the background story, go to the npcData.json file.").color(NamedTextColor.GRAY))
                    )
            );
            return true;
        }

        sender.sendMessage(NPCConversations.PREFIX.append(
                Component.text("§cUsage: /conversation <create|delete> <name>"))
        );

        return true;
    }

    @Override
    public @Nullable List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
        if (strings.length == 1) {
            return List.of("create", "delete");
        }
        if (strings.length == 2) {
            if (strings[0].equalsIgnoreCase("delete")) {
                return NPCService.getInstance().getNpcs().stream().map(NPC::getName).toList();
            }
        }
        return List.of();
    }
}
