package com.yutian.minerwar.effects;

import com.yutian.minerwar.MinerWar;
import com.yutian.minerwar.game.Game;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.entity.Bee;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import tools.RandomUtil;

public class SummonTheBee {
    public static void apply(Game game, Player zhubo, String userName, int amount) {
        if (!game.isStarted()) {return;}
        if(amount >= 200){
            // 获取掉落次数
            int forA = 10 + (amount / 50);
            // 获取每次掉落数量
            int forB = (int) (amount / forA * 0.7);
            for (int i = 0; i < forA;i++){
                Bukkit.getScheduler().runTaskLater(MinerWar.inst(), () -> {
                    for (int a = 0; a < forB;a++) {
                        spawnMobs(zhubo,userName);
                    }
                    zhubo.playSound(zhubo.getLocation(), "huh", 1.0F, 1.0F);
                },(long) i*5);
            }
        }else if(amount >= 120){
            for (int i = 0; i < 20;i++){
                Bukkit.getScheduler().runTaskLater(MinerWar.inst(), () -> {
                    for (int ai = 0; ai < 6;ai++) {
                        spawnMobs(zhubo,userName);
                    }
                    zhubo.playSound(zhubo.getLocation(), "huh", 1.0F, 1.0F);
                },(long) i*5);
            }
        }else if(amount >= 30){
            for (int i = 0; i < 10;i++){
                Bukkit.getScheduler().runTaskLater(MinerWar.inst(), () -> {
                    for (int ai = 0; ai < 3;ai++) {
                        spawnMobs(zhubo,userName);
                    }
                    zhubo.playSound(zhubo.getLocation(), "huh", 1.0F, 1.0F);
                },(long) i*5);
            }
        }else {
            for (int i = 0; i < amount; i++) {
                spawnMobs(zhubo,userName);
            }
            zhubo.playSound(zhubo.getLocation(), "huh", 1.0F, 1.0F);
        }
    }

    public static void spawnMobs(Player zhubo,String userName){
        Location location = zhubo.getLocation().add(0,3,0);
        location.getWorld().spawnParticle(Particle.FIREWORKS_SPARK, location, 2, 1, 1, 1, 0.085);
        Bee entity = (Bee) location.getWorld().spawnEntity(location, EntityType.BEE);
        entity.setTarget(zhubo);
        entity.setHealth(1);
        entity.setCustomName("§c" + userName);
        entity.setCustomNameVisible(true);
        Vector vector = new Vector(RandomUtil.getRandomDouble(-1,1,1), RandomUtil.getRandomDouble(-1,1,1),RandomUtil.getRandomDouble(-1,1,1));
        entity.setVelocity(vector);
    }
}
