package cc.util.bossshop;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.meta.ItemMeta;

public class FBukkit{
    
    private static String mPrefix="&c[CCLogger]";
    
    public static Player[] getOnlinePlayers(){
        Method tMethod=null;
        try{
            tMethod=Bukkit.class.getDeclaredMethod("getOnlinePlayers");
            Object tObject=tMethod.invoke(null);
            Player[] tPlayers=null;
            if(tObject instanceof Player[]) tPlayers=(Player[])tObject;
            else{
                Collection<Player> tcPlayers=(Collection<Player>)tObject;
                tPlayers=new Player[tcPlayers.size()];
                tcPlayers.toArray(tPlayers);
            }
            return tPlayers;
        }catch(Throwable exp){
            FBukkit.severe("获取在线玩家列表时发生了错误",exp);
            return new Player[0];
        }
    }
    
    public static ArrayList<String> getOnlinePlayersName(){
        ArrayList<String> playerNames=new ArrayList<>();
        for(Player sPlayer : FBukkit.getOnlinePlayers())
            playerNames.add(sPlayer.getName());
        return playerNames;
    }
    
    public static OfflinePlayer[] getOfflinePlayers(){
        Method tMethod=null;
        try{
            tMethod=Bukkit.class.getDeclaredMethod("getOfflinePlayers");
            Object tObject=tMethod.invoke(null);
            OfflinePlayer[] tPlayers=null;
            if(tObject instanceof OfflinePlayer[]) tPlayers=(OfflinePlayer[])tObject;
            else{
                Collection<OfflinePlayer> tcPlayers=(Collection<OfflinePlayer>)tObject;
                tPlayers=new OfflinePlayer[tcPlayers.size()];
                tcPlayers.toArray(tPlayers);
            }
            return tPlayers;
        }catch(Throwable exp){
            FBukkit.severe("获取离线玩家列表时发生了错误",exp);
            return new OfflinePlayer[0];
        }
    }
    
    public static ArrayList<String> getOfflinePlayersName(){
        ArrayList<String> playerNames=new ArrayList<>();
        for(OfflinePlayer sPlayer : FBukkit.getOfflinePlayers())
            playerNames.add(sPlayer.getName());
        return playerNames;
    }
    
    public static boolean isItemMetaEmpty(ItemMeta pMeat){
        Boolean result=(Boolean)ClassHelper.invokeMethod(pMeat,"isEmpty");
        if(result==null||result) return true;
        return false;
    }
    
    /**
     * 设置ClassHelper类日志输出前缀
     */
    private static void setLogPrefix(String pPrefix){
        if(StringUtils.isNotEmpty(pPrefix))
            FBukkit.mPrefix=pPrefix;
    }
    
    /**
     * 在发生异常时用于输出调试信息,或输出警告信息到控制台
     * @param pMsg 错误的提示消息
     */
    public static void severe(String pMsg){
        FBukkit.log(mPrefix+"&c[ERROR]",pMsg);
    }
    
    /**
     * 输出信息到控制台
     * @param pMsg 提示消息
     */
    public static void log(String pPrefix,String pMsg){
        if(StringUtils.isEmpty(pMsg)) return;
        if(StringUtils.isEmpty(pPrefix))
            pPrefix=FBukkit.mPrefix;
        String sendMessage=ChatColor.translateAlternateColorCodes('&',pPrefix+" "+pMsg);
        if(Bukkit.getServer()!=null){
            Bukkit.getConsoleSender().sendMessage(sendMessage);
        }else Logger.getLogger("minecraft").log(Level.SEVERE,sendMessage);
    }
    
    /**
     * 在发生异常时用于输出调试信息,或输出警告信息到控制台
     * @param msg 错误的提示消息
     * @param thrown 要抛出的异常
     */
    public static void severe(String msg,Throwable thrown){
        FBukkit.severe(mPrefix+"&c[ERROR]",msg,thrown);
    }
    
    /**
     * 输出调试信息
     * @param pPrefix 消息前缀
     * @param msg 错误的提示消息
     * @param thrown 要抛出的异常
     */
    public static void severe(String pPrefix,String msg,Throwable thrown){
        if(Bukkit.getServer()!=null){
            FBukkit.log(pPrefix,msg);
            String[] lines= org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace(thrown).split("(\r?\n)+");
            for(String sLine : lines)
                FBukkit.log(pPrefix,sLine);
        }else Logger.getLogger("minecraft").log(Level.SEVERE,pPrefix+msg,thrown);
    }
    
    
}
