package me.trouper.sentinel.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * Randomize items from a list
 * @param <T> list of?
 */
public final class RandomUtils<T> {

    public static Date getDate(long id) throws ParseException {
        String dateString = String.valueOf(id);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return dateFormat.parse(dateString);
    }

    public static long generateID() {
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String formattedDate = dateFormat.format(now);
        long id = Long.parseLong(formattedDate);

        return id;
    }
    
    

    private final List<T> array;

    /**
     * From array list
     * @param array list
     */
    public RandomUtils(List<T> array) {
        this.array = array;
    }

    /**
     * From set
     * @param array set
     */
    public RandomUtils(Set<T> array) {
        this.array = new ArrayList<>(array);
    }

    /**
     * From array
     * @param array array
     */
    public RandomUtils(T[] array) {
        this.array = List.of(array);
    }

    /**
     * Pick random from the array
     * @return random of list of?
     */
    public T pickRand() {
        return array.get(rand(array.size() - 1));
    }

    /**
     * Generates a random integer from 1 to (max)
     * @param max max value
     * @return random
     */
    public static int rand(int max) {
        if (max <= 0) throw new IllegalArgumentException("max cannot be less than 1!");
        return (int) Math.ceil(Math.random() * max);
    }

    /**
     * Generates a random integer from (min) to (max)
     * @param min min value
     * @param max max value
     * @return random
     */
    public static int rand(int min, int max) {
        if (max <= 0 || min <= 0) throw new IllegalArgumentException("max or min cannot be less than 1!");
        if (max <= min) throw new IllegalArgumentException("max cannot be less than or equal to min!");
        return min + (int) Math.floor(Math.random() * (max - min + 1));
    }
}
