package cc.util.bossshop;

import java.lang.reflect.Method;
import java.util.ArrayList;

import org.apache.commons.lang.StringUtils;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

import cc.util.bossshop.pluginmodel.ABukkitPlugin;

public class LocalLanguage{
    
    public static final Method method_NMSItemStack_getName;
    public static final Method method_NMSItemStack_getUnlocalizedName;
    
    protected final static boolean isFML;
    private static Object instance_LanguageRegistry;
    private static Method method_LanguageRegistry_getStringLocalization;
    
    protected ABukkitPlugin mPlugin;
    
    static{
        //NMS ItemStck
        ItemStack tItem=new ItemStack(Material.STONE);
        Object tNMSItemStack_mcitem=ClassHelper.invokeStaticMethod(NMSHelper.method_CraftItemStack_asNMSCopy,tItem);
        //NMS ItemStack getName
        Object tObj_NBTTagCompound=ClassHelper.getInstance(NMSHelper.clazz_NBTTagCompound);
        Object tNBTTagCompound_displaye=ClassHelper.getInstance(NMSHelper.clazz_NBTTagCompound);
        String displayName="1234567890",unlocalStoneName="tile.stone";
        Object tNBTTagString_name=ClassHelper.getInstance(NMSHelper.clazz_NBTTagString,String.class,displayName);
        ClassHelper.invokeMethod(tNBTTagCompound_displaye,NMSHelper.method_NBTTagCompound_set,new Object[]{"Name",tNBTTagString_name});
        ClassHelper.invokeMethod(tObj_NBTTagCompound,NMSHelper.method_NBTTagCompound_set,new Object[]{"display",tNBTTagCompound_displaye});
        ClassHelper.setFieldValue(tNMSItemStack_mcitem,NMSHelper.field_NMSItemStack_tag,tObj_NBTTagCompound);
        ArrayList<Method> mayGetNameMethods=ClassHelper.getUnknowMethod(NMSHelper.clazz_NMSItemStack,String.class);
        int getNamePos=-1,getUnlocalNamePos=-1;
        for(int i=0;i<mayGetNameMethods.size();i++){
            String getStr=(String)ClassHelper.invokeMethod(tNMSItemStack_mcitem,mayGetNameMethods.get(i));
            //FBukkit.log("NMSItemStack方法返回值: "+getStr);
            if(getStr.equals(displayName)) getNamePos=i;
            if(getStr.startsWith(unlocalStoneName)) getUnlocalNamePos=i;
        }
        method_NMSItemStack_getName=mayGetNameMethods.get(getNamePos);
        method_NMSItemStack_getUnlocalizedName=mayGetNameMethods.get(getUnlocalNamePos);
        //NMS ItemStack getName-end
        
        isFML=ClassHelper.isClassLoaded("cpw.mods.fml.common.registry.LanguageRegistry");
        if(isFML){
            Class<?> cpwlang=ClassHelper.getClass("cpw.mods.fml.common.registry.LanguageRegistry");
            instance_LanguageRegistry=ClassHelper.invokeStaticMethod(cpwlang,"instace");
            method_LanguageRegistry_getStringLocalization=ClassHelper.getMethod(cpwlang,"getStringLocalization");
        }
    }
    
    protected LocalLanguage(ABukkitPlugin pPlugin){
        this.mPlugin=pPlugin;
    }
    
    /**
     * 物品在Lore中是否设置过名字
     */
    public boolean hasSetName(ItemStack pItem){
        if(pItem==null) return false;
        if(pItem.getItemMeta()==null) return false;
        return pItem.getItemMeta().hasDisplayName();
    }
    
    /**
     * 获取由物品ID格式化后的名字
     * <p>
     * 例如"MINECRAFT_DIRT"将会格式化未"Minecratf Dirt"
     * </p>
     */
    public String getIDFormatName(ItemStack pItem){
        if(pItem==null) return "";
        char[] tID=pItem.getType().name().toLowerCase().replace("_"," ").toCharArray();
        tID[0]=(char)(tID[0]&(0xDF));
        for(int i=0;i<tID.length;i++){
            if(tID[i]==' '&&i<tID.length-1){
                if(tID[i+1]<'a'||tID[i+1]>'z') continue;
                tID[i+1]=(char)(tID[i+1]&(0xDF));
                i++;
            }
        }
        return new String(tID);
    }
    
    /**
     * 获取指定物品指定语言的翻译
     * <p>
     * 如果物品本身存在显示名字,将返回该名字
     * 如果指定的语言不存在,将返回英文
     * 如果指定的物品不在翻译列表,将返回物品材料名字
     * </p>
     * @param pItem 指定的物品
     * @param pLang 指定的语言
     * @return 物品显示名字
     */
    public String getDisplayName(ItemStack pItem,String pLang){
        if(pItem==null) return "";
        if(this.hasSetName(pItem)) return pItem.getItemMeta().getDisplayName();
        String nowName=null;
        if(isFML){
            String unlocalName=this.getUnlocalizedName(pItem);
            nowName=this.getStringLocalization(this.getUnlocalizedName(pItem),pLang);
            if(StringUtils.isNotEmpty(nowName)) return nowName;
            nowName=this.getStringLocalization(unlocalName+".name",pLang);
            if(StringUtils.isNotEmpty(nowName)) return nowName;
        }
        Object NMSItemStack=ClassHelper.invokeStaticMethod(NMSHelper.method_CraftItemStack_asNMSCopy,pItem);
        if(NMSItemStack==null) return this.getIDFormatName(pItem);
        return (String)ClassHelper.invokeMethod(NMSItemStack,method_NMSItemStack_getName);
    }
    
    /**
     * 获取NMS物品未本地化时的语言key
     * @param pItemStack 物品
     * @return 物品语言key
     */
    public String getUnlocalizedName(ItemStack pItemStack){
        if(pItemStack==null) return "";
        Object NMSItemStack=ClassHelper.invokeStaticMethod(NMSHelper.method_CraftItemStack_asNMSCopy,pItemStack);
        if(NMSItemStack==null) return "";
        return (String)ClassHelper.invokeMethod(NMSItemStack,method_NMSItemStack_getUnlocalizedName);
    }
    
    private String getStringLocalization(String pKey,String pLang){
        return (String)ClassHelper.invokeMethod(instance_LanguageRegistry,method_LanguageRegistry_getStringLocalization,new Object[]{pKey,pLang});
    }
    
}
