package org.black_ixx.bossshop.listeners;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.UUID;

import org.black_ixx.bossshop.BossShop;
import org.black_ixx.bossshop.core.BSBuy;
import org.black_ixx.bossshop.core.BSEnums;
import org.black_ixx.bossshop.core.BSShopHolder;
import org.black_ixx.bossshop.lottery.LotteryHolder;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.Event.Result;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.inventory.InventoryType.SlotType;
import org.bukkit.inventory.ItemStack;

import cc.util.bossshop.pluginmodel.IClearAble;

public class InventoryListener implements Listener,IClearAble{

    private BossShop mPlugin;
    private long mLastClear=0;
    private long mClearDis=3600000;
    private HashMap<UUID,Long> mWait=new HashMap<>();

    public InventoryListener(BossShop pPlugin){
        this.mPlugin=pPlugin;
        this.mPlugin.getServer().getPluginManager().registerEvents(this,this.mPlugin);
        this.mPlugin.registerClearModel(this);
    }

    @Override
    public boolean clearStatus(){
        this.mWait.clear();
        return true;
    }

    /**
     * 在一定的时间后清理掉无用的等待列表
     */
    private void clearWaitList(){
        if((this.mLastClear+this.mClearDis)>System.currentTimeMillis()) return;
        for(Iterator<Entry<UUID,Long>> it=this.mWait.entrySet().iterator();it.hasNext();){
            if((it.next().getValue()+this.mPlugin.getConfigManager().getCoolTime()*1000)<System.currentTimeMillis()) it.remove();
        }
        this.mLastClear=System.currentTimeMillis();
    }

    @EventHandler
    public void closeShop(InventoryCloseEvent event){
        if(!(event.getInventory().getHolder() instanceof BSShopHolder)) return;
        if(!(event.getPlayer() instanceof Player)) return;
        Player tPlayer=(Player)event.getPlayer();
        BSShopHolder bsHolder=(BSShopHolder)event.getInventory().getHolder();
        this.mPlugin.send(tPlayer,bsHolder.getShop().getLeaveMessage());
        if(bsHolder instanceof LotteryHolder){
            LotteryHolder ltHolder=(LotteryHolder)bsHolder;
            ltHolder.closeLottery();
        }
    }

    public void onDargItem(InventoryDragEvent pEvent){
        
    }
    
    @EventHandler(priority=EventPriority.HIGH)
    public void purchase(InventoryClickEvent event){
        if(!(event.getWhoClicked() instanceof Player)) return;
        if(!(event.getInventory().getHolder() instanceof BSShopHolder)) return;

        BSShopHolder holder=(BSShopHolder)event.getInventory().getHolder();
        event.setCancelled(true);
        event.setResult(Result.DENY);

        Player pPlayer=(Player)event.getWhoClicked();
        this.clearWaitList();
        try{
            ItemStack item=event.getCurrentItem();
            if(item==null||item.getType()==Material.AIR) return;
            if(event.getSlotType()==SlotType.QUICKBAR) return;
            BSBuy buy=holder.getShopItem(event.getRawSlot());
            if(buy==null) return;
            if(buy.getBuyType()==BSEnums.BSBuyType.Nothing) return;
            // 检查是否很快的点击
            Long time=this.mWait.put(pPlayer.getUniqueId(),System.currentTimeMillis());
            if(time!=null&&(time+this.mPlugin.getConfigManager().getCoolTime())>System.currentTimeMillis()){
                this.mPlugin.send(pPlayer,this.mPlugin.getLangManager().get("Main.YouClickTooQuick"));
                return;
            }
            
            if(!buy.canBuy(pPlayer,holder)) return;
            buy.stockOut(pPlayer,holder);

        }catch(Throwable exp){
            this.mPlugin.severe(exp.getMessage(),exp);
            this.mPlugin.send(pPlayer,this.mPlugin.getLangManager().get("Main.ErrorHappend"));
        }
    }

}
