package com.yaohun.wheatwar.effect;

import com.yaohun.wheatwar.WheatWar;
import com.yaohun.wheatwar.game.Game;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.Sound;
import org.bukkit.entity.*;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.util.Vector;
import tools.CDTimeAPI;
import tools.RandomUtil;

import java.util.UUID;

public class SummonTheTNTRain {

    public static void apply(Game game, Player zhubo, String userName, int amount) {
        if (!game.isStarted()) {
            return;
        }
        // 获取主播的uuid
        UUID uuid = zhubo.getUniqueId();
        // 计时器key
        String timepieceKey = "SummonRainTNTEffect";
        startTimepieceEvent(game,zhubo,userName,timepieceKey);
        if(CDTimeAPI.isCD(uuid,timepieceKey)){
            long newTime = CDTimeAPI.getCD(uuid,timepieceKey) + (195L * amount);
            CDTimeAPI.setPlayerCD(uuid,timepieceKey,newTime);
        } else {
            CDTimeAPI.setPlayerCD(uuid,timepieceKey,195L * 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(game);
                spawnMobs(game);
                if(RandomUtil.getRandomInt(1,100) >= 70) {
                    zhubo.playSound(zhubo.getLocation(), "youbom", 1, 1);
                }
            }
        }.runTaskTimer(WheatWar.inst(), 0L, 5L);
        game.addTasks(task);
    }

    public static void spawnMobs(Game game) {
        Location location = game.getFarmlandRandom().clone().add(0,1,0);
        TNTPrimed entity = (TNTPrimed) game.getWorld().spawn(location, TNTPrimed.class);
        // 设置爆炸范围和伤害监听器
        entity.setYield(3.0F); // 爆炸范围（默认值为 4.0F）
        entity.setIsIncendiary(false); // 如果需要爆炸引起火焰，可设置为 true
        entity.setFuseTicks(40); // 设置2秒后爆炸（1秒=20 ticks）
        Location centralPoint = game.getCentralPoint();
        // 计算方向
        Vector direction = centralPoint.toVector().subtract(location.toVector()).normalize();
        // 设置初速度
        double initialVerticalVelocity = 0.4; // 向上抛的速度
        double horizontalVelocity = 1.35; // 控制水平抛出的力度
        // 创建一个向量，包括水平和垂直方向的速度
        Vector velocity = direction.multiply(horizontalVelocity).setY(initialVerticalVelocity);
        entity.setVelocity(velocity);
        new BukkitRunnable() {
            private int i = 0;
            @Override
            public void run() {
                if(i >= 15){
                    cancel();
                    return;
                }
                game.getWorld().spawnParticle(Particle.ELECTRIC_SPARK,entity.getLocation().clone().add(0,1,0),2);
                i++;
            }
        }.runTaskTimer(WheatWar.inst(),0,1L);
    }
}
