import pygame as pg
from modules import Book, GameOver, Mathematics
from modules.RewardWindow import RewardWindow


# тут писал код не я, поэтому я отказываюсь это комментировать
class Swamp:
    def __init__(self):
        self.cell_info = None
        self.event = None
        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.test = False
        self.width = 13
        self.height = 8
        self.board = Mathematics.field_generation()  # Матрица
        self.cell_size = 30
        self.time_to_close = 0

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

    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
                pg.draw.rect(screen, (100, 100, 100),
                             (self.x, self.y, self.cell_size, self.cell_size), 1)

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

        if not self.book_use:
            self.test = False

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

        if self.time_to_close != 0:
            self.time_to_close -= 1
            self.reward_window.open_window(self.cell_info, 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, screen):
        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
                self.cell_info = self.board[cell_coords[1]][cell_coords[0]]
                self.time_to_close = 100
                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):  # Действия после клика на клетчатое поле
        pass

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