import pygame
import settings as s
from time import time
from random import choice
from json import dumps

# from domino import Domino


pygame.font.init()
my_font = pygame.font.SysFont('Calibri (Body)', 50)

class Hand_screen(pygame.sprite.Sprite):
    """рука игрока"""

    def __init__(self):
        super().__init__()
        self.type = "hand_screen"
        self.w = s.SCREEN_WIDTH
        self.h = s.DOMINO_H * 1.5
        self.color = (141, 111, 100)
        self.surf = pygame.Surface((self.w, self.h))
        self.surf.fill(self.color)
        self.pos = (0, s.SCREEN_HEIGHT - self.h)
        self.rect = self.surf.get_rect(center=(self.pos[0] + s.SCREEN_WIDTH / 2, self.pos[1] + self.h / 2))


    def hand_blit(self, screen, lives):
        screen.blit(self.surf, self.rect)
        text = my_font.render(f"Lives: {lives}", True, (255, 255, 255))
        screen.blit(text, (s.SCREEN_WIDTH  - 250, s.SCREEN_HEIGHT - 100))

class Button(pygame.sprite.Sprite):

    def __init__(self):
        super().__init__()
        self.type = "button"
        self.w = 300
        self.h = 50
        self.color = (101, 71, 60)
        self.surf = pygame.Surface((self.w, self.h))
        self.surf.fill(self.color)
        self.pos = (s.SCREEN_WIDTH - 30, s.SCREEN_HEIGHT - 350)
        self.rect = self.surf.get_rect(center=(self.pos[0] - self.w / 2, self.pos[1]))
        self.text = "take new domino"

    def blit_button(self, screen):
        # блит кнопки
        screen.blit(self.surf, self.rect)

        text = my_font.render(f"{self.text}", True, (255, 255, 255))
        screen.blit(text,
                    (self.rect.x + (self.w / 2) - text.get_width() / 2,
                     self.rect.y + (self.h / 2) - text.get_height() / 2))
        # print(self.rect.x + (self.w / 2) - v_up.get_width(), self.rect.y + (self.h / 4) - v_up.get_height())

    def active_button(self, mouse_x, mouse_y, r_click_time, dominos):
        # если нажали на кнопку, добавляем домино в руку
        values = False
        if self.rect.x <= mouse_x <= self.rect.x + self.w and self.rect.y <= mouse_y <= self.rect.y + self.h:
            # pygame.transform.rotate(self.surf, 90)
            x = 110 * len(dominos) - 1
            y = s.SCREEN_HEIGHT - s.DOMINO_H / 2 - 25
            if len(s.DOMINOS) > 0:
                values = choice(s.DOMINOS)
                # domino = Domino(x, y, values, len(dominos) - 1)
                s.DOMINOS.remove(values)

            return time(), values, [x, y]
        return r_click_time, False, False


class Empty_domino(pygame.sprite.Sprite):
    """пустышка"""

    def __init__(self, x, y, pos):
        super().__init__()
        self.type = "empty domino"
        self.w = s.DOMINO_W * 1.1
        self.h = s.DOMINO_H * 1.1
        self.surf = pygame.Surface((self.w, self.h))
        self.color = (100, 10, 10)
        self.surf.fill(self.color)
        self.rect = self.surf.get_rect(center=(x + self.w / 2, y + self.h / 2))
        self.pos = pos


class Game_screen(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.type = "game_screen"
        self.w = s.SCREEN_WIDTH * 10
        self.h = s.SCREEN_HEIGHT * 10
        self.color = (220, 220, 220)
        self.surf = pygame.Surface((self.w, self.h))
        self.surf.fill(self.color)
        self.rect = self.surf.get_rect(center=(0, 0))

        self.group = pygame.sprite.Group()
        self.group.add(self)
        self.empty_group = pygame.sprite.Group()

        empty_up = Empty_domino(s.SCREEN_WIDTH / 2, s.SCREEN_HEIGHT / 2 - (s.DOMINO_W * 1.1 * 2 + 10), "up")
        empty_down = Empty_domino(s.SCREEN_WIDTH / 2, s.SCREEN_HEIGHT / 2 + (s.DOMINO_W * 1.1 * 2 + 10), "down")
        empty_both = Empty_domino(s.SCREEN_WIDTH / 2, s.SCREEN_HEIGHT / 2, "both")
        self.emptys = [empty_up, empty_down, empty_both]

        self.group.add(empty_both)
        self.empty_group.add(empty_both)

        self.value_up = None
        self.value_down = None

        self.empty_up = empty_both
        self.empty_down = empty_both

        self.lives = 3

    def blit_g_screen(self, screen, dominos):
        # add_empty()

        for entity in self.group:
            if entity.type == "domino":
                entity.blit_domino(screen, dominos)
            else:
                screen.blit(entity.surf, entity.rect)

    def update_pos(self, mouse_x, mouse_y, mouse_new_x, mouse_new_y):
        # check if mouse colide with domino
        if self.rect.x <= mouse_new_x <= self.rect.x + self.w and self.rect.y <= mouse_new_y <= self.rect.y + self.h:

            for entity in self.group:
                entity.rect.x -= mouse_x
                entity.rect.y -= mouse_y