import pygame
from mestre import Mestre
from loop_batalha import loop_batalha
from funcoes import *

class TelaSala3:
    def __init__(self, player, time):
        pygame.init()
        self.window = pygame.display.set_mode((624, 672))
        pygame.display.set_caption('Pokémon')

        self.assets = {
            'pause_box' : pygame.transform.scale(pygame.image.load('assets/pause_box.png'), (140, 120)),
            'seta' : pygame.transform.scale(pygame.image.load('assets/loja/seta_loja.png'), (12, 20)),

            'frente' : pygame.transform.scale(pygame.image.load('assets/npc/mestre3_frente.png'), (42, 60)),
            'costas' : pygame.transform.scale(pygame.image.load('assets/npc/mestre3_costas.png'), (42, 60)),
            'esquerda' : pygame.transform.scale(pygame.image.load('assets/npc/mestre3_esquerda.png'), (45, 60)),
            'direita' : pygame.transform.scale(pygame.image.load('assets/npc/mestre3_direita.png'), (45, 60)),
            'full' : pygame.transform.scale(pygame.image.load('assets/npc/grande/mestre3_grande.png'), (30, 57)),

            'mapa' : pygame.transform.scale(pygame.image.load('assets/mapa/sala3.png'), (624, 609)),
            'topo' : pygame.transform.scale(pygame.image.load('assets/mapa/objetos/bloco_sala.png'), (78, 54)),
            'parede' : pygame.transform.scale(pygame.image.load('assets/mapa/objetos/parede_fechada.png'), (78, 90)),
            'chat' : pygame.transform.scale(pygame.image.load('assets/chatbox.png'), (600, 126)),
            'musica_fundo': 'assets/sound/music/musica_fundo.mp3',
            'pre_batalha' : 'assets/sound/music/pre_batalha.mp3',
            'blip' : pygame.mixer.Sound('assets/sound/effect/blip.mp3'),
            'porta' : pygame.mixer.Sound('assets/sound/effect/porta_fechando.mp3'),
            'troca_sala' : pygame.mixer.Sound('assets/sound/effect/troca_sala.mp3'),
            'menu' : pygame.mixer.Sound('assets/sound/effect/abre_menu.mp3'),

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

        


        self.state = {
            'player' : player,
            'mestre' : Mestre("AGATHA",time, self.assets['frente'], self.assets['costas'], self.assets['esquerda'], self.assets['direita'],self.assets['full'],"Oh my! You’re something special, child!?"),
            'parede' : [],
            'fecha' : False,
            'fala' : 0,
            'porta_aberta' : False,
            'cheat' : 0,
            'audio' : True,
            'vitoria' : False,
            'fade' : True,
            'alpha' : 255,
            'seta' : 0,
            'pos_seta' : [(394, 180), (394, 211)],
            'pause' : False,
            'move' : True,
        }


        self.state['mestre'].rect.x = self.window.get_width()//2 - self.state['mestre'].image.get_width()//2
        self.state['mestre'].rect.y = self.window.get_height()//2 - self.state['mestre'].image.get_height()//2

        self.area1 = pygame.Rect(self.state['mestre'].rect.x, self.state['mestre'].rect.y - 30, self.state['mestre'].image.get_width(), self.state['mestre'].image.get_height() + 60)
        self.area2 = pygame.Rect(self.state['mestre'].rect.x - 30, self.state['mestre'].rect.y, self.state['mestre'].image.get_width() + 60, self.state['mestre'].image.get_height())


        self.state['parede'].append(parede_invisivel(0, 0, 143, 495)) #esquerda
        self.state['parede'].append(parede_invisivel(0, 0, 624, 101)) #cima
        self.state['parede'].append(parede_invisivel(0, 640, 624, 32)) #baixo
        self.state['parede'].append(parede_invisivel(480, 0, 192, 624)) #direita

        self.state['parede'].append(parede_invisivel(0, 498, 278, 139)) #paredebaixo
        self.state['parede'].append(parede_invisivel(346, 498, 278, 139)) #paredebaixo

        self.state['parede'].append(parede_invisivel(0, 0, 280, 158)) #paredecima
        self.state['parede'].append(parede_invisivel(344, 0, 287, 158)) #paredecima
        self.state['parede'].append(parede_invisivel(288, 101, 48, 40)) #porta

        self.state['parede'].append(parede_invisivel(self.state['mestre'].rect.x, self.state['mestre'].rect.y, self.state['mestre'].image.get_width(), self.state['mestre'].image.get_height()))

    def desenha(self):
        self.window.blit(self.assets['mapa'], (self.window.get_width()//2 - self.assets['mapa'].get_width()//2, self.window.get_height()//2 - self.assets['mapa'].get_height()//2))

        if self.state['porta_aberta']:
            pygame.draw.rect(self.window, (0, 0, 0), pygame.Rect(288, 104, 48, 49))

        self.window.blit(self.state['player'].image, self.state['player'].rect)

        self.window.blit(self.state['mestre'].image, self.state['mestre'].rect)
        
        self.window.blit(self.assets['topo'], (274, 497))



        if self.state['fecha']:
            self.window.blit(self.assets['parede'], (273, 551))

        if self.state['fala'] > 0:
            self.window.blit(self.assets['chat'], (self.window.get_width()//2 - self.assets['chat'].get_width()//2, self.window.get_height() - self.assets['chat'].get_height() - 45))

        if self.state['fala'] == 1 and not self.state['vitoria']:
            self.window.blit(pygame.font.Font.render(self.assets['fonte'], "I am AGATHA of the ELITE FOUR.", True, (25, 25, 25)), (48, 520))

        if self.state['fala'] == 2 and not self.state['vitoria']:
            nome = self.state['player'].nome
            self.window.blit(pygame.font.Font.render(self.assets['fonte'], f'{nome}! I\'l show you how a real', True, (25, 25, 25)), (48, 520))
            self.window.blit(pygame.font.Font.render(self.assets['fonte'], "TRAINER battles!", True, (25, 25, 25)), (48, 565))

        if self.state['fala'] == 1 and self.state['vitoria']:
            self.window.blit(pygame.font.Font.render(self.assets['fonte'], "You win!", True, (25, 25, 25)), (48, 520))
        if self.state['fala'] == 2 and self.state['vitoria']:
            self.window.blit(pygame.font.Font.render(self.assets['fonte'], 'I have nothing else to say.', True, (25, 25, 25)), (48, 520))
            self.window.blit(pygame.font.Font.render(self.assets['fonte'], "Run along now, child!", True, (25, 25, 25)), (48, 565))
            

        if self.state['alpha'] > 0:
            fade = pygame.Surface((self.window.get_width(), self.window.get_height()))
            fade.fill((0, 0, 0))
            fade.set_alpha(self.state['alpha'])
            self.window.blit(fade, (0, 0))
            self.state['alpha'] -= 25

        if self.state['pause']:
            self.window.blit(self.assets['pause_box'], (370, 150))
            self.window.blit(pygame.font.Font.render(self.assets['fonte_pause'], "POKéMON", True, (25, 25, 25)), (410, 174))
            self.window.blit(pygame.font.Font.render(self.assets['fonte_pause'], "BAG", True, (25, 25, 25)), (410, 204))
            self.window.blit(self.assets['seta'], self.state['pos_seta'][self.state['seta']])


        pygame.display.update()

    def update(self):
        if self.state['move']:
            self.state['player'].update(self.state['parede'], self.state['fala'])



        if self.state['player'].rect.y < 500 - self.state['player'].image.get_height() and self.state['audio']:
            self.state['fecha'] = True
            self.state['parede'].append(parede_invisivel(274, 498, 100, 50))
            self.assets['porta'].play()
            self.state['audio'] = False

        if self.state['porta_aberta']:
            for obj in range(len(self.state['parede'])):
                if self.state['parede'][obj] == pygame.Rect(288, 101, 48, 40):
                    del self.state['parede'][obj]
                    break
    

        for evento in pygame.event.get():
            if evento.type == pygame.QUIT:
                return 'sair'
            if evento.type == pygame.KEYDOWN and evento.key == pygame.K_SPACE:
                if not self.state['pause']:
                    if pygame.Rect.colliderect(self.state['player'].rect, self.area1) or pygame.Rect.colliderect(self.state['player'].rect, self.area2):
                        self.state['fala'] += 1
                        if self.state['fala'] > 0:
                            self.assets['blip'].play()
                        if self.state['fala'] == 2 and not self.state['vitoria']:
                            pygame.mixer.music.load('assets/sound/music/pre_batalha.mp3')
                            pygame.mixer.music.play(-1)
                else:
                    if self.state['seta'] == 1:
                        self.state['pause'] = False
                        return 'bag'
                    if self.state['seta'] == 0:
                        self.state['pause'] = False
                        return "troca", [3]
            if evento.type == pygame.KEYDOWN and evento.key == pygame.K_c:
                self.state['cheat'] += 1
            if evento.type == pygame.KEYDOWN and (evento.key == pygame.K_DOWN or evento.key == pygame.K_s) and self.state['seta'] < 1 and self.state['pause']:
                    self.state['seta'] += 1
                    self.assets['blip'].play()
            if evento.type == pygame.KEYDOWN and (evento.key == pygame.K_UP or evento.key == pygame.K_w) and self.state['seta'] > 0 and self.state['pause']:
                self.state['seta'] -= 1
                self.assets['blip'].play()
            if evento.type == pygame.KEYDOWN and (evento.key == pygame.K_ESCAPE or evento.key == pygame.K_p):
                if self.state['pause']:
                        self.state['pause'] = False
                        self.state['seta'] = 0
                        self.assets['blip'].play()
                elif self.state['fala'] == 0:
                    self.state['pause'] = True
                    self.assets['menu'].play()

        if self.state['pause']:
                self.state['move'] = False
        else:
            self.state['move'] = True

        if self.state['fala'] > 2 and self.state['vitoria']:
            self.state['fala'] = 0

        if self.state['cheat'] == 5:
            self.state['porta_aberta'] = True
            self.state['player'].cheater = True

        if pygame.rect.Rect.colliderect(self.state['player'].rect, pygame.Rect(288, 101, 48, 10)):
            self.state['player'].rect.x = self.window.get_width()//2 - self.state['player'].image.get_width()//2
            self.state['player'].rect.y = 580
            self.assets['troca_sala'].play()
            fadeout(self.window)
            return 'sala4'
        

        if self.state['fala'] == 3 and not self.state['vitoria']:
            pygame.mixer.music.load('assets/sound/music/batalha.mp3')
            pygame.mixer.music.play(-1)
            loop = loop_batalha(self.state['player'], self.state['mestre'])
            if loop in ['sair', 'gameover']:
                return loop
            if loop == 'vitoria':
                pygame.mixer.music.load('assets/sound/music/musica_fundo.mp3')
                pygame.mixer.music.play(-1)
                self.state['porta_aberta'] = True
                self.state['fala'] = 1
                self.state['vitoria'] = True
                self.window = pygame.display.set_mode((624, 672))
            