// Arup Guha
// 9/27/2026
// Inventory class for COP 3330 Program 4 (Fall 2026)

import java.util.*;

class Inventory {

	final public static int TOTAL_ROWS = 26;
	final public static int REGION_ROWS = 13;
	final public static int REGION_COLS = 20;

	private Ticket[][] seats;
	private boolean[][] sold;
	private int numColSections;
	private int numRegions;
	private int numSold;
	private int totalRevenue;
	
	// Pre-condition: The dimension of prices is 2 by n, corresponding to an arena with
	//                26 rows and 20n seats, with sections numbered 1 to 2n. The first
	//                n sections are rows 1-13, and the last n sections are rows 14-26.
	//                prices[i][j] is the initial cost of every ticket in section n*i+j+1.
	// Post-condition: Initializes the inventory to be 26 rows and 20n columns of tickets,
	//                 set to the price indicated in the prices array, and setting no
	//                 tickets as sold.
	public Inventory(int[][] prices) {
	
		// Number of sections in a row.
		numColSections = prices[0].length;
		int totalCols = numColSections*REGION_COLS;
		
		// Allocate references for all of these Tickets.
		seats = new Ticket[TOTAL_ROWS][totalCols];
		
		// Set up sold array and number of regions.
		sold = new boolean[TOTAL_ROWS][totalCols];
		numSold = 0;
		totalRevenue = 0;
		numRegions = 2*numColSections;
		
		// Do some pattern matching to figure this out, but a succinct way to accurately create all tickets.
		for (int i=0; i<TOTAL_ROWS; i++)
			for (int j=0; j<totalCols; j++)
				seats[i][j] = new Ticket((char)('A'+i), j, prices[i/REGION_ROWS][j/REGION_COLS]);
	}
	
	/*** Getters ***/
	public int getNumSold() {
		return numSold;
	}
	
	public int getTotalRevenue() {
		return totalRevenue;
	}
	
	// Returns the number of available seats in the section with number secNum.
	public int numAvailable(int secNum) {
	
		// To be safe.
		if (secNum < 1 || secNum > numRegions) return 0;
		
		// Set up the starting indexes for both loops.
		int rowStart = REGION_ROWS*((secNum-1)/numColSections);
		int colStart = REGION_COLS*((secNum-1)%numColSections);
		
		// Add these up.
		int res = 0;
		for (int i=rowStart; i<rowStart+REGION_ROWS; i++)
			for (int j=colStart; j<colStart+REGION_COLS; j++)
				if (!sold[i][j])
					res++;
					
		return res;
	}
	
	// Returns true iff we can buy numTickets tickets from region number region.
	public boolean canBuy(int region, int numTickets) {

		// Find the number of tickets available in this region.
		int available = numAvailable(region);
		
		// Can't do it.
		if (numTickets <= 0 || available < numTickets)
			return false;

		// Otherwise we can.
		return true;
	}
	
	// Pre-condition: Should only be called if numTickets number of tickets can be bought from
	//                the region with the region number region.
	public void buySeats(int secNum, int numTickets) {
	
		// Set up the starting indexes for both loops.
		int rowStart = REGION_ROWS*((secNum-1)/numColSections);
		int colStart = REGION_COLS*((secNum-1)%numColSections);
		
		int thisSale = 0;
		
		// Go through the section.
		for (int i=rowStart; i<rowStart+REGION_ROWS; i++) {
			for (int j=colStart; j<colStart+REGION_COLS; j++) {
				
				// Sell it!
				if (!sold[i][j]) {
					sold[i][j] = true;
					numSold++;
					thisSale++;
					totalRevenue += seats[i][j].getFaceValue();
					System.out.println(seats[i][j]);
				}
				
				// Get out.
				if (thisSale == numTickets)
					break;
			}
			
			// Get out.
			if (thisSale == numTickets)
				break;
			
		}
	}
	
	// Returns a String representation of the current object.
	public String toString() {
		
		// The sample output has this, so this is where this goes.
		String res = "Here is the current seating chart:\n\n";
		
		// A bit annoying, but a typical "real world" task.
		String line = "  ";
		String pattern = "01234567890123456789 ";
		for (int i=0; i<seats[0].length/REGION_COLS; i++)
			line = line + pattern;
		res = res + line + "\n";
		
		// Do each row.
		for (int i=0; i<TOTAL_ROWS; i++) {
			
			// Start of line.
			line = (char)('A'+i)+" ";
			
			// Go through each ticket.
			for (int j=0; j<sold[i].length; j++) {
				
				// Print the appropriate character for this ticket.
				if (sold[i][j])
					line = line + "X";
				else
					line = line + "O";
				
				// When to add a space between regions.
				if (j%REGION_COLS == REGION_COLS-1)
					line = line + " ";
			}
			
			// We always do this.
			res = res + line + "\n";
			
			// If we are going to a new set of regions, we do this too.
			if (i == REGION_ROWS-1)
				res = res + "\n";
		}
		
		// Ta da!
		return res;
	}
}