package com.yaohun.demonskills.util;

import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

import java.util.Random;

public class RandomUtil {

    public static boolean random(double d) {
        return d >= new Random().nextFloat();
    }

    public static int getRandomInt(int min, int max) {
        if (min == max) {
            return max;
        }
        Random r = new Random();
        int i = min < max ? min : max;
        int a = min < max ? max : min;
        return r.nextInt(a - i + 1) + i;
    }

    public static double getRandomDouble(double min, double max, int scl) {
        int pow = (int) Math.pow(10, scl);
        return Math.floor((Math.random() * (max - min) + min) * pow) / pow;
    }

    public static Location getRandomLocation(World world, double minX, double maxX, double minY, double maxY, double minZ, double maxZ) {
        double rx = getRandomDouble(minX, maxX, 3);
        double ry = getRandomDouble(minY, maxY, 3);
        double rz = getRandomDouble(minZ, maxZ, 3);
        return new Location(world, rx, ry, rz);
    }

    public static Location getRandomLocation(Entity player, double radius) {
        Location loc = player.getLocation();
        double minX = loc.getX() - radius;
        double maxX = loc.getX() + radius;
        double minY = loc.getY() - radius;
        double maxY = loc.getY() + radius;
        double minZ = loc.getZ() - radius;
        double maxZ = loc.getZ() + radius;
        double rx = getRandomDouble(minX, maxX, 1);
        double ry = getRandomDouble(minY, maxY, 1);
        double rz = getRandomDouble(minZ, maxZ, 1);
        return new Location(loc.getWorld(), rx, ry, rz);
    }

}
