package com.yutian.minerwar.listener;

import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Color;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import tools.CDTimeAPI;
import tools.RandomUtil;

import java.util.UUID;

public class GameEffect implements Listener {

    @EventHandler
    public void onMove(PlayerMoveEvent e) {
        Player p = e.getPlayer();
        UUID uuid = p.getUniqueId();
        Location loc = p.getLocation();
        World world = loc.getWorld();
        if (CDTimeAPI.getCD(uuid, "angelicSanctuary") >= 1) {
            long milliseconds = CDTimeAPI.getCD(uuid, "angelicSanctuary");
            double seconds = (double) milliseconds / 1000;
            String message = "§6天使庇护剩余时间: §b" + String.format("%.1f", seconds) + "秒";
            p.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(message));
        } else if (CDTimeAPI.getCD(uuid, "supermining") >= 1) {
            long milliseconds = CDTimeAPI.getCD(uuid, "supermining");
            double seconds = (double) milliseconds / 1000;
            String message = "§6超级挖掘剩余时间: §b" + String.format("%.1f", seconds) + "秒";
            p.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(message));
        } else if (CDTimeAPI.getCD(uuid, "SummonArrowEffect") >= 1) {
            long milliseconds = CDTimeAPI.getCD(uuid, "SummonArrowEffect");
            int seconds = (int) milliseconds / 250;
            String message = "§6箭头剩余未发射: §b" + seconds + "支";
            p.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(message));
        } else if (CDTimeAPI.getCD(uuid, "SummonEvokerFangsEffect") >= 1) {
            long milliseconds = CDTimeAPI.getCD(uuid, "SummonEvokerFangsEffect");
            int seconds = (int) milliseconds / 250;
            String message = "§6地刺剩余数量: §b" + seconds + "个";
            p.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(message));
        } else if (CDTimeAPI.getCD(uuid, "bigWindill") >= 1) {
            long milliseconds = CDTimeAPI.getCD(uuid, "bigWindill");
            double seconds = (double) milliseconds / 1000;
            String message = "§6大风车倒计时: §b" + String.format("%.1f", seconds) + "秒";
            p.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(message));
        } else if (CDTimeAPI.getCD(uuid, "turnOffTheLight") >= 1) {
            long milliseconds = CDTimeAPI.getCD(uuid, "turnOffTheLight");
            double seconds = (double) milliseconds / 1000;
            String message = "§6致盲时刻倒计时: §b" + String.format("%.1f", seconds) + "秒";
            p.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(message));
        } else if (CDTimeAPI.getCD(uuid, "slowEffect") >= 1) {
            long milliseconds = CDTimeAPI.getCD(uuid, "slowEffect");
            double seconds = (double) milliseconds / 1000;
            String message = "§6迟缓药水倒计时: §b" + String.format("%.1f", seconds) + "秒";
            p.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(message));
        } else if (CDTimeAPI.getCD(uuid, "speedEffect") >= 1) {
            long milliseconds = CDTimeAPI.getCD(uuid, "speedEffect");
            double seconds = (double) milliseconds / 1000;
            String message = "§6加速药水倒计时: §b" + String.format("%.1f", seconds) + "秒";
            p.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(message));
        }
        // 设置颜色为红色
        Color color = Color.RED;
        int random = RandomUtil.getRandomInt(1, 100);
        if (random >= 75) {
            color = Color.OLIVE;
        } else if (random >= 50) {
            color = Color.AQUA;
        } else if (random >= 25) {
            color = Color.GREEN;
        }
        world.spawnParticle(Particle.REDSTONE, loc.add(0, 0.2, 0), 2, 0.1, 0.1, 0.1, new Particle.DustOptions(color, 1));
    }

}
