import pygame
import copy

class Box:
    def __init__(self, player,pokedex): # a tela da box, mostra cada pokemon que tem, e te deixa selecionar um deles para sua party(utiliza tela troca para mudar party)
        pygame.init()
        self.window = pygame.display.set_mode((720, 480))
        pygame.display.set_caption('Pokémon')


        self.assets = {
            'cancel' : pygame.transform.scale(pygame.image.load('assets/box/box_close_button.png'), (pygame.image.load('assets/box/box_close_button.png').get_width()*3,pygame.image.load('assets/box/box_close_button.png').get_height()*3)),
            'data' : pygame.transform.scale(pygame.image.load('assets/box/box_data.png'), (pygame.image.load('assets/box/box_data.png').get_width()*3,pygame.image.load('assets/box/box_data.png').get_height()*3)),
            'wallpaper' : pygame.transform.scale(pygame.image.load('assets/box/box_wallpaper.png'), (pygame.image.load('assets/box/box_wallpaper.png').get_width()*3,pygame.image.load('assets/box/box_wallpaper.png').get_height()*3)),
            'fundo' : pygame.transform.scale(pygame.image.load('assets/box/fundo.png'), (pygame.image.load('assets/box/fundo.png').get_width()*3,pygame.image.load('assets/box/fundo.png').get_height()*3)),
            'fundo2' : pygame.transform.scale(pygame.image.load('assets/box/fundo2.png'), (pygame.image.load('assets/box/fundo2.png').get_width()*3,pygame.image.load('assets/box/fundo2.png').get_height()*3)),
            
            'mao' : pygame.transform.scale(pygame.image.load('assets/box/box_mao.png'), (pygame.image.load('assets/box/box_mao.png').get_width()*2,pygame.image.load('assets/box/box_mao.png').get_height()*2)),
            'party' : pygame.transform.scale(pygame.image.load('assets/box/box_party.png'), (pygame.image.load('assets/box/box_party.png').get_width()*3,pygame.image.load('assets/box/box_party.png').get_height()*3)),

            "barraVerde" : pygame.image.load('assets/batalha/barra_verde.png'),
            "barraAmarela" : pygame.image.load('assets/batalha/barra_amarela.png'),
            "barraVermelha" : pygame.image.load('assets/batalha/barra_vermelha.png'),
            'pc_close' : pygame.mixer.Sound('assets/sound/effect/pc_off.mp3'),
            'setaPreta' : pygame.transform.scale(pygame.image.load('assets/batalha/seta_preta.png'), (pygame.image.load('assets/batalha/seta_preta.png').get_width()*3,pygame.image.load('assets/batalha/seta_preta.png').get_height()*3)),



            "fonte":  pygame.font.Font("assets/batalha/fonte/fonte.ttf", 40),
            "fonteP":  pygame.font.Font("assets/batalha/fonte/fonte.ttf", 32),
            "fontePP":  pygame.font.Font("assets/batalha/fonte/fonte.ttf", 24),

        }

        
        self.pokedex = pokedex

        self.state = {
            'player' : player,
            "selected": 0,
            "box":0,
        }







    def desenha(self):

        self.window.blit(self.assets['wallpaper'], (0,0))
        self.window.blit(self.assets['wallpaper'], (0 + self.assets["data"].get_width(),0))

        if self.state["box"] == 0:
            self.window.blit(self.assets['fundo'], (6 + self.assets["data"].get_width(),51))
            self.window.blit(self.assets['setaPreta'], (676, 63))

        else:
            self.window.blit(self.assets['fundo2'], (6 + self.assets["data"].get_width(),51))
            self.window.blit(pygame.transform.flip(self.assets['setaPreta'],1,0), (260, 63))

        self.window.blit(self.assets['data'], (0,7))
        self.window.blit(self.assets["fonte"].render("BOX " + str(self.state["box"]), 100, "black"),(450, 66)) 
        

        size = 30
        l=[0,0]
        for i in range(0 + size*self.state["box"],len(self.pokedex)):

            if l[0] == 6:
                l[1] += (self.assets["fundo"].get_height()-20)/6
                l[0] = 0
            self.window.blit( pygame.transform.scale(self.pokedex[i].assets[2], (self.pokedex[i].assets[2].get_width()*2,self.pokedex[i].assets[2].get_height()*2)), (45 + self.assets["data"].get_width() +  (self.assets["fundo"].get_width() - 10)/6*l[0] - self.pokedex[i].assets[2].get_width() ,146 + l[1]))
            

            if self.state["selected"] == i:
                self.window.blit(self.assets["mao"], (45 + self.assets["data"].get_width() +  (self.assets["fundo"].get_width() - 10)/6*l[0] - self.pokedex[i].assets[2].get_width() ,160 + l[1] - 50))

                self.window.blit( pygame.transform.scale(self.pokedex[i].assets[0], (self.pokedex[i].assets[0].get_width()*3,self.pokedex[i].assets[0].get_height()*3)), (125  - self.pokedex[i].assets[0].get_width()*(3/2) ,150 -  self.pokedex[i].assets[0].get_height()*(3/2)))
                self.window.blit(self.assets["fonte"].render(self.pokedex[i].nome.upper(), 100, "white"),( 15 ,265)) 
                for t in range(len(self.pokedex[i].tipo)):
                    self.window.blit(self.assets["fonteP"].render(self.pokedex[i].tipo[t].upper(), 100, "white"),( 15 + 100*t ,300)) 
                
  
                for m in  range(len(self.pokedex[i].moveset)):
                    y = 0
                    if m > 1:
                        y = 1
                    self.window.blit(self.assets["fontePP"].render(self.pokedex[i].moveset[m].nome.upper(), 100, "black"),( 8 + 108*m - 216*y,350 + y*50)) 

            if self.state["selected"] == -1 + 30*self.state["box"]:
                self.assets['setaPreta'] = pygame.transform.scale(pygame.image.load('assets/batalha/seta_preta.png'), (pygame.image.load('assets/batalha/seta_preta.png').get_width()*4,pygame.image.load('assets/batalha/seta_preta.png').get_height()*4))
            else:
                self.assets['setaPreta'] = pygame.transform.scale(pygame.image.load('assets/batalha/seta_preta.png'), (pygame.image.load('assets/batalha/seta_preta.png').get_width()*3,pygame.image.load('assets/batalha/seta_preta.png').get_height()*3))



            
            l[0] += 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_SPACE:
                if self.state["selected"] >=0+ 30*self.state["box"]:
                    return "troca", [2, copy.copy(self.pokedex[self.state["selected"]])]
                else:
                    self.state["box"] = 1 + -1*self.state["box"]
                    self.state["selected"] = -1 + 30*self.state["box"]
                    



                
            elif evento.type == pygame.KEYDOWN:
                if (evento.key == pygame.K_a or evento.key == pygame.K_LEFT) and self.state["selected"] - 1 < len(self.pokedex):
                    self.state["selected"] -= 1

                if evento.key == pygame.K_ESCAPE:
                    self.assets['pc_close'].play()
                    self.window = pygame.display.set_mode((624, 672))
                    return "lobby"
                if evento.key == pygame.K_d or evento.key ==pygame.K_RIGHT and self.state["selected"] +1 < len(self.pokedex) and self.state["selected"] < 29 + 30*self.state["box"]:
                    self.state["selected"] += 1
                if (evento.key == pygame.K_s or evento.key ==pygame.K_DOWN) and self.state["selected"] + 6 < 30 + 30*self.state["box"]:
                    if self.state["selected"] == -1 + 30*self.state["box"]:
                        self.state["selected"] = 0 + 30*self.state["box"]
                    else:
                        self.state["selected"] += 6

                
                if (evento.key == pygame.K_w or evento.key ==pygame.K_UP) and self.state["selected"] - 6 < len(self.pokedex):
                    self.state["selected"] -= 6
                
                if self.state["selected"] > 29 + 30*self.state["box"]:
                    self.state["selected"] = 29 + 30*self.state["box"]

                if self.state["selected"] >= len(self.pokedex):
                    self.state["selected"] = len(self.pokedex) -2


                if self.state["selected"] < 0 + 30*self.state["box"]:
                    self.state["selected"] = -1 + 30*self.state["box"]
                elif self.state["selected"] < 30*self.state["box"] -1 :
                    self.state["selected"] += 30*self.state["box"]


                
                

        return " "