package org.black_ixx.bossshop.managers;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import org.apache.commons.lang.StringUtils;
import org.black_ixx.bossshop.BossShop;
import org.bukkit.inventory.ItemStack;

import cc.util.bossshop.ClassHelper;
import cc.util.bossshop.LocalLanguage;
import cc.util.bossshop.NMSHelper;
import cc.util.bossshop.config.CommentedYamlConfig;
import cc.util.bossshop.pluginmodel.INeedConfig;
import cc.util.bossshop.pluginmodel.INeedReload;


public class ItemNameManager extends LocalLanguage implements INeedConfig,INeedReload{

    private final HashMap<String,String> mItemNames=new HashMap<>();
    private String mLang="zh_CN";
    
    public ItemNameManager(BossShop pPlugin){
        super(pPlugin);
        this.mPlugin.registerConfigModel(this);
        this.mPlugin.registerReloadModel(this);
    }

    public String getLang(){
        return this.mLang;
    }
    
    public String getDisPlayName(ItemStack pItem){
        return this.getDisplayName(pItem,this.mLang);
    }
    
    @Override
    public String getDisplayName(ItemStack pItem,String pLang){
        if(pItem==null) return "";
        if(this.hasSetName(pItem)) return pItem.getItemMeta().getDisplayName();
        if(pLang.equals("en_US")){   //如果是en_US 直接使用ItemStack的getName
            Object NMSItemStack=ClassHelper.invokeStaticMethod(NMSHelper.method_CraftItemStack_asNMSCopy,pItem);
            if(NMSItemStack==null) return this.getIDFormatName(pItem);
            return (String)ClassHelper.invokeMethod(NMSItemStack,method_NMSItemStack_getName);
        }
        String unlocalName=this.getUnlocalizedName(pItem);
        String itemName=this.mItemNames.get(unlocalName);
        if(StringUtils.isNotEmpty(itemName)) return itemName;
        itemName=this.mItemNames.get(unlocalName+".name");
        if(StringUtils.isNotEmpty(itemName)) return itemName;
        return super.getDisplayName(pItem,pLang);
    }
    
    @Override
    public boolean reloadConfig(){
        if(this.mLang.equals("en_US")){
            this.mItemNames.clear();
            this.mPlugin.info("en_US语言无需配置物品翻译");
            return true;
        }
        InputStream fis=null;
        String fileName="ItemName_"+this.mLang+".lang";
        File existItemName=new File(this.mPlugin.getDataFolder(),fileName);
        if(!existItemName.isFile()){
            fis=this.mPlugin.getResource("lang/"+fileName);
            if(fis==null){
                this.mPlugin.warn("未找到物品翻译文件["+fileName+"]");
                return false;
            }
            try{
                if(!existItemName.getParentFile().isDirectory())
                    existItemName.getParentFile().mkdirs();
                existItemName.createNewFile();
            }catch(IOException ioexp){
                try{
                    fis.close();
                }catch(IOException e){}
                this.mPlugin.severe("创建物品翻译文件["+fileName+"]时发生了错误: "+ioexp.getMessage(),ioexp);
                return false;
            }
            FileOutputStream fos=null;
            byte[] buffer=new byte[1024];
            try{
                fos=new FileOutputStream(existItemName);
                int readCount=-1;
                while((readCount=fis.read(buffer))!=-1)
                    fos.write(buffer,0,readCount);
            }catch(IOException ioexp){
                this.mPlugin.severe("向物品翻译文件["+fileName+"]写入内容时发生了错误: "+ioexp.getMessage(),ioexp);
                return false;
            }finally{
                if(fos!=null) try{
                    fos.close();
                }catch(IOException e){}
                try{
                    fis.close();
                    fis=null;
                }catch(IOException e){}
            }
        }
        String[] contentLines=new String[0];
        try{
            fis=new FileInputStream(existItemName);
            byte[] contents=new byte[fis.available()];
            fis.read(contents);
            contentLines=new String(contents,"UTF-8").split("[\\r]?\n");
        }catch(IOException ioexp){
            this.mPlugin.severe("从文件["+fileName+"]读入物品翻译是发生了错误"+ioexp.getMessage(),ioexp);
            return false;
        }finally{
            if(fis!=null) try{
                fis.close();
            }catch(IOException e){}
        }
        this.mItemNames.clear();
        for(String sLine : contentLines){
            String[] t=sLine.split("=",2);
            if(t.length<2) continue;
            this.mItemNames.put(t[0],t[1]);
        }
        this.mPlugin.info("已经成功载入原版物品翻译");
        return true;
    }

    @Override
    public void setConfig(){
        CommentedYamlConfig tConfig=this.mPlugin.getConfigManager().getConfig();
        this.mLang=tConfig.getString("ItemNameLang",this.mLang).trim();
        if(this.mLang.isEmpty()) this.mLang="zh_CN";
        this.mPlugin.info("设置物品翻译语言为["+this.mLang+"]");
    }

}
