import pygame as pg
from math import pi
from modules import Book, GameOver


class Swamp:
    def __init__(self):
        self.left_book = None
        self.down_book = None
        self.right_book = None
        self.top_book = None
        self.top_area = None
        self.left_area = None
        self.y = None
        self.x = None
        self.width = 13
        self.height = 8
        self.board = [[0] * 13 for _ in range(8)]  # Матрица
        self.cell_size = 30

        self.book_use = False  # Если self.book_use является True, то активируется класс Book
        self.book = Book.Book()
        self.frogs = 3  # кол-во лягушек
        self.game_over = GameOver.GameOver()

    def render(self, screen):  # Рисование поля, книги и болота
        myimage = pg.image.load("styles/background.jpg")
        rect = screen.get_rect()
        x = rect[2]
        y = rect[3]
        scaled_image = pg.transform.smoothscale(myimage, (x, y))
        screen.blit(scaled_image, (0, 0))

        pg.draw.rect(screen, (255, 255, 255), (630, 400, 50, 60))  # книга
        pg.draw.rect(screen, (132, 7, 8), (630, 400, 50, 55))
        pg.draw.rect(screen, (98, 0, 15), (630, 400, 5, 60))
        pg.draw.line(screen, (231, 113, 0), [640, 420], [675, 420], 3)

        for row in range(self.width):  # Клетчатое поле
            for col in range(self.height):
                self.x = self.left_area + row * self.cell_size
                self.y = self.top_area + col * self.cell_size
                if self.board[col][row] == 0:
                    pg.draw.rect(screen, (100, 100, 100),
                                 (self.x, self.y, self.cell_size, self.cell_size), 1)
                elif self.board[col][row] == 1:
                    pg.draw.rect(screen, (252, 232, 131), (self.x, self.y, self.cell_size, self.cell_size))

        if self.book_use is True:  # При нажатии на книгу рисуется небольшой экран с информацией
            from modules.Mathematics import recalculation_events, get_random_event
            event = get_random_event()
            print(event)
            recalculation_events(event["well"])
            self.book.info(screen, event)

        if self.frogs == 0:  # Если все лягушки вымрут, то открывается окно с Game over
            self.game_over.end(screen)

    def set_view(self, left, top, cell_size):
        self.left_area = left
        self.top_area = top
        self.cell_size = cell_size

    def set_book(self, top, right, down, left):  # Координаты сторон книги
        self.top_book = top
        self.right_book = right
        self.down_book = down
        self.left_book = left

    def get_cell(self, mouse_pos):
        board_width = self.width * self.cell_size
        board_height = self.height * self.cell_size
        if self.left_area < mouse_pos[0] < self.left_area + board_width:
            if self.top_area < mouse_pos[1] < self.top_area + board_height:
                cell_coords = (mouse_pos[0] - self.top_area) // self.cell_size + 1, \
                              (mouse_pos[1] - self.left_area) // self.cell_size - 1
                return cell_coords
        elif self.left_book < mouse_pos[0] < self.right_book:
            if self.top_book < mouse_pos[1] < self.down_book and self.book_use is False:
                self.book_use = True
            elif self.book_use is True and self.top_book < mouse_pos[1] < self.down_book:
                self.book_use = False
        return None

    def on_click(self, mouse_pos):  # Действия после клика на клетчатое поле
        if mouse_pos is None:
            self.board = [[0] * 13 for _ in range(8)]
        elif self.board[mouse_pos[1]][mouse_pos[0]] == 0:
            self.board = [[0] * 13 for _ in range(8)]
            self.board[mouse_pos[1]][mouse_pos[0]] = 1
        elif self.board[mouse_pos[1]][mouse_pos[0]] == 1:
            self.board[mouse_pos[1]][mouse_pos[0]] = 0

    def get_click(self, mouse_pos):
        cell = self.get_cell(mouse_pos)
        self.on_click(cell)
