package com.yutian.minerwar.effects;

import com.yutian.minerwar.MinerWar;
import com.yutian.minerwar.game.Game;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.EvokerFangs;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import tools.CDTimeAPI;

import java.util.UUID;

public class SummonTheEvokerFang {

    public static void apply(Game game, Player zhubo, String userName, int amount) {
        if (!game.isStarted()) {return;}
        // 获取主播的uuid
        UUID uuid = zhubo.getUniqueId();
        // 计时器key
        String timepieceKey = "SummonEvokerFangsEffect";
        startTimepieceEvent(game,zhubo,userName,timepieceKey);
        if(CDTimeAPI.isCD(uuid,timepieceKey)){
            long newTime = CDTimeAPI.getCD(uuid,timepieceKey) + (250L * amount);
            CDTimeAPI.setPlayerCD(uuid,timepieceKey,newTime);
        } else {
            CDTimeAPI.setPlayerCD(uuid,timepieceKey,250L * amount);
        }
    }

    public static void startTimepieceEvent(Game game,Player zhubo,String userName,String timepieceKey) {
        if (CDTimeAPI.isCD(zhubo.getUniqueId(), timepieceKey)) {
            return;
        }
        BukkitTask task = new BukkitRunnable() {
            @Override
            public void run() {
                if (!CDTimeAPI.isCD(zhubo.getUniqueId(), timepieceKey)) {
                    cancel();
                    return;
                }
                spawnMobs(zhubo,userName);
            }
        }.runTaskTimer(MinerWar.inst(), 0L, 5L);
        game.addTasks(task);
    }

    public static void spawnMobs(Player zhubo,String userName){
            Location location = zhubo.getLocation().clone();
            location.getWorld().spawnParticle(Particle.LAVA, location, 3, 1, 1, 1, 0.085);
            EvokerFangs entity = (EvokerFangs) location.getWorld().spawnEntity(location, EntityType.EVOKER_FANGS);
            entity.setCustomName("§c" + userName);
            entity.setCustomNameVisible(true);

    }
}
