package com.shady.bunny;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.ThreadLocalRandom;

public class Easter {

    public enum Location {HOUSE, TREE, SHED, FLOWER_BANK, GARDEN, GRASS}

    public record EasterEgg(long id, Location location){

        public EasterEgg {
            Objects.requireNonNull(location);
        }

        public static EasterEgg createRandom(){
            var rng = ThreadLocalRandom.current();
            var id = Math.abs(rng.nextLong());
            var values = Location.values();
            var location = values[rng.nextInt(0, values.length)];
            return new EasterEgg(id,location);
        }
    }

    public record Bag(List<EasterEgg> content){

        public Bag(List<EasterEgg> content) {
            Objects.requireNonNull(content);
            if (content.size()!=10){
                throw new IllegalArgumentException("A bag must be constructed from exactly 10 eggs");
            }
            this.content = List.copyOf(content);
        }
    }

    /**
     * Search for an easter egg.
     *
     * @return the easter egg
     * @throws InterruptedException if the thread is interrupted while looking for the egg
     */
    public static EasterEgg hunt() throws InterruptedException {
        Thread.sleep(200);
        return EasterEgg.createRandom();
    }

    /**
     * Create a bag from a list of exactly 10 easter eggs
     * @param content a list of exactly 10 easter eggs
     * @return
     * @throws InterruptedException
     */
    public static Bag bag(List<EasterEgg> content) throws InterruptedException {
        Objects.requireNonNull(content);
        if (content.size()!=10){
            throw new IllegalArgumentException("A bag must be constructed from exactly 10 eggs");
        }
        Thread.sleep(200);
        return new Bag(content);
    }

    /**
     * Sell a given bag
     * @param bag bag to be sold
     * @return price at which the bag was sold
     * @throws InterruptedException
     */
    public static int sell(Bag bag) throws InterruptedException {
        Objects.requireNonNull(bag);
        Thread.sleep(400);
        return Math.abs(bag.hashCode())%100;
    }
}