import pygame
import sys

import DecardSystem
import consts
import Function
import Mouse

#ІНДЕЯ №1 з вікіпедії
"""
def get_cord(rect1_cord, rect2_cord) -> float:
    x1 = rect1_cord[0][0]
    y1 = rect1_cord[0][1]
    x2 = rect1_cord[1][0]
    y2 = rect1_cord[1][1]

    x3 = rect2_cord[0][0]
    y3 = rect2_cord[0][1]
    x4 = int(rect2_cord[1][0])
    y4 = int(rect2_cord[1][1])
    
    matrix = [
        [x2 - x1, x4 - x3, x3 - x1],
        [y2 - y1, y4 - y3, y3 - y1]
    ]

    delta_x = matrix[0][2] * matrix[1][1] - matrix[0][1] * matrix[1][2]
    delta_y = matrix[1][2] * matrix[0][0] + matrix[1][0] * matrix[0][2]
    delta   = matrix[1][1] * matrix[0][0] - matrix[1][0] * matrix[0][1]

    if delta_x > 0 and delta_y > 0 and delta > 0:
        return (delta_x / delta, delta_y / delta)
"""

class Main():

    def __init__(
        self, *,
        width                 = consts.WIDTH,
        height                = consts.HEIGHT,
        beckground_color      = consts.BACKGROUND_COLOR,
        lucky_color           = consts.LUCKY_COLOR,
        caption               = consts.CAPTION,
        size_font             = consts.SIZE_FONT,
        fps                   = consts.FPS,
        x_1                   = consts.X_1,
        y_1                   = consts.Y_1
        ):
        
        self.width            = width
        self.height           = height
        self.beckground_color = pygame.Color(beckground_color)
        self.lucky_color      = pygame.Color(lucky_color)
        self.caption          = caption
        self.size_font        = size_font
        self.display          = (self.width, self.height)
        self.decard_system    = DecardSystem.DecardSystem()
        self.function         = Function.Function()
        self.mouse            = Mouse.Mouse()
        self.clock            = pygame.time.Clock()
        self.fps              = fps
        self.x_1              = x_1
        self.y_1              = y_1


    def __create_window(self) -> None:
        self.screen = pygame.display.set_mode(self.display)
        self.bg     = pygame.Surface((self.width, self.height))
        self.font   = pygame.font.Font(None, self.size_font)

        self.bg.fill(self.beckground_color)
        pygame.display.set_caption(self.caption)


    def __get_cord(self):
        x = self.rect2_cord[0][0]

        for y in range(int(self.y_1 * 9)):
            if y < self.y_1:
                continue

            rect = pygame.draw.rect(self.screen, self.lucky_color, ((x * self.x_1, y), (1, 1)))

            if rect.colliderect(self.rect1):
                return (x - 1, 9 - (y // self.y_1))


    def app_run(self):

        pygame.init()
        self.__create_window()
        self.screen.blit(self.bg, (0,0))

        while True:
            for i in range(10):
                if i == 0:continue
                pygame.draw.line(self.screen, self.lucky_color, (self.y_1, self.y_1 * i), (self.height - self.y_1, self.y_1 * i))
                pygame.draw.line(self.screen, self.lucky_color, (self.x_1 * i, self.x_1), (self.x_1 * i, self.width - self.x_1))
            
            #draw all
            self.decard_system.draw(self.screen, self.font)
            self.rect1 = self.function.draw(self.screen)

            #QUIT
            for e in pygame.event.get():
                if e.type == pygame.QUIT:
                    sys.exit(0)

                if e.type == pygame.MOUSEBUTTONDOWN:
                    if e.button == 1:  #  левая кнопка мыши
                        mouse_pos = pygame.mouse.get_pos()
                        self.rect2, self.rect2_cord = self.mouse.draw_click(self.screen, mouse_pos)

                        if self.rect1.colliderect(self.rect2):
                            coords_dot = self.__get_cord()
                            print(coords_dot)
                        else:
                            continue

            pygame.display.update()
            self.clock.tick(self.fps) #set Frame Pres Second


def main():
    main = Main()
    main.app_run()

if __name__ == "__main__":
    main()
