package org.black_ixx.bossshop.managers;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.black_ixx.bossshop.BossShop;
import org.black_ixx.bossshop.core.BSBuy;
import org.black_ixx.bossshop.core.BSShop;
import org.black_ixx.bossshop.core.BSShopHolder;
import org.black_ixx.bossshop.events.BSDisplayItemEvent;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;

import cc.util.bossshop.FBukkit;

/**
 * 定制专属于某个玩家的商店
 * @author 聪聪
 *
 */
public class ShopCustomizer{

    private final static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
    private BossShop mPlugin;
    
    public ShopCustomizer(BossShop pPlugin){
        this.mPlugin=pPlugin;
    }
    
    private String C(String key,Player player){
        return BossShop.replaceParam(player,this.mPlugin.getLangManager().getNode(key));
    }

    /**
     * 根据指定的玩家,商店创建物品到指定的背包
     * @param pShop 商店
     * @param pPlayer 玩家
     * @param pInventory 背包
     * @param pPoss 要显示的物品的位置
     */
    public Inventory updateInventory(BSShop pShop,Player pPlayer,Inventory pInventory,Set<Integer> pPoss){
        if(!(pInventory.getHolder() instanceof BSShopHolder)) return pInventory;
        pInventory.clear();
        for(Integer pos : pPoss){
            BSBuy buy=pShop.getShopItem(pos);
            if(buy==null||!buy.disaplyToPlayer(pPlayer)) continue;
            ItemStack menu_item=pShop.getMenuItem(pos);
            if(!buy.isNeedEdit()){
                pInventory.setItem(pos,menu_item);
                continue;
            }
            pInventory.setItem(pos,createPersonalMenuItem(pPlayer,buy,menu_item));
        }
        return pInventory;
    }

    /**
     * 根据玩家信息,商品信息设置菜单物品Lore
     * <p>此函数只管设置lore,不会检测是否隐藏物品</p>
     * @param pPlayer 玩家
     * @param pBuy 商品
     * @param pMenuItem 菜单物品
     * @return 肯定不是null
     */
    public ItemStack createPersonalMenuItem(Player pPlayer,BSBuy pBuy,ItemStack pMenuItem){
        BSShop pShop=pBuy.getShop();
        BSDisplayItemEvent event=new BSDisplayItemEvent(pPlayer,pShop,pBuy);
        Bukkit.getPluginManager().callEvent(event);
        if(event.isCancelled()) return null;
        ItemMeta meta=pMenuItem.getItemMeta();
        //设置骷髅所有者
        if(meta instanceof SkullMeta){
            SkullMeta skullMeta=(SkullMeta)meta;
            if(skullMeta.hasOwner()) skullMeta.setOwner(BossShop.replaceParam(pPlayer,skullMeta.getOwner()));
        }
        //设置lore和物品名字
        if(meta.hasDisplayName())
            meta.setDisplayName(BossShop.replaceParam(pPlayer,meta.getDisplayName()));
        List<String> lore=meta.getLore();
        if(lore!=null){
            int co=0;
            for(String line : lore){
                lore.set(co,BossShop.replaceParam(pPlayer,line));
                co++;
            }
        }else lore=new ArrayList<String>();
        // 设置剩余数量,售卖时间
        int limit=pBuy.getLimit();
        int perLimit=pBuy.getPersonalLimit();
        Date start_t=pBuy.getStartTime();
        Date stop_t=pBuy.getStopTime();
        ArrayList<String> addlore=new ArrayList<>();
        String faillore=null;
        //设置剩余数量
        if(limit>-1){
            if(limit>0) addlore.add(C("Lore.GoodsStockLeft",pPlayer).replace("%numb%",limit+""));
            else faillore=C("Lore.AlreadySoldOut",pPlayer);
        }
        //设置个人剩余数量
        if(faillore==null&&perLimit>-1){
            int leftbuy=perLimit-this.mPlugin.getManager(RecordManager.class).getBuyRecord(pShop.getShopName(),pBuy.getName(),pPlayer.getUniqueId());
            if(leftbuy<0) leftbuy=0;
            if(leftbuy==0) faillore=C("Main.OutOfPersonalLimit",pPlayer);
            else addlore.add(C("Lore.EveryoneLimit",pPlayer).replace("%numb%",perLimit+"").replace("%left%",leftbuy+""));
        }
        //设置开始和结束时间
        if(faillore==null&&(start_t!=null||stop_t!=null)){
            Date date=new Date();
            if(start_t==null||date.after(start_t)){
                if(stop_t==null||date.before(stop_t)){
                    addlore.add(C("Lore.NowCanBuyGoods",pPlayer));
                    if(stop_t!=null) addlore.add(C("Lore.WillCloseInTime",pPlayer).replaceAll("%time%",getTimeString(stop_t)));
                }else faillore=C("Lore.GoodsOutOfDate",pPlayer);
            }else faillore=C("Lore.BuyIsNotTime",pPlayer).replaceAll("%time%",getTimeString(start_t));
        }
        if(faillore==null) lore.addAll(addlore);
        else lore.add(faillore);
        if(lore.size()!=0) meta.setLore(lore);
        if(!FBukkit.isItemMetaEmpty(meta)) pMenuItem.setItemMeta(meta);
        return pMenuItem;
    }

    private static String getTimeString(Date date){
        return sdf.format(date);
    }

}
