package com.Acrobot.ChestShop.Listeners.Modules;

import com.Acrobot.Breeze.Utils.MaterialUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Events.ChestShopReloadEvent;
import com.Acrobot.ChestShop.Events.ItemParseEvent;
import com.Acrobot.ChestShop.Events.ItemStringQueryEvent;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;

import java.io.File;
import java.io.IOException;
import java.util.Locale;

/**
 * @author Acrobot
 */
public class ItemAliasModule implements Listener {
    private YamlConfiguration configuration;
    /**
     * Map ChestShop item code -> alias
     */
    private BiMap<String, String> aliases;

    public ItemAliasModule() {
        load();
    }

    private void load() {
        File file = new File(ChestShop.getFolder(), "itemAliases.yml");

        configuration = YamlConfiguration.loadConfiguration(file);

        configuration.options().header(
                "This file specified optional aliases for certain item codes. (Use the full name from /iteminfo)"
                        + "\nPlease note that these aliases should fit on a sign for it to work properly!"
        );

        if (!file.exists()) {
            configuration.addDefault("Item String#3d", "My Cool Item");
            configuration.addDefault("Other Material#Eg", "Some other Item");

            try {
                configuration.options().copyDefaults(true);
                configuration.save(ChestShop.loadFile("itemAliases.yml"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        aliases = HashBiMap.create(configuration.getKeys(false).size());

        for (String key : configuration.getKeys(false)) {
            if (configuration.isString(key)) {
                aliases.put(key, configuration.getString(key));
            }
        }
    }

    @EventHandler
    public void onReload(ChestShopReloadEvent event) {
        load();
    }

    @EventHandler(priority = EventPriority.LOW)
    public void onItemParse(ItemParseEvent event) {
        String code = aliases.inverse().get(event.getItemString());
        if (code != null) {
            event.setItem(MaterialUtil.getItem(code));
        }
    }

    @EventHandler(priority = EventPriority.HIGH)
    public void onItemStringQuery(ItemStringQueryEvent event) {
        if (event.getItemString() != null) {
            String newCode = null;

            if (aliases.containsKey(event.getItemString())) {
                newCode = aliases.get(event.getItemString());
            } else if (!event.getItemString().contains("#")) {
                newCode = aliases.get(event.getItemString().toLowerCase(Locale.ROOT));
            } else {
                String[] parts = event.getItemString().split("#", 2);
                String lowercaseCode = parts[0].toLowerCase(Locale.ROOT) + "#" + parts[1];
                if (aliases.containsKey(lowercaseCode)) {
                    newCode = aliases.get(lowercaseCode);
                }
            }

            if (newCode != null) {
                event.setItemString(newCode);
            }
        }
    }
}

@@ -622,16 +622,38 @@
 .Locale;
+%0Aimport java.util.Map;
 %0A%0A/**%0A *
@@ -2327,32 +2327,1265 @@
 tItemString());%0A
+        if (code == null) %7B%0A            String%5B%5D typeParts = event.getItemString().replaceAll(%22(?%3C!%5E)(%5BA-Z1-9%5D)%22, %22_$1%22).toUpperCase(Locale.ROOT).split(%22%5B _%5D%22);%0A            int length = Short.MAX_VALUE;%0A            for (Map.Entry%3CString, String%3E entry : aliases.entrySet()) %7B%0A                if (entry.getValue().length() %3C length && entry.getValue().startsWith(event.getItemString())) %7B%0A                    length = (short) entry.getValue().length();%0A                    code = entry.getKey();%0A                %7D else if (typeParts.length %3E 1) %7B%0A                    String%5B%5D nameParts = entry.getValue().split(%22%5B _%5D%22);%0A                    if (typeParts.length == nameParts.length) %7B%0A                        boolean matched = true;%0A                        for (int i = 0; i %3C nameParts.length; i++) %7B%0A                            if (!nameParts%5Bi%5D.startsWith(typeParts%5Bi%5D)) %7B%0A                                matched = false;%0A                                break;%0A                            %7D%0A                        %7D%0A                        if (matched) %7B%0A                            code = entry.getKey();%0A                            break;%0A                        %7D%0A                    %7D%0A                %7D%0A            %7D%0A        %7D%0A
         if (code

Fix shortened item aliases not working (Fixes #425)