package org.black_ixx.bossshop.lottery;

import java.util.HashMap;

import org.black_ixx.bossshop.BossShop;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;

import cc.util.bossshop.pluginmodel.INeedConfig;

public class LotteryManager implements INeedConfig{

    public static final String CFG_MAIN="LotterySystem";
    public static final String CFG_ReflashBaseTicks="ReflashBaseTicks";
    public static final String CFG_ReflashLowMultiple="ReflashLowMultiple";
    public static final String CFG_RefalshFastCount="RefalshFastCount";
    public static final String CFG_ShowPrecent="ShowPrecent";
    public static int mReflashBaseTicks=5;
    public static int mReflashLowMultiple=4;
    public static int mRefalshFastCount=20;
    public static int mShowPrecent=34;
    private BossShop mPlugin;
    
    public LotteryManager(BossShop pPlugin){
        this.mPlugin=pPlugin;
        this.mPlugin.registerConfigModel(this);
    }
    
    @Override
    public void setConfig(){
        ConfigurationSection tSec=this.mPlugin.getConfigManager().getConfig().getConfigurationSection(CFG_MAIN);
        if(tSec==null) return;
        mReflashBaseTicks=tSec.getInt(CFG_ReflashBaseTicks,mReflashBaseTicks);
        if(mReflashBaseTicks<2) mShowPrecent=2;
        mReflashLowMultiple=tSec.getInt(CFG_ReflashLowMultiple,mReflashLowMultiple);
        if(mReflashLowMultiple<1) mReflashLowMultiple=1;
        mRefalshFastCount=tSec.getInt(CFG_RefalshFastCount,mRefalshFastCount);
        if(mRefalshFastCount<1) mRefalshFastCount=20;
        mShowPrecent=tSec.getInt(CFG_ShowPrecent,mShowPrecent);
        if(mShowPrecent<=0||mShowPrecent>50) mShowPrecent=34;
        this.mPlugin.debug("已经重载抽奖配置:"+mReflashBaseTicks+"|"+mReflashLowMultiple+"|"+mRefalshFastCount+"|"+mShowPrecent);
    }
    
    
    /**
     * 任务列表
     * <p>
     * 第一个参数为任务目标背包的持有者<br />
     * 第二个参数为Bukkit任务ID
     * </p>
     */
    private final static HashMap<LotteryHolder,Integer> mTasks=new HashMap<>();
    
    /**
     * 添加一个任务到列表中
     * @param pHolder 任务关联的holder
     * @param pTaskId Bukkit任务Id
     */
    public static void addTask(LotteryHolder pHolder,int pTaskId){
        LotteryManager.mTasks.put(pHolder,pTaskId);
    }
    
    /**
     * 尝试停止一个列表中的任务
     * @param pHolder 任务关联的holder
     */
    public static void stopTask(LotteryHolder pHolder){
        Integer tTaskId=LotteryManager.mTasks.remove(pHolder);
        if(tTaskId!=null){
            Bukkit.getScheduler().cancelTask(tTaskId);
        }
    }
    
    /**
     * 检查holder是否已经有关联的任务存在
     * @param pHolder 任务关联的holder
     * @return 是否存在
     */
    public static boolean existTask(LotteryHolder pHolder){
        return LotteryManager.mTasks.containsKey(pHolder);
    }

}
