import pygame as pg
from math import pi, sin, cos
from random import *


class Game:
    def __init__(self):
        self.line = None
        self.old_color = None
        self.interval_x = None
        self.interval_y = None
        self.colors = [tuple([randrange(50, 256, 40) for _ in range(3)]) for _ in range(10)]
        self.intervals_x = [(430, 460), (350, 430), (245, 350), (165, 245), (130, 165), (130, 165), (165, 245),
                            (245, 350), (350, 430), (430, 460)]
        self.intervals_y = [(170, 265), (113, 170), (113, 113), (113, 170), (170, 265), (265, 360), (360, 425),
                            (425, 425), (365, 425), (265, 365)]
        self.line_color = choice(self.colors)
        self.clock = pg.time.Clock()
        self.speed = 15  # Здесь можно менять скорость
        self.right = False
        self.lose = False
        self.corner = 440
        self.scores = 0
        self.x = 300
        self.y = 430

    def render(self, screen_move):  # Создание круга из случайных цветов и стрелки
        pg.draw.arc(screen_move, (self.colors[0]),
                    (120, 90, 350, 350), 0, pi / 5, 7)
        pg.draw.arc(screen_move, (self.colors[1]),
                    (120, 90, 350, 350), pi / 5, 2 * pi / 5, 7)
        pg.draw.arc(screen_move, (self.colors[2]),
                    (120, 90, 350, 350), 2 * pi / 5, 3 * pi / 5, 7)
        pg.draw.arc(screen_move, (self.colors[3]),
                    (120, 90, 350, 350), 3 * pi / 5, 4 * pi / 5, 7)
        pg.draw.arc(screen_move, (self.colors[4]),
                    (120, 90, 350, 350), 4 * pi / 5, pi, 7)
        pg.draw.arc(screen_move, (self.colors[5]),
                    (120, 90, 350, 350), pi, 6 * pi / 5, 7)
        pg.draw.arc(screen_move, (self.colors[6]),
                    (120, 90, 350, 350), 6 * pi / 5, 7 * pi / 5, 7)
        pg.draw.arc(screen_move, (self.colors[7]),
                    (120, 90, 350, 350), 7 * pi / 5, 8 * pi / 5, 7)
        pg.draw.arc(screen_move, (self.colors[8]),
                    (120, 90, 350, 350), 8 * pi / 5, 9 * pi / 5, 7)
        pg.draw.arc(screen_move, (self.colors[9]),
                    (120, 90, 350, 350), 9 * pi / 5, 2 * pi, 7)
        pg.draw.line(screen_move, self.line_color, [300, 270], [self.x, self.y], 9)

        font = pg.font.Font(None, 50)
        text = font.render('Счёт:', True, (255, 255, 255))
        screen_move.blit(text, (50, 500))
        text = font.render(f'{self.scores}/10', True, (255, 255, 255))
        screen_move.blit(text, (70, 540))
        if self.scores == 10:
            print("Прекол")
            self.get_reward(10)

    def side_change(self):  # Изменение стороны и проверка
        if not self.right:
            self.right = True
        elif self.right:
            self.right = False
        if color_change:
            self.old_color = self.line_color
            while self.old_color == self.line_color:
                self.line_color = choice(self.colors)

            self.interval_x = self.intervals_x[self.colors.index(self.old_color)]
            self.interval_y = self.intervals_y[self.colors.index(self.old_color)]
            if self.interval_x[0] <= round(self.x) <= self.interval_x[1] and \
                    ((round(self.y) <= self.interval_y[0] == self.interval_y[1]) or
                     (self.interval_y[0] <= round(self.y) <= self.interval_y[1])):
                self.scores += 1
                self.speed += 10
            else:
                print("Не прекол")
                scores = self.scores
                self.get_reward(scores)
                global running
                running = False

    def animation(self):  # анимация (изменение координат)
        if self.right:
            if self.corner > 90 and self.corner > 270:
                self.y = sin(abs(self.corner)) * 165 + 270
                self.x = cos(abs(self.corner)) * 165 + 295
            else:
                self.y = sin(abs(self.corner)) * 165 + 270
                self.x = -cos(abs(self.corner)) * 165 + 295
            self.corner += self.speed * self.clock.tick(50000) / 10000
        else:
            if self.corner > 90 or self.corner > 270:
                self.y = sin(abs(self.corner)) * 165 + 270
                self.x = cos(abs(self.corner)) * 165 + 295
            else:
                self.y = sin(abs(self.corner)) * 165 + 270
                self.x = -cos(abs(self.corner)) * 165 + 295
                self.line = pg.transform.rotate(self.line, self.corner)
            self.corner -= self.speed * self.clock.tick(50000) / 10000

    @staticmethod
    def get_reward(scope):
        scope_mass = {
            "1": 5,
            "2": 10,
            "3": 20,
            "4": 35,
            "5": 50,
            "6": 75,
            "7": 100,
            "8": 150,
            "9": 200,
            "10": 1000
        }
        from modules.DB import UseDB
        db = UseDB("game")
        db.update_document(
            {
                "save.name": "start"
            },
            {
                "money": db.find_document({})[0]["money"] + scope_mass[str(scope)]
            }
        )


pg.init()
size = 600, 600
animation = False
color_change = False
running = True


def start():
    global running, animation, color_change
    pg.init()
    screen = pg.display.set_mode(size)
    game = Game()
    animation = False
    color_change = False
    running = True
    while running:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                running = False
            if event.type == pg.MOUSEBUTTONDOWN:  # Запуск игры
                animation = True
                game.side_change()
                color_change = True
        if animation:
            game.animation()
        pg.display.update()
        screen.fill((40, 40, 40))
        game.render(screen)
        pg.display.flip()
    pg.display.quit()
