import pygame


class Troca:
    def __init__(self, player, batalha): #tela que mostra todos seus pokemons, em primeiro lugar seu pokemon ativo, aqui se modifica ordem dos pokemons e com a box muda o pokemon que eh
        pygame.init()
        self.window = pygame.display.set_mode((720, 480))
        pygame.display.set_caption('Pokémon')

        self.assets = {
            'cancel1' : pygame.transform.scale(pygame.image.load('assets/selecao/botao_cancel_selecionado.png'), (pygame.image.load('assets/selecao/botao_cancel_selecionado.png').get_width()*3,pygame.image.load('assets/selecao/botao_cancel_selecionado.png').get_height()*3)),
            'cancel0' : pygame.transform.scale(pygame.image.load('assets/selecao/botao_cancel.png'), (pygame.image.load('assets/selecao/botao_cancel.png').get_width()*3,pygame.image.load('assets/selecao/botao_cancel.png').get_height()*3)),
            'fundo' : pygame.transform.scale(pygame.image.load('assets/selecao/fundo_selecao.png'), (pygame.image.load('assets/selecao/fundo_selecao.png').get_width()*3,pygame.image.load('assets/selecao/fundo_selecao.png').get_height()*3)),
            'hp' : pygame.transform.scale(pygame.image.load('assets/selecao/hp_selecao.png'), (pygame.image.load('assets/selecao/hp_selecao.png').get_width()*3,pygame.image.load('assets/selecao/hp_selecao.png').get_height()*3)),
            'pokemonG0' : pygame.transform.scale(pygame.image.load('assets/selecao/pokemon_selecao_grande.png'), (pygame.image.load('assets/selecao/pokemon_selecao_grande.png').get_width()*3,pygame.image.load('assets/selecao/pokemon_selecao_grande.png').get_height()*3)),
            'pokemon0' : pygame.transform.scale(pygame.image.load('assets/selecao/pokemon_selecao.png'), (pygame.image.load('assets/selecao/pokemon_selecao.png').get_width()*3,pygame.image.load('assets/selecao/pokemon_selecao.png').get_height()*3)),
            'pokemonG1' : pygame.transform.scale(pygame.image.load('assets/selecao/pokemon_selecionado_grande.png'), (pygame.image.load('assets/selecao/pokemon_selecionado_grande.png').get_width()*3,pygame.image.load('assets/selecao/pokemon_selecionado_grande.png').get_height()*3)),
            'pokemon1' : pygame.transform.scale(pygame.image.load('assets/selecao/pokemon_selecionado.png'), (pygame.image.load('assets/selecao/pokemon_selecionado.png').get_width()*3,pygame.image.load('assets/selecao/pokemon_selecionado.png').get_height()*3)),
            'pokemonG0D' : pygame.transform.scale(pygame.image.load('assets/selecao/pokemon_selecao_grande_faint.png'), (pygame.image.load('assets/selecao/pokemon_selecao_grande.png').get_width()*3,pygame.image.load('assets/selecao/pokemon_selecao_grande.png').get_height()*3)),
            'pokemon0D' : pygame.transform.scale(pygame.image.load('assets/selecao/pokemon_selecao_fainted.png'), (pygame.image.load('assets/selecao/pokemon_selecao.png').get_width()*3,pygame.image.load('assets/selecao/pokemon_selecao.png').get_height()*3)),
            'pokemonG1D' : pygame.transform.scale(pygame.image.load('assets/selecao/pokemon_selecionado_grande_fainted.png'), (pygame.image.load('assets/selecao/pokemon_selecionado_grande.png').get_width()*3,pygame.image.load('assets/selecao/pokemon_selecionado_grande.png').get_height()*3)),
            'pokemon1D' : pygame.transform.scale(pygame.image.load('assets/selecao/pokemon_selecionado_fainted.png'), (pygame.image.load('assets/selecao/pokemon_selecionado.png').get_width()*3,pygame.image.load('assets/selecao/pokemon_selecionado.png').get_height()*3)),
            'pokemonG0T' : pygame.transform.scale(pygame.image.load('assets/selecao/pokemon_selecao_grande_troca.png'), (pygame.image.load('assets/selecao/pokemon_selecao_grande.png').get_width()*3,pygame.image.load('assets/selecao/pokemon_selecao_grande.png').get_height()*3)),
            'pokemon0T' : pygame.transform.scale(pygame.image.load('assets/selecao/pokemon_selecao_troca.png'), (pygame.image.load('assets/selecao/pokemon_selecao.png').get_width()*3,pygame.image.load('assets/selecao/pokemon_selecao.png').get_height()*3)),
            'pokemonG1T' : pygame.transform.scale(pygame.image.load('assets/selecao/pokemon_selecionado_grande_troca.png'), (pygame.image.load('assets/selecao/pokemon_selecionado_grande.png').get_width()*3,pygame.image.load('assets/selecao/pokemon_selecionado_grande.png').get_height()*3)),
            'pokemon1T' : pygame.transform.scale(pygame.image.load('assets/selecao/pokemon_selecionado_troca.png'), (pygame.image.load('assets/selecao/pokemon_selecionado.png').get_width()*3,pygame.image.load('assets/selecao/pokemon_selecionado.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'),


            "burned" : pygame.transform.scale(pygame.image.load('assets/batalha/burn.png'), (pygame.image.load('assets/batalha/burn.png').get_width()*2,pygame.image.load('assets/batalha/burn.png').get_height()*2)),
            "paralized" : pygame.transform.scale(pygame.image.load('assets/batalha/paralize.png'), (pygame.image.load('assets/batalha/paralize.png').get_width()*2,pygame.image.load('assets/batalha/paralize.png').get_height()*2)),
            "poisoned" : pygame.transform.scale(pygame.image.load('assets/batalha/poison.png'), (pygame.image.load('assets/batalha/poison.png').get_width()*2,pygame.image.load('assets/batalha/poison.png').get_height()*2)),




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

        
        self.batalha = batalha


        self.state = {
            'player' : player,
            "selected": 0,
            "movendo": -1

        }







    def desenha(self):

        pos =  ((8,60),(263,27),(263,100),(263,173),(263,246),(263,319),(0,0))
        self.window.blit(self.assets['fundo'], (0,0))

        m= "pokemonG1"
        if self.state["selected"] == 0:
            m= "pokemonG1"
            y = 123
        else:
            m= "pokemonG0"
            y = 117

        if self.state["movendo"] == 0:
            m += "T"
        else:
            if self.state["player"].team[0].hp < 1:
                m += "D"

        self.window.blit(self.assets[m], pos[0])


        self.window.blit(pygame.transform.scale(self.state["player"].team[0].assets[2], (self.state["player"].team[0].assets[2].get_width()*3,self.state["player"].team[0].assets[2].get_height()*3)),(pos[0][0] +30, pos[0][1] + self.state["player"].team[0].assets[2].get_height()*3-10)) #  pokemon player
        self.window.blit(self.assets["fonte"].render(self.state["player"].team[0].nome.upper(), 100, "white"),(pos[0][0] + 100 ,pos[0][1] +50)) #player poke hp

        barra =self.assets["barraVerde"]
        if self.state["player"].team[0].hp/self.state["player"].team[0].maxHp < 0.2:
            barra = self.assets["barraVermelha"]
        elif self.state["player"].team[0].hp/self.state["player"].team[0].maxHp < 0.5:
            barra = self.assets["barraAmarela"]

        self.window.blit(pygame.transform.scale(barra,(barra.get_width()*16.2*(self.state["player"].team[0].hp/self.state["player"].team[0].maxHp),barra.get_height()*3)),(pos[0][0] + 90 ,pos[0][1]+y)) #barra 
        
        hpColourTxt = "black"
        self.window.blit(self.assets["fonte"].render(str(self.state["player"].team[0].hp), 100,hpColourTxt),(pos[0][0] + 130 ,pos[0][1]+132)) #player poke hp
        self.window.blit(self.assets["fonte"].render(str(self.state["player"].team[0].maxHp), 100,hpColourTxt),(pos[0][0] + 200 ,pos[0][1]+132)) #player poke hp

        if self.state["player"].team[0].condition in ["burned","paralized","poisoned"]:
            self.window.blit(self.assets[ self.state["player"].team[0].condition],(pos[0][0] + 90 ,pos[0][1]+90))



        for i in range(1,6):

            m = ""
            if self.state["selected"] == i:
                m ='pokemon1'
                y =27
            else:
                m = 'pokemon0'
                y = 24

            if self.state["movendo"] == i:
                m += "T"
            else:
                if self.state["player"].team[i].hp < 1:
                    m += "D"
            
            self.window.blit(self.assets[m], pos[i])

            self.window.blit(pygame.transform.scale(self.state["player"].team[i].assets[2], (self.state["player"].team[i].assets[2].get_width()*3,self.state["player"].team[i].assets[2].get_height()*3)),(pos[i][0], pos[i][1])) #  pokemon player

            barra =self.assets["barraVerde"]
            if self.state["player"].team[i].hp/self.state["player"].team[i].maxHp < 0.2:
                barra = self.assets["barraVermelha"]
            elif self.state["player"].team[i].hp/self.state["player"].team[i].maxHp < 0.5:
                barra = self.assets["barraAmarela"]

            self.window.blit(pygame.transform.scale(barra,(barra.get_width()*16.2*(self.state["player"].team[i].hp/self.state["player"].team[i].maxHp),barra.get_height()*3)),(pos[i][0] + 287 ,pos[i][1]+y)) #barra 
            self.window.blit(self.assets["fonte"].render(str(self.state["player"].team[i].hp), 100,hpColourTxt),(pos[i][0] + 325 ,pos[i][1]+34))
            self.window.blit(self.assets["fonte"].render(str(self.state["player"].team[i].maxHp), 100,hpColourTxt),(pos[i][0] + 395 ,pos[i][1]+34)) #player poke hp

            if self.state["player"].team[i].condition in ["burned","paralized","poisoned"]:
                self.window.blit(self.assets[ self.state["player"].team[i].condition],(pos[i][0] + 100 ,pos[i][1]+42))


            
            self.window.blit(self.assets["fonte"].render(self.state["player"].team[i].nome.upper(), 100, "white"),(pos[i][0] + 100 ,pos[i][1]+5)) #player poke hp


        self.window.blit(self.assets["fonte"].render("Choose a POKéMON.", 100,"black"),(40,412)) #text
        if self.batalha != 0:
            if self.state["selected"] == 6:
                self.window.blit(self.assets["cancel1"],(553, 400)) #cancel
            else:
                self.window.blit(self.assets["cancel0"],(553, 400)) #cancel


        


        pygame.display.update()

    def update(self):

        



    

        for evento in pygame.event.get():
            if evento.type == pygame.QUIT:
                if self.batalha != 0:
                    if self.batalha[0] == 3 or self.batalha[0] == 2:
                        return 'sair'
                return 'sair', None
            if evento.type == pygame.KEYDOWN and evento.key == pygame.K_SPACE:
                
                if self.batalha != 0 and self.state["selected"] == 6:
                    if self.batalha[0] == 1:
                        return "batalha"
                    if self.batalha[0] == 2:
                        return "box"
                    if self.batalha[0] == 3:
                        self.window = pygame.display.set_mode((624, 672))
                        return self.batalha[1]
                    if self.batalha[0] == 4:
                        self.window = pygame.display.set_mode((624, 672))
                        return "bag", self.batalha[2]
                if self.state["player"].team[self.state["selected"]].hp >0:
                    if self.batalha == 0 or self.batalha[0] == 1:
                        temp = self.state["player"].team[0]
                        self.state["player"].team[0] = self.state["player"].team[self.state["selected"]]
                        self.state["player"].team[self.state["selected"]]  =temp

                        return "batalha"

                    if self.batalha[0] == 2:

                        self.state["player"].team[self.state["selected"]] =self.batalha[1]
                        return "box"

                if self.batalha != 0:
                    if self.batalha[0] == 3:
                        if self.state["movendo"] != -1:
                            temp = self.state["player"].team[self.state["movendo"]]
                            self.state["player"].team[self.state["movendo"]] = self.state["player"].team[self.state["selected"]]
                            self.state["player"].team[self.state["selected"]]  = temp
                            self.state["movendo"] = -1
                        else:
                            self.state["movendo"] = self.state["selected"]
                    if self.batalha[0] == 4:
                        self.batalha[1].usa(self.state["player"].team[self.state["selected"]])
                        temp = []
                        x = False
                        for i in self.state["player"].bag:
                            if i != self.batalha[1]:
                                temp.append(i)
                            elif x == True:
                                temp.append(i)
                            else:
                                x = True
                        self.state["player"].bag = list(temp)
                        return "bag" , self.batalha[2]
                        

            l = 6
            if self.batalha == 0:
                l=5
            if evento.type == pygame.KEYDOWN:
                if evento.key == pygame.K_a or evento.key == pygame.K_LEFT:
                    self.state["selected"] = 0

                if self.state["selected"] < l:
                    if evento.key == pygame.K_d or evento.key ==pygame.K_RIGHT:
                        self.state["selected"] += 1
                    if evento.key == pygame.K_s or evento.key ==pygame.K_DOWN:
                        self.state["selected"] += 1

                if self.state["selected"] > 1:
                    if evento.key == pygame.K_w or evento.key ==pygame.K_UP:
                        self.state["selected"] -= 1
                
                if evento.key == pygame.K_ESCAPE:
                    if self.batalha != 0:
                        if self.batalha[0] == 1:
                            return "batalha"
                        if self.batalha[0] == 3:
                            self.window = pygame.display.set_mode((624, 672))
                            return self.batalha[1]
                        if self.batalha[0] == 2:
                            return "box"
                        if self.batalha[0] == 4:
                            return "bag" , self.batalha[2]

        return " "