package org.black_ixx.bossshop.points;

import org.black_ixx.bossshop.BossShop;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;

import cc.util.bossshop.pluginmodel.INeedConfig;
import cc.util.bossshop.pluginmodel.INeedReload;

public class PointsManager extends IPointsAPI implements INeedConfig,INeedReload{

    private IPointsAPI pa;
    private PointsPlugin mPointPlugin;
    private String mPointsPlugin="auto-detect";

    public enum PointsPlugin{
        PLAYERPOINTS,COMMANDPOINTS,ENJIN_MINECRAFT_PLUGIN,POINTSAPI,CUSTOM;

        private String name;

        public void setCustom(String name){
            this.name=name;
        }

        public String getCustom(){
            return name;
        }
    }

    /**
     * 点券管理系统
     */
    public PointsManager(BossShop pPlugin){
        super(pPlugin,"");
        this.mPlugin.registerConfigModel(this);
        this.mPlugin.registerReloadModel(this);
    }

    public String getPluginName(){
        return this.mPointPlugin==null?"":this.mPointPlugin.name();
    }
    
    @Override
    public void setConfig(){
        this.mPointsPlugin=this.mPlugin.getConfigManager().getConfig().getString("PointsPlugin",this.mPointsPlugin);
    }
    
    public boolean reloadConfig(){
        mPointPlugin=null;
        if(mPointsPlugin!=null){
            if(Bukkit.getPluginManager().getPlugin("PlayerPoints")!=null){
                mPointPlugin=PointsPlugin.PLAYERPOINTS;
            }
            if(Bukkit.getPluginManager().getPlugin("CommandPoints")!=null){
                if(mPointPlugin==null||mPointsPlugin.equalsIgnoreCase("CommandPoints"))
                    mPointPlugin=PointsPlugin.COMMANDPOINTS;
            }
            if(Bukkit.getPluginManager().getPlugin("PointsAPI")!=null){
                if(mPointPlugin==null||mPointsPlugin.equalsIgnoreCase("PointsAPI"))
                    mPointPlugin=PointsPlugin.POINTSAPI;
            }
            if(Bukkit.getPluginManager().getPlugin("EnjinMinecraftPlugin")!=null){
                if(mPointPlugin==null||mPointsPlugin.equalsIgnoreCase("EnjinMinecraftPlugin"))
                    mPointPlugin=PointsPlugin.ENJIN_MINECRAFT_PLUGIN;
            }
            if(PointsAPI.get(mPointsPlugin)!=null){
                mPointPlugin=PointsPlugin.CUSTOM;
                mPointPlugin.setCustom(mPointsPlugin);
            }
        }
        if (mPointPlugin==null) {
            this.mPlugin.severe("未找到任何支持的点券插件");
            pa = new FailedPointsAPI(this.mPlugin);
            return false;
        }else if(mPointsPlugin.equalsIgnoreCase("auto-detect")){
            this.mPlugin.info("自动选择"+mPointPlugin.toString()+"点券插件");
        }else if(!mPointPlugin.toString().equalsIgnoreCase(mPointsPlugin)){
            this.mPlugin.severe("未找到配置文件中指定的"+mPointsPlugin+"点券插件,但是发现了支持的点券插件"+mPointPlugin.toString()+",将此插件作为默认");
        }else{
            this.mPlugin.info("自定义点券插件为 " + mPointPlugin.name() + ".");
        }
        switch (mPointPlugin) {
        case COMMANDPOINTS:
            if (Bukkit.getPluginManager().getPlugin("CommandPoints") == null) {
                this.mPlugin.severe("You defined CommandPoints as the Points Plugin... BUT IT WAS NOT FOUND?! Please download it at Bukkit.org!");
                return false;
            }
            pa = new CommandPointsAPI(this.mPlugin);
            return false;
        case ENJIN_MINECRAFT_PLUGIN:
            if (Bukkit.getPluginManager().getPlugin("EnjinMinecraftPlugin") == null) {
                this.mPlugin.severe("You defined Enjin Minecraft Plugin as the Points Plugin... BUT IT WAS NOT FOUND?! Please download it at Bukkit.org!");
                return false;
            }
            pa = new EnjinPointsAPI(this.mPlugin);
            return false;
        case PLAYERPOINTS:
            if (Bukkit.getPluginManager().getPlugin("PlayerPoints") == null) {
                this.mPlugin.severe("You defined PlayerPoints as the Points Plugin... BUT IT WAS NOT FOUND?! Please download it at Bukkit.org!");
                return false;
            }
            pa = new PlayerPointsAPI(this.mPlugin);
            return false;
        case POINTSAPI:
            if (Bukkit.getPluginManager().getPlugin("PointsAPI") == null) {
                this.mPlugin.severe("You defined PointsAPI as the Points Plugin... BUT IT WAS NOT FOUND?! Please download it at Bukkit.org!");
                return false;
            }
            pa = new PointsAPIPlugin(this.mPlugin);
            return false;
        case CUSTOM:
            IPointsAPI customPoints = PointsAPI.get(mPointPlugin.getCustom());
            if (customPoints != null) {
                pa = customPoints;
                return false;
            }    
            break;
        }
        this.mPlugin.warn("PlayerPoints/CommandPoints was not found... You need one of those plugins if you want this plugin to work with Points! Get PlayerPoints here: http://dev.bukkit.org/server-mods/playerpoints/");
        pa = new FailedPointsAPI(this.mPlugin);
        return true;
    }

    public int getPoints(OfflinePlayer player){
        return pa.getPoints(player);
    }

    public int setPoints(OfflinePlayer player,int points){
        return pa.setPoints(player,points);
    }

    public int givePoints(OfflinePlayer player,int points){
        return pa.givePoints(player,points);
    }

    public int takePoints(OfflinePlayer player,int points){
        return pa.takePoints(player,points);
    }



}
