from operator import itemgetter

from Code import Config
from Code.UI.Shape import Shape


class Shapes:

	def create_shape(self, start_square_pos: tuple, squares, type):
		return self._create_shape(start_square_pos, squares, Structures.shapes[type])

	def _get_size(self, points):
		x = max(max(points, key=itemgetter(0)))
		y = max(max(points, key=itemgetter(1)))
		return (x, y)

	def _create_shape(self, start_square_pos: tuple, squares, points):
		if self._check_bounderies(self._get_size(points), start_square_pos, squares):
			for point in points:
				squares[start_square_pos[0] + point[1]][start_square_pos[1] + point[0]].active = True
		return squares

	def _check_bounderies(self, bounderies, start_square_pos: tuple, field):
		print(bounderies)
		delta_start_pos_x = start_square_pos[0]
		delta_start_pos_y = start_square_pos[1]
		x_valid = (Config.GeneralConfig.fields_amount_x - 2 - int(delta_start_pos_x)) > bounderies[0]
		y_valid = (Config.GeneralConfig.fields_amount_y - 2 - int(delta_start_pos_y)) > bounderies[1]
		return x_valid and y_valid


class Structures:
	shapes = {Shape.GLIDER_LEFT_DOWN: [(0, 0), (1, 0), (2, 0), (2, 1), (1, 2)],
	          Shape.HORIZONTAL_GLIDER_LEFT: [(0, 0), (1, 0), (2, 0), (3, 0), (4, 1), (4, 3), (0, 1), (0, 2), (1, 3)],
	          Shape.HORIZONTAL_GLIDER_RIGHT: [(0, 1), (0, 3), (1, 0), (3, 0), (2, 0), (4, 0), (4, 1), (4, 2), (3, 3)],
	          Shape.GLIDER_RIGHT: [(0, 2), (1, 0), (1, 2), (2, 1), (2, 2)],
	          Shape.VERTICAL_GLIDER_RIGHT: [(1, 0), (3, 0), (0, 1), (0, 3), (0, 2), (0, 4), (1, 4), (2, 4), (3, 3)],
	          Shape.VERTICAL_GLIDER_LEFT: [(0, 0), (0, 1), (0, 2), (0, 3), (1, 4), (3, 4), (1, 0), (2, 0), (3, 1)]
	          
	          }
