package com.yaohun.mcbot.listsener;

import com.yaohun.mcbot.McBot;
import com.yaohun.mcbot.api.McBotAPI;
import com.yaohun.mcbot.config.Config;
import com.yaohun.mcbot.data.sql.SQLIO;
import com.yaohun.mcbot.event.BotFriendMessageEvent;
import com.yaohun.mcbot.event.BotGroupMessageEvent;
import com.yaohun.mcbot.util.CDTimeAPI;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;

import java.util.concurrent.TimeUnit;

public class FriendListener implements Listener {

    @EventHandler
    public void onGroupChat(BotFriendMessageEvent e) {
        long senderId = e.getSenderId();
        if(CDTimeAPI.isCD(senderId,"groupBotCd")){
            return;
        }
        CDTimeAPI.addPlayerCD(senderId,"groupBotCd",1000 * 5);
        String message = e.getMessage();
        if(message.contains("/功能") || message.contains("/菜单")) {
            String to_message = "/查询   - 查看绑定信息\n" +
                    "/在线  - 查看当前在线人数\n" +
                    "/延迟 [玩家]  - 网络延迟查看\n" +
                    "/强制下线  - 让账号在游戏中强制退出";
            e.getBotFriend().sendMessage(to_message);
        }else if(e.getMessage().equalsIgnoreCase("/在线")) {
            int onlineplayer = BungeeCord.getInstance().getOnlineCount();
            String msg = Config.getMessage("checkServerOnline").replace("%count%",String.valueOf(onlineplayer));
            e.getBotFriend().sendMessage(msg);
        }else if(message.contains("/查询")) {
            // 获取这个发送者的QQ号
            String userName = SQLIO.getUserName(String.valueOf(e.getSenderId()));
            if(userName == null){
                String msg = Config.getMessage("notBoundYet");
                e.getBotFriend().sendMessage(msg);
                return;
            }
            String msg = Config.getMessage("querySuccessful");
            e.getBotFriend().sendMessage(msg.replace("%user%", userName));
        }else if(e.getMessage().contains("/延迟 ")){
            // 获取玩家名字
            String playName = e.getMessage().replace("/延迟 ","").replace(" ","");
            ProxiedPlayer player = ProxyServer.getInstance().getPlayer(playName);
            if (player == null) {
                String msg = Config.getMessage("offlinePlayer").replace("%user%", playName);
                e.getBotFriend().sendMessage(msg);
                return;
            }
            String msg = Config.getMessage("checkPlayerPing").replace("%user%", playName);
            msg = msg.replace("%ping%",String.valueOf(player.getPing()));
            e.getBotFriend().sendMessage(msg);
        }else if(e.getMessage().contains("/强制下线")){
            // 获取这个发送者的QQ号
            String userName = SQLIO.getUserName(String.valueOf(e.getSenderId()));
            if(userName == null){
                String msg = Config.getMessage("unboundAccount");
                e.getBotFriend().sendMessage(msg);
                return;
            }
            ProxiedPlayer player = ProxyServer.getInstance().getPlayer(userName);
            if (player == null) {
                String msg = Config.getMessage("offlinePlayerError").replace("%user%", userName);
                e.getBotFriend().sendMessage(msg);
                return;
            }
            ProxyServer.getInstance().getScheduler().schedule(McBot.inst(),
                    new Runnable() {
                        public void run() {
                            player.disconnect("§c号主已进行强制下线");
                            String msg = Config.getMessage("kickOnlinPlayer").replace("%user%",userName);
                            e.getBotFriend().sendMessage(msg);
                        }
                    }, 1L, TimeUnit.SECONDS);
        }
    }
}
