import pygame
import math

cell_size = 5

grid_x_count = 70
grid_y_count = 50

grid = []

for y in range(grid_y_count):
    grid.append([])
    for x in range(grid_x_count):
        grid[y].append(False)

def update():
    global selected_x
    global selected_y

    mouse_x, mouse_y = pygame.mouse.get_pos()
    selected_x = min(math.floor(mouse_x / cell_size), grid_x_count - 1)
    selected_y = min(math.floor(mouse_y / cell_size), grid_y_count - 1)

    if pygame.mouse.get_pressed()[0]:
        grid[selected_y][selected_x] = True

def on_key_down():
    global grid
    
    next_grid = []
    
    for y in range(grid_y_count):
        next_grid.append([])
        for x in range(grid_x_count):
            next_grid[y].append(True)
    
    grid = next_grid

# Temporary
def on_mouse_down(pos, button):
    if button == mouse.RIGHT:
        neighbor_count = 0

        print('Finding neighbors of grid[' +
            str(selected_y) + '][' + str(selected_x) + ']')

        for dy in range(-1, 2):
            for dx in range(-1, 2):

                print(' Checking grid[' +
                    str(selected_y + dy) + '][' + str(selected_x + dx) + ']')

                if (not (dy == 0 and dx == 0)
                    and 0 <= (selected_y + dy) < grid_y_count
                    and 0 <= (selected_x + dx) < grid_x_count
                    and grid[selected_y + dy][selected_x + dx]):

                    print('  Neighbor found')
                    neighbor_count += 1

        print('Total neighbors: ' + str(neighbor_count))

def draw():
    screen.fill((255, 255, 255))

    for y in range(grid_y_count):
        for x in range(grid_x_count):
            cell_draw_size = cell_size - 1

            if x == selected_x and y == selected_y:
                color = (0, 255, 255)
            elif grid[y][x]:
                color = (255, 0, 255)
            else:
                color = (220, 220, 220)

            screen.draw.filled_rect(
                Rect(
                    (x * cell_size, y * cell_size),
                    (cell_draw_size, cell_draw_size)
                ),
                color=color
            )