package org.black_ixx.bossshop.core;

import org.black_ixx.bossshop.BossShop;
import org.black_ixx.bossshop.managers.ShopCustomizer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;

/**
 * used by Inventory of BSShop
 */

public class BSShopHolder implements InventoryHolder{

    protected BossShop mPlugin;
    protected final BSShop mShop;
    protected final Player mOwner;
    protected Inventory mInv;

    public BSShopHolder(BossShop pPlugin,Player pOwner,BSShop pShop){
        this.mPlugin=pPlugin;
        this.mOwner=pOwner;
        this.mShop=pShop;
    }

    /**
     * 获取holder所持有的背包
     */
    @Override
    public Inventory getInventory(){
        return mInv;
    }

    /**
     * 设置holder的背包,必须调用
     * @param pInv 要设置成的背包
     */
    public void setInventory(Inventory pInv){
        this.mInv=pInv;
    }

    /**
     * 获取背包内的物品
     * <p>
     * 函数内部已经做过过滤,对于不符合条件的物品一律返回null
     * </p>
     * @param pPos 位置
     * @return 如果不存在则为null
     */
    public BSBuy getShopItem(int pPos){
        return mShop.getShopItem(pPos);
    }

    /**
     * 获取holder所关联的商店
     */
    public BSShop getShop(){
        return mShop;
    }
    
    /**
     * 获取holder所关联的玩家
     */
    public Player getOwner(){
        return this.mOwner;
    }

    /**
     * 将定制的物品放入到背包中
     * @return 如果商店不存在false,否则true
     */
    public boolean updateInventory(){
        if(!this.mShop.isShopValid()){
            this.mInv.clear();
            this.mOwner.closeInventory();
            return false;
        }
        BossShop.getInstance().getManager(ShopCustomizer.class).updateInventory(this.mShop,this.mOwner,this.mInv,this.mShop.getMenuItemSet());
        return true;
    }

}
