import pygame
from loja import Loja
from funcoes import *


class TelaLobby:
    def __init__(self, player):
        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)),

            'mapa' : pygame.transform.scale(pygame.image.load('assets/mapa/mapa_lobby.png'), (432, 528)),
            'chat' : pygame.transform.scale(pygame.image.load('assets/chatbox.png'), (600, 126)),
            'musica_fundo': 'assets/sound/music/musica_fundo.mp3',
            'blip' : pygame.mixer.Sound('assets/sound/effect/blip.mp3'),
            'troca_sala' : pygame.mixer.Sound('assets/sound/effect/troca_sala.mp3'),
            'menu' : pygame.mixer.Sound('assets/sound/effect/abre_menu.mp3'),
            'pc_open' : pygame.mixer.Sound('assets/sound/effect/pc_acessa.mp3'),

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

        pygame.mixer.music.load(self.assets['musica_fundo'])
        pygame.mixer.music.play(-1)
        pygame.mixer.music.set_volume(0.5)


        meio = (self.window.get_width()//2, self.window.get_height()//2)

        self.state = {
            'player' : player,
            'parede' : [],
            'fala' : 0,
            'alpha' : 255//2,
            'telaloja' : False,
            'loja' : Loja(self.window, player),
            'seta' : 0,
            'pos_seta' : [(394, 180), (394, 211)],
            'pause' : False,
            'move' : True,
        }

        self.colisao = pygame.Rect(177, 377, 36, 28)

        self.pc = pygame.Rect(195, 171, 42, 63)

        self.board = pygame.Rect(388, 162, 37, 64)

        self.state['player'].rect.x = meio[0] - self.state['player'].image.get_width()//2
        self.state['player'].rect.y = meio[1] - self.state['player'].image.get_height()//2


        self.state['parede'].append(parede_invisivel(95, 300, 95, 170)) #mesa loja
        self.state['parede'].append(parede_invisivel(0, 0, 95, 672)) #esquerda
        self.state['parede'].append(parede_invisivel(528, 0, 114, 672)) #direita
        self.state['parede'].append(parede_invisivel(0, 0, 624, 73)) #cima
        self.state['parede'].append(parede_invisivel(0, 600, 624, 72)) #baixo
        self.state['parede'].append(parede_invisivel(95, 73, 178, 89)) #paredecima
        self.state['parede'].append(parede_invisivel(271, 73, 80, 26)) #paredecima
        self.state['parede'].append(parede_invisivel(350, 73, 180, 90)) #paredecima
        

        self.state['parede'].append(parede_invisivel(193, 150, 47, 35)) #pc
        self.state['parede'].append(parede_invisivel(385, 150, 47, 35)) #estatua2

        self.state['parede'].append(parede_invisivel(480, 228, 48, 84)) #planta1
        self.state['parede'].append(parede_invisivel(480, 324, 48, 84)) #planta2
        self.state['parede'].append(parede_invisivel(480, 420, 48, 54)) #planta3


    def desenha(self):
        self.window.fill((0, 0, 0))

        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))

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

        if self.state['fala'] > 0 and not self.state['telaloja']:
            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:
            self.window.blit(pygame.font.Font.render(self.assets['fonte'], "Hi, there!", True, (25, 25, 25)), (48, 520))
            self.window.blit(pygame.font.Font.render(self.assets['fonte'], "May I help you?", True, (25, 25, 25)), (48, 565))

        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']])

        if not self.state['telaloja']:
            pygame.display.update()

    def update(self):
        if self.state['telaloja']:
            retorna = poploja(self.window, self, self.state['player'])
            if retorna == 'sair':
                return retorna
            if retorna == 'volta':
                self.state['telaloja'] = False
                self.state['fala'] = 0

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

            if self.state['player'].rect.y < 162 - self.state['player'].image.get_height():
                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 'sala1'

            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.colisao):
                            self.state['fala'] += 1
                            if self.state['fala'] == 1:
                                self.assets['blip'].play()
                        if pygame.Rect.colliderect(self.state['player'].rect, self.pc):
                            self.assets['pc_open'].play()
                            return 'box'
                        if pygame.Rect.colliderect(self.state['player'].rect, self.board):
                            return 'board'
                    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_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:
                self.state['telaloja'] = True

