import pygame
import json

class Score:
    def __init__(self):
        pygame.init()
        self.window = pygame.display.set_mode((720, 480))
        pygame.display.set_caption('Pokémon')

        self.assets = {
            'background' : pygame.transform.scale(pygame.image.load('assets/score.png'), (720, 480)),

            "fonte_titulo":  pygame.font.Font("assets/Univers Next Pro Heavy Condensed.ttf", 50),
            "fonte":  pygame.font.Font("assets/batalha/fonte/fonte.ttf", 30),
        }

        self.pos = [
            [55, 115],
            [55, 159],
            [55, 201],
            [55, 245],
            [55, 285],
            [55, 327],
            [55, 370],
            [55, 410],
            [325, 115],
            [325, 159],
            [325, 201],
            [325, 245],
            [325, 285],
            [325, 327],
            [325, 370],
            [325, 410],
        ]

        with open('save.json', 'r') as openfile:
            dicionario = json.load(openfile)
        self.name = dicionario['name']
        self.cheater = dicionario['cheater']

    def desenha(self):
        self.window.blit(self.assets['background'], (0, 0))

        txt = pygame.font.Font.render(self.assets['fonte_titulo'], "PREVIOUS CHAMPIONS", True, (255, 255, 255))
        self.window.blit(txt, (self.window.get_width()//2 - txt.get_width()//2, 8))

        i = 1
        for linha in self.pos:
            self.window.blit(pygame.font.Font.render(self.assets['fonte'], str(i) + ' - ', True, (25, 25, 25)), (linha))
            i += 1


        ordem = 0
        for i in range(len(self.name) - 1, 0, -1):
            campeao =  self.name[i]
            if self.cheater[i]:
                campeao += ' (CHEATER)'
            self.window.blit(pygame.font.Font.render(self.assets['fonte'], campeao, True, (25, 25, 25)), (self.pos[ordem][0] + 40, self.pos[ordem][1]))
            ordem += 1

        pygame.display.update()

    def update(self):
        for evento in pygame.event.get():
            if evento.type == pygame.QUIT:
                return 'sair'
            if evento.type == pygame.KEYDOWN and evento.key == pygame.K_ESCAPE:
                self.window = pygame.display.set_mode((624, 672))
                pygame.display.set_caption('Pokémon')
                return 'lobby'