import pygame
from funcoes import *
import save

class Fame:
    def __init__(self, player):
        pygame.init()
        self.window = pygame.display.set_mode((789, 526.5))
        pygame.display.set_caption('Pokémon')

        save.save(player)

        self.assets = {
            'fundo' : pygame.transform.scale(pygame.image.load('assets/hall_fame.png'), (789, 526.5)),
            'caixa' : pygame.transform.scale(pygame.image.load('assets/chatbox.png'), (690, 126)),
            'player' : pygame.transform.scale(pygame.image.load('assets/player/player_grande.png'), (160, 220)),

            'blip' : pygame.mixer.Sound('assets/sound/effect/blip.mp3'),

            "fonte":  pygame.font.Font("assets/batalha/fonte/fonte.ttf", 50),
        }

        self.state = {
            'player' : player,
            'time' : [],
            'aparece' : 0,
        }

        for obj in self.state['player'].team:
            pkmn = obj.assets[0]
            img = pygame.transform.scale(pkmn, (pkmn.get_width() * 3, pkmn.get_height() * 3))
            self.state['time'].append(img)

        pygame.mixer.music.load('assets/sound/music/hall_fame.mp3')
        pygame.mixer.music.play(-1)
        pygame.mixer.music.set_volume(0.5)  

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

        if self.state['aparece'] > 0:
            img = self.state['time'][0]
            self.window.blit(img, (150 - img.get_width()//2, 130 - img.get_height()//2))
        if self.state['aparece'] > 1:
            img = self.state['time'][1]
            self.window.blit(self.state['time'][1], (self.window.get_width()//2 - self.state['time'][1].get_width()//2, 130 - img.get_height()//2))
        if self.state['aparece'] > 2:
            img = self.state['time'][2]
            self.window.blit(self.state['time'][2], (650  - img.get_width()//2, 130 - img.get_height()//2))
        if self.state['aparece'] > 3:
            img = self.state['time'][3]
            self.window.blit(self.state['time'][3], (150  - img.get_width()//2, 305  - img.get_height()//2))
        if self.state['aparece'] > 4:
            img = self.state['time'][4]
            self.window.blit(self.state['time'][4], (self.window.get_width()//2 - self.state['time'][4].get_width()//2, 305  - img.get_height()//2))
        if self.state['aparece'] > 5:
            img = self.state['time'][5]
            self.window.blit(self.state['time'][5], (650  - img.get_width()//2, 305  - img.get_height()//2))

        if self.state['aparece'] > 6:
            black = pygame.surface.Surface((789, 526.5))
            black.fill((71, 77, 94))
            black.set_alpha(200)
            self.window.blit(black, (0, 0))

            self.window.blit(self.assets['player'], (self.window.get_width()//2 - self.assets['player'].get_width()//2, self.window.get_height()//3 - self.assets['player'].get_height()//2))
            self.window.blit(self.assets['caixa'], (self.window.get_width()//2 - self.assets['caixa'].get_width()//2, 380))
            self.window.blit(pygame.font.Font.render(self.assets['fonte'], "LEAGUE CHAMPION!", True, (25, 25, 25)), (100, 395))
            self.window.blit(pygame.font.Font.render(self.assets['fonte'], "CONGRATULATIONS!", True, (25, 25, 25)), (100, 443))

        if self.state['aparece'] < 8:
            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_SPACE:
                self.state['aparece'] += 1
                if self.state['aparece'] < 8:
                    self.assets['blip'].play()


        if self.state['aparece'] > 7:
            fadeout(self.window)
            return 'sair'