package org.black_ixx.bossshop.mail;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.black_ixx.bossshop.BossShop;
import org.black_ixx.bossshop.core.BSEnums.BSBuyType;
import org.black_ixx.bossshop.managers.ItemNameManager;
import org.bukkit.inventory.ItemStack;

public class MailItem{
    
    private BSBuyType mItemType;
    /**
     * 过期物品
     * item-->ItemStack,money->int,point->int
     */
    private Object mItem;
    private String mSource;
    /**
     * 物品预览
     */
    private String mPreview=null;
    
    /**
     * 构造一个Mail物品
     * @param pItem 物品,允许Integer,List<ItemStack> ItemStack
     * @param pSaleType 物品类型,只支持Item,points,money
     * @param pCount 数量
     * @param pSource 来源,用于生成预览
     */
    public MailItem(Object pItem,BSBuyType pSaleType,int pCount,String pSource){
        this.mItem=pItem;
        this.mItemType=pSaleType;
        this.mSource=pSource;
        this.mPreview=this.resetItem(pCount);
    }
    
    protected String C(String pNode){
        return BossShop.getInstance().getLangManager().getNode(pNode);
    }
    
    /**
     * 创建邮件物品的预览
     * @param pCount 数量
     * @return 预览
     */
    protected String resetItem(int pCount){
        switch(this.mItemType){
        case Money:
            if(pCount>=1) this.mItem=((Integer)this.mItem)*pCount;
            this.mPreview=this.mItem+C("Word.Money");
            break;
        case Points:
            if(pCount>=1) this.mItem=((Integer)this.mItem)*pCount;
            this.mPreview=this.mItem+C("Word.Points");
            break;
        case Item:
            ItemStack tItem=null;
            if(this.mItem instanceof List<?>) tItem=((List<ItemStack>)this.mItem).get(0);
            else tItem=(ItemStack)this.mItem;
            this.mItem=tItem;
            if(pCount>=1) tItem.setAmount(tItem.getAmount()*pCount);
            String itemname=BossShop.getInstance().getManager(ItemNameManager.class).getDisPlayName(tItem);
            this.mPreview=tItem.getAmount()+C("Word.Count")+itemname;
            break;
        default:
            BossShop.getInstance().warn(C("CFG.UnsupportMailItemType")+"["+this.mItemType.name()+"]");
        }
        this.mPreview=(StringUtils.isEmpty(mSource)?C("Mail.FromUnknow")+": ":this.mSource+": ")+this.mPreview;
        return this.mPreview;
    }
    
    public String getPreview(){
        return this.mPreview;
    }
    
    public String getSource(){
        return this.mSource;
    }
    
    /**
     * 邮件列表中的物品统一为一件
     * @return
     */
    public Object getItem(){
        return this.mItem;
    }
    
    public BSBuyType getItemType(){
        return this.mItemType;
    }
    
}
