package de.steamwar.lobby.particle;

import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.entity.Player;

import java.util.Random;
import java.util.function.Consumer;

public interface ParticleElement {
    Random RANDOM = new Random();

    default Color randomColor() {
        return Color.fromRGB(RANDOM.nextInt(255), RANDOM.nextInt(255), RANDOM.nextInt(255));
    }

    default float randomSize() {
        return RANDOM.nextFloat() / 2 + 1;
    }

    default Particle.DustOptions randomParticleDust() {
        return new Particle.DustOptions(randomColor(), randomSize());
    }

    default void display(Location location, Player root, boolean onlySelf, boolean onlyOther, Consumer<Player> consumer) {
        if (onlySelf) {
            consumer.accept(root);
            return;
        }
        Bukkit.getOnlinePlayers().forEach(player -> {
            if (onlyOther && player == root) {
                return;
            }
            int viewDistance = player.getClientViewDistance() * 16;
            if (location.distanceSquared(player.getLocation()) <= viewDistance * viewDistance) {
                consumer.accept(player);
            }
        });
    }

    default void aggregateAttributes(ParticleData particleData) {
    }

    default ParticleTickType tickType() {
        return ParticleTickType.MOVE;
    }

    void tick(ParticleTickData particleTickData);
}
