package vg.civcraft.mc.civmodcore.inventorygui.components;

import java.util.function.IntPredicate;

public final class SlotPredicates {

	private static final int ROW_LENGTH = 9;

	private SlotPredicates() {
	}

	public static IntPredicate slots(int count) {
		Counter counter = new Counter(count);
		return counter::isValid;
	}

	public static IntPredicate rows(int rows) {
		return rows(rows, 9);
	}

	public static IntPredicate rows(int rows, int rowLength) {
		return slots(rows * rowLength);
	}

	public static IntPredicate offsetRectangle(int rows, int columns, int verticalOffset, int horizontalOffset,
			int rowLength) {
		OffsetSlotter slotter = new OffsetSlotter(verticalOffset, horizontalOffset, rows, columns, rowLength);
		return slotter::isValid;
	}

	public static IntPredicate offsetRectangle(int rows, int columns, int verticalOffset, int horizontalOffset) {
		return offsetRectangle(rows, columns, verticalOffset, horizontalOffset, ROW_LENGTH);
	}

	public static IntPredicate rectangle(int rows, int columns, int rowLength) {
		return offsetRectangle(rows, columns, 0, 0, rowLength);
	}

	public static IntPredicate rectangle(int rows, int columns) {
		return rectangle(rows, columns, ROW_LENGTH);
	}

	private static class OffsetSlotter {

		private int lowerHorizontalBound;
		private int lowerVerticalBound;
		private int upperHorizontalBound;
		private int upperVerticalBound;
		private int rowLength;

		OffsetSlotter(int offSetRow, int offSetColumn, int sizeRows, int sizeColumns, int rowLength) {
			this.lowerHorizontalBound = offSetColumn;
			this.upperHorizontalBound = offSetColumn + sizeColumns - 1;
			this.lowerVerticalBound = offSetRow;
			this.upperVerticalBound = offSetRow + sizeRows - 1;
			this.rowLength = rowLength;
		}

		boolean isValid(int index) {
			int row = index / rowLength;
			int column = index % rowLength;
			return column >= lowerHorizontalBound && column <= upperHorizontalBound && row >= lowerVerticalBound
					&& row <= upperVerticalBound;
		}

	}

	private static class Counter {
		private int count;

		Counter(int count) {
			this.count = count;
		}

		public boolean isValid(int index) {
			return count-- > 0;
		}
	}

}

@@ -361,20 +361,22 @@
 ows(int 
-rows
+height
 ) %7B%0A%09%09re
@@ -385,20 +385,22 @@
 rn rows(
-rows
+height
 , 9);%0A%09%7D
@@ -434,28 +434,30 @@
 te rows(int 
-rows
+height
 , int rowLen
@@ -478,20 +478,22 @@
 n slots(
-rows
+height
  * rowLe
@@ -555,67 +555,39 @@
 int 
-rows, int columns, int verticalOffset, int horizontalOffset
+height, int width, int y, int x
 ,%0A%09%09
@@ -652,55 +652,27 @@
 ter(
-verticalOffset, horizontalOffset, rows, columns
+y, x, height, width
 , ro
@@ -764,143 +764,87 @@
 int 
-rows, int columns, int verticalOffset, int horizontalOffset) %7B%0A%09%09return offsetRectangle(rows, columns, verticalOffset, horizontalOffset
+height, int width, int y, int x) %7B%0A%09%09return offsetRectangle(height, width, y, x
 , RO
@@ -900,33 +900,33 @@
 gle(int 
-rows, int columns
+height, int width
 , int ro
@@ -961,29 +961,29 @@
 ctangle(
-rows, columns
+height, width
 , 0, 0, 
@@ -1044,25 +1044,25 @@
 int 
-rows, int columns
+height, int width
 ) %7B%0A
@@ -1084,21 +1084,21 @@
 gle(
-rows, columns
+height, width
 , RO
@@ -1341,66 +1341,39 @@
 int 
-offSetRow, int offSetColumn, int sizeRows, int sizeColumns
+y, int x, int height, int width
 , in
@@ -1418,28 +1418,17 @@
 Bound = 
-offSetColumn
+x
 ;%0A%09%09%09thi
@@ -1456,34 +1456,17 @@
 d = 
-offSetColumn + sizeColumns
+x + width
  - 1
@@ -1496,25 +1496,17 @@
 Bound = 
-offSetRow
+y
 ;%0A%09%09%09thi
@@ -1532,28 +1532,18 @@
 d = 
-offSetRow + sizeRows
+y + height
  - 1

Rename SlotPredicate internals for clarity (why, I hate this file)