// Arup Guha
// 9/28/2026
// ShoeCollection class

import java.util.*;

public class ShoeCollection {

	private Shoe[] shoeList;
	private int maxShoes;
	private int slotsLeft;
	
	// Default constructor gives us 10 slots.
	public ShoeCollection() {
		shoeList = new Shoe[10];
		Arrays.fill(shoeList, null);
		maxShoes = 10;
		slotsLeft = 10;
	}
	
	// This constructs a ShoeCollection with a maximum of numSlots
	// number of shoes.
	public ShoeCollection(int numSlots) {
		shoeList = new Shoe[numSlots];
		maxShoes = numSlots;
		slotsLeft = numSlots;
	}
	
	// Returns the first empty slot.
	private int getEmptySlot() {
		
		// Start at 0, keep going until we hit null.
		int idx = 0;
		while (shoeList[idx] != null)
			idx++;
			
		// Here is our slot.
		return idx;
	}
	
	// Attempts to add newShoe to the collection.
	public boolean addShoe(Shoe newShoe) {
	
		// Can't do it.
		if (slotsLeft == 0) return false;
		
		// Find where this shoe goes.
		int newLoc = getEmptySlot();
		
		// Add it.
		shoeList[newLoc] = newShoe;
		slotsLeft--;
		return true;
	}
	
	// Returns -1 if there are no shoes in the collection. Otherwise, a random
	// shoe is chosen.
	public int getRndShoeIndex() {
		
		// No shoes to choose from!
		if (slotsLeft == maxShoes) return -1;
	
		// Use this object for random numbers.
		Random rndObj = new Random();
		
		// Initial selection.
		int idx = rndObj.nextInt(maxShoes);
		
		// Keep trying until we find some shoes!
		while (shoeList[idx] == null)
			idx = rndObj.nextInt(maxShoes);
			
		// Ta da!
		return idx;
	}
	
	// Attempts to walk numSteps using a randomly selected shoe from this
	// collection.
	public int walkRandom(int numSteps) {
		
		// Pick a random index.
		int idx = getRndShoeIndex();
		
		// This means you have no shoes :(
		if (idx == -1) return 0;
		
		// Get the actual number of steps walked and return it.
		int actual = shoeList[idx].walk(numSteps);
		return actual;
	}
	
	// Returns a string representation of this Shoe Collection.
	public String toString() {
		
		// Just show the shoes that exist and which slots they are in.
		String res = "";
		for (int i=0; i<shoeList.length; i++)
			if (shoeList[i] != null)
				res = res + "slot "+i+": "+shoeList[i].toString()+"\n";
			
		return res;
	}
	
	public Shoe removeAtIndex(int idx) {
		
		// There is no shoe here.
		if (shoeList[idx] == null) return null;
		
		// Store a reference to this object to return.
		Shoe tmp = shoeList[idx];
		
		// Set this reference to null.
		shoeList[idx] = null;
		
		// This needs to be updated.
		slotsLeft++;
		
		// Return it.
		return tmp;
	}
	
	// Removes all shoes in the collection with no tread left.
	public void cleanUpBadShoes() {
		
		// Go through the list.
		for (int i=0; i< shoeList.length; i++) {
			
			// Criteria to be removed.
			if (shoeList[i] != null && shoeList[i].noTreadLeft()) {
				shoeList[i] = null;
				slotsLeft++;
			}
		}
	}
	
	// Quick tests.
	public static void main(String[] args) {
		
		Shoe nike = new Shoe("Nike", "Blue", 129.99, 5000000);
		Shoe reebok = new Shoe("Reebok", "White", 119.95, 7000000);
		ShoeCollection mine = new ShoeCollection();
		mine.addShoe(nike);
		mine.addShoe(reebok);
		System.out.println(mine);
		
		// I would walk 15000 steps...
		for (int i=0; i<5; i++) {
			mine.walkRandom(15000);
			System.out.println(mine);
		}
		
		// Add a couple of shoes and print.
		mine.addShoe(new Shoe("Ascics", "Red", 89.99, 1000000));
		mine.addShoe(new Shoe("NewBalance", "Gray", 109.99, 200000));
		mine.addShoe(new Shoe("Hoka", "Green", 131.99, 300000));
		mine.addShoe(new Shoe("Adidas", "Black", 99.99, 1900000));
		System.out.println(mine);
		
		// Test remove at.
		Shoe tmp = mine.removeAtIndex(2);
		System.out.println(mine);
		System.out.println("I removed "+tmp+"\n");
		
		// Walk a bunch.
		Random rndObj = new Random();
		for (int i=0; i<120; i++)
			mine.walkRandom(10000+rndObj.nextInt(100000));
		System.out.println(mine);
		
		// Now test clean up.
		System.out.println("Perform clean out...\n");
		mine.cleanUpBadShoes();
		System.out.println(mine);
	}
}