package net.knarcraft.blacksmith.command;

import net.knarcraft.blacksmith.config.GlobalSetting;
import net.knarcraft.blacksmith.config.NPCSetting;
import net.knarcraft.blacksmith.config.SettingValueType;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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

import static net.knarcraft.blacksmith.util.TabCompleteValuesHelper.getTabCompletions;
import static net.knarcraft.blacksmith.util.TabCompletionHelper.filterMatchingContains;

/**
 * The tab completer for the command used for changing global configuration options
 */
public class BlackSmithConfigTabCompleter implements TabCompleter {

    @Nullable
    @Override
    public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
                                      @NotNull String[] args) {
        if (args.length == 1) {
            if (!sender.hasPermission("blacksmith.admin")) {
                return new ArrayList<>();
            }

            List<String> availableCommands = new ArrayList<>();
            availableCommands.add("reload");
            for (NPCSetting setting : NPCSetting.values()) {
                availableCommands.add(setting.getCommandName());
            }
            for (GlobalSetting globalSetting : GlobalSetting.values()) {
                availableCommands.add(globalSetting.getCommandName());
            }
            return filterMatchingContains(availableCommands, args[0]);
        } else if (args.length == 2) {
            return tabCompleteCommandValues(args[0], args[1]);
        }
        return null;
    }

    /**
     * Tab completes the values available for the given command
     *
     * @param commandName  <p>The name of the used command</p>
     * @param commandValue <p>The command value used to filter tab-completions</p>
     * @return <p>Some valid options for the command's argument</p>
     */
    private List<String> tabCompleteCommandValues(String commandName, String commandValue) {
        if (commandName.equalsIgnoreCase("reload")) {
            return new ArrayList<>();
        }
        for (GlobalSetting globalSetting : GlobalSetting.values()) {
            if (globalSetting.getCommandName().equalsIgnoreCase(commandName)) {
                return getCompletions(globalSetting, commandValue);
            }
        }
        for (NPCSetting npcSetting : NPCSetting.values()) {
            if (npcSetting.getCommandName().equalsIgnoreCase(commandName)) {
                return filterMatchingContains(getTabCompletions(
                        npcSetting.getValueType()), commandValue);
            }
        }
        return null;
    }

    /**
     * Gets tab-completions for the given global setting and filters on the command value
     *
     * @param globalSetting <p>The global setting to get tab-completions for</p>
     * @param commandValue  <p>The command value used to filter between available tab-completions</p>
     * @return <p>The available tab-completions</p>
     */
    private List<String> getCompletions(GlobalSetting globalSetting, String commandValue) {
        List<String> returnValues = filterMatchingContains(getTabCompletions(globalSetting.getValueType()), commandValue);
        if (globalSetting == GlobalSetting.BASE_PRICE || globalSetting == GlobalSetting.PRICE_PER_DURABILITY_POINT) {
            returnValues.addAll(filterMatchingContains(getTabCompletions(SettingValueType.MATERIAL), commandValue));
        } else if (globalSetting == GlobalSetting.ENCHANTMENT_COST) {
            returnValues.addAll(filterMatchingContains(getTabCompletions(SettingValueType.ENCHANTMENT), commandValue));
        }
        return returnValues;
    }

}
