package org.black_ixx.bossshop.managers;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.black_ixx.bossshop.BossShop;
import org.black_ixx.bossshop.core.BSMultiplier;
import org.black_ixx.bossshop.core.BSEnums.BSPriceType;
import org.bukkit.entity.Player;

import cc.util.bossshop.config.CommentedYamlConfig;
import cc.util.bossshop.pluginmodel.INeedConfig;

public class MultiplierHandler implements INeedConfig{

    private BossShop plugin;
    private Set<BSMultiplier> multipliers=new HashSet<BSMultiplier>();

    /**
     * 会员价格系统
     * @param plugin
     */
    public MultiplierHandler(BossShop plugin){
        this.plugin=plugin;
        this.plugin.registerConfigModel(this);
    }
    

    @Override
    public void setConfig(){
        CommentedYamlConfig tConfig=this.plugin.getConfigManager().getConfig();
        if(!tConfig.getBoolean("MultiplierGroups.Enabled")) return;
        List<String> lines=tConfig.getStringList("MultiplierGroups.List");
        if(lines==null) return;
        multipliers.clear();
        for(String s : lines){
            BSMultiplier m=new BSMultiplier(plugin,s);
            if(m.isValid()){
                multipliers.add(m);
            }
        }
    }

    public double calculateWithMultiplier(Player p,BSPriceType type,double d){
        for(BSMultiplier m : multipliers){
            if(m.getType()==type){
                if(m.hasPermission(p)){
                    d=d*m.getMultiplier();
                }
            }
        }
        return d;
    }

    public int calculateWithMultiplier(Player p,BSPriceType type,int d){
        double x=d;
        for(BSMultiplier m : multipliers){
            if(m.getType()==type){
                if(m.hasPermission(p)){
                    x=x*m.getMultiplier();
                }
            }
        }
        return (int)x;
    }

    public Set<BSMultiplier> getMultipliers(){
        return multipliers;
    }

}
