package com.yaohun.wheatwar.effect;

import com.yaohun.wheatwar.WheatWar;
import com.yaohun.wheatwar.game.Game;
import net.minecraft.world.entity.EntityInsentient;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Particle;
import org.bukkit.attribute.Attribute;
import org.bukkit.block.Block;
import org.bukkit.block.data.Ageable;
import org.bukkit.block.data.BlockData;
import org.bukkit.craftbukkit.v1_18_R2.entity.CraftLivingEntity;
import org.bukkit.entity.*;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.util.Vector;
import tools.CDTimeAPI;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

public class SummonTheZombie {

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

    public static HashMap<UUID, Integer> zombieKillMap = new HashMap<>(); // 村民 正在执行的任务 需前往的地方

    public static void spawnMobs(Game game,String userName) {
        Location location = game.getFarmlandRandom().clone().add(0,1,0);
        Zombie entity = (Zombie) location.getWorld().spawnEntity(location, EntityType.ZOMBIE);
        entity.setCollidable(false);
        entity.setCustomNameVisible(true);
        entity.setCustomName("§9" + userName+"[§e"+3+"§9]");
        zombieKillMap.put(entity.getUniqueId(),3);
        entity.setBaby(false);
        entity.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED).setBaseValue(0.4);
        entity.getAttribute(Attribute.GENERIC_FOLLOW_RANGE).setBaseValue(50);
        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() {
            @Override
            public void run() {
                if(entity.isDead()){
                    cancel();
                    return;
                }
                entity.setCustomName("§9" + userName+"[§e"+zombieKillMap.get(entity.getUniqueId())+"§9]");
            }
        }.runTaskTimer(WheatWar.inst(), 20L,10L);
    }
}
