import pygame
import loopBatalha
import batalha

class TelaBatalha:
    def __init__(self,player,enemy): #tela de batalha, liga as telas de troca e bag tambem para serem usadas durate a batalha, aqui seleciona o ataque para ser rodado pelo loopBatalha e mostra as mensagens recebidas
        pygame.init()
        self.window = pygame.display.set_mode((720, 480))
        pygame.display.set_caption('Pokémon')

        self.assets = {
            'bg' : pygame.transform.scale(pygame.image.load('assets/batalha/background.png'), (720, 336)),
            'caixaTXT' : pygame.transform.scale(pygame.image.load('assets/batalha/caixa_batalha.png'), (720, 144)),
            '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)),
            'caixaAtaque' : pygame.transform.scale(pygame.image.load('assets/batalha/caixa_ataque.png'), (pygame.image.load('assets/batalha/caixa_ataque.png').get_width()*3,pygame.image.load('assets/batalha/caixa_ataque.png').get_height()*3)),
            'caixaOpcoes' : pygame.transform.scale(pygame.image.load('assets/batalha/caixa_opcoes.png'), (pygame.image.load('assets/batalha/caixa_opcoes.png').get_width()*3,pygame.image.load('assets/batalha/caixa_opcoes.png').get_height()*3)),
            "enemyInfo" : pygame.transform.scale(pygame.image.load('assets/batalha/caixa_pokemon_inimigo.png'), (pygame.image.load('assets/batalha/caixa_pokemon_inimigo.png').get_width()*3,pygame.image.load('assets/batalha/caixa_pokemon_inimigo.png').get_height()*3)),
            "playerInfo" : pygame.transform.scale(pygame.image.load('assets/batalha/caixa_pokemon_player.png'), (pygame.image.load('assets/batalha/caixa_pokemon_player.png').get_width()*3,pygame.image.load('assets/batalha/caixa_pokemon_player.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()*3,pygame.image.load('assets/batalha/burn.png').get_height()*3)),
            "paralized" : pygame.transform.scale(pygame.image.load('assets/batalha/paralize.png'), (pygame.image.load('assets/batalha/paralize.png').get_width()*3,pygame.image.load('assets/batalha/paralize.png').get_height()*3)),
            "poisoned" : pygame.transform.scale(pygame.image.load('assets/batalha/poison.png'), (pygame.image.load('assets/batalha/poison.png').get_width()*3,pygame.image.load('assets/batalha/poison.png').get_height()*3)),


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


        }

        x = batalha.init()
        self.pokedex = x[0]

        self.state = {
            'player' : player,
            'enemy' : enemy,
            "activePokemon": player.team[0],
            "opponentPokemon": enemy.team[0],
            "mensagem" : ["what will you do?"],
            "vitoria" : False,
            "action" : False,
            "msgPos" : 0,
            "EnemyPokemon" : 0
        }

        self.selectPos = [0,0]


        self.tela = 0 # 0 = opcoes, 1 = ataques, 2 = pokemons, 3 = bag?, -1 = somente mensagem

    def posicoesF (pos):
        dic = {"[0, 0]":0,"[1, 0]":1,"[0, 1]": 2,"[1, 1]":3}
        return dic[f"{pos}"]


    def desenha(self):
        
        
        if self.state["vitoria"] == False:

            self.window.blit(self.assets["bg"],(0,0)) #fundo
            self.window.blit(self.assets["caixaTXT"],(0,337)) #caixa texto

            self.window.blit(self.assets["fonteM"].render(self.state["mensagem"][self.state["msgPos"]], 100, "white"),(35,357)) 



            self.window.blit(pygame.transform.scale(self.state["activePokemon"].assets[1], (self.state["activePokemon"].assets[1].get_width()*3,self.state["activePokemon"].assets[1].get_height()*3)),(100, 336 - self.state["activePokemon"].assets[1].get_height()*3)) #  pokemon player
            self.window.blit(pygame.transform.scale(self.state["opponentPokemon"].assets[0], (self.state["opponentPokemon"].assets[0].get_width()*3,self.state["opponentPokemon"].assets[0].get_height()*3)),(475,50)) # pokemon opponent
            
            self.window.blit(self.assets["playerInfo"],( 710 - self.assets["playerInfo"].get_width() ,330 - self.assets["playerInfo"].get_height())) # player info
            self.window.blit(self.assets["fonte"].render(self.state["activePokemon"].nome.upper(), 100, "black"),(710 - self.assets["playerInfo"].get_width() +45 ,330 - self.assets["playerInfo"].get_height() + 7)) #player poke name
            self.window.blit(self.assets["fonte"].render(str(self.state["activePokemon"].hp) + "/" + str(self.state["activePokemon"].maxHp), 100, "black"),(585 ,280)) #player poke hp

            self.window.blit(self.assets["enemyInfo"],(10,15)) #opponent info
            self.window.blit(self.assets["fonte"].render(self.state["opponentPokemon"].nome.upper(), 100, "black"),(10 +20 ,15 + 7)) #opponent poke name


            barra1 =self.assets["barraVerde"]
            if self.state["opponentPokemon"].hp/self.state["opponentPokemon"].maxHp < 0.2:
                barra1 = self.assets["barraVermelha"]
            elif self.state["opponentPokemon"].hp/self.state["opponentPokemon"].maxHp < 0.5:
                barra1 = self.assets["barraAmarela"]
            
            barra2 =self.assets["barraVerde"]
            if self.state["activePokemon"].hp/self.state["activePokemon"].maxHp < 0.2:
                barra2 = self.assets["barraVermelha"]
            elif self.state["activePokemon"].hp/self.state["activePokemon"].maxHp < 0.5:
                barra2 = self.assets["barraAmarela"]

            if self.state["opponentPokemon"].hp > 0 and self.state["activePokemon"].hp > 0:
                self.window.blit(pygame.transform.scale(barra1,(barra1.get_width()*16.2*(self.state["opponentPokemon"].hp/self.state["opponentPokemon"].maxHp),barra1.get_height()*3)),(126,66)) #barra inimigo
                self.window.blit(pygame.transform.scale(barra2,(barra2.get_width()*16.2*(self.state["activePokemon"].hp/self.state["activePokemon"].maxHp),barra2.get_height()*3)),(541,270)) #barra player


            if self.tela == 0:
                self.window.blit(self.assets["caixaOpcoes"],(720 - self.assets["caixaOpcoes"].get_width(),337))
                setaPos = [[385,550],[375,420]]
            elif self.tela == 1:
                self.window.blit(self.assets["caixaAtaque"],(0,337))
                setaPos = [[50,260],[370,425]]
                self.window.blit(self.assets["fonte"].render(self.state["activePokemon"].moveset[0].nome.upper(), 100, "black"),(75, 365))   # move 1
                self.window.blit(self.assets["fonte"].render(self.state["activePokemon"].moveset[1].nome.upper(), 100, "black"),(285, 365)) # move 2 
                self.window.blit(self.assets["fonte"].render(self.state["activePokemon"].moveset[2].nome.upper(), 100, "black"),(75, 420)) # move 3
                self.window.blit(self.assets["fonte"].render(self.state["activePokemon"].moveset[3].nome.upper(), 100, "black"),(285, 420)) # move 3
                self.window.blit(self.assets["fonte"].render(str(self.state["activePokemon"].moveset[TelaBatalha.posicoesF(self.selectPos)].PP), 100, "black"),(609, 365)) # move pp 
                self.window.blit(self.assets["fonte"].render(str(self.state["activePokemon"].moveset[TelaBatalha.posicoesF(self.selectPos)].maxPP), 100, "black"),(664, 365)) # move max pp
                self.window.blit(self.assets["fonte"].render((self.state["activePokemon"].moveset[TelaBatalha.posicoesF(self.selectPos)].tipo), 100, (25,25,25)),(590, 415)) # move tipo atk


            if self.state["activePokemon"].condition in ["burned","paralized","poisoned"]:
                self.window.blit(self.assets[self.state["activePokemon"].condition],(620, 233))

            if self.state["opponentPokemon"].condition in ["burned","paralized","poisoned"]:
                self.window.blit(self.assets[self.state["opponentPokemon"].condition],(208, 30))





            if self.tela != -1:
                self.window.blit(self.assets["setaPreta"],(setaPos[0][self.selectPos[0]],setaPos[1][self.selectPos[1]],30,30)) #seletor
        else:

            self.window.blit(self.assets["bg"],(0,0)) #fundo
            self.window.blit(pygame.transform.scale(self.state["enemy"].assets["full"], (self.state["enemy"].assets["full"].get_width()*5,self.state["enemy"].assets["full"].get_height()*5)),(720//2 - self.state["enemy"].assets["full"].get_width()*5//2, 480//2- self.state["enemy"].assets["full"].get_height()*5//2)) #  mestre

            self.window.blit(self.assets["caixaTXT"],(0,337)) #caixa texto

            self.window.blit(self.assets["fonteM"].render(self.state["mensagem"][self.state["msgPos"]], 100, "white"),(35,357)) 
        
            

        pygame.display.update()

    def update(self):
       

        

        if self.state["activePokemon"] != self.state["player"].team[0] and self.state["action"] == True:
            self.state["activePokemon"] = self.state["player"].team[0]
            self.pokedex[-1].nome = "player"
            self.pokedex[-1].hp = self.state["activePokemon"].hp
            self.pokedex[-1].tipo = self.state["activePokemon"].tipo
            self.pokedex[-1].maxHp = self.state["activePokemon"].maxHp
            self.pokedex[-1].defesa = self.state["activePokemon"].defesa
            mensagem = loopBatalha.battleLoop(self.pokedex[-1],self.state["opponentPokemon"],-1)
            self.pokedex[-1].nome = "missingNo"
            self.state["activePokemon"].hp =self.pokedex[-1].hp 
            self.pokedex[-1].hp = 1
            self.pokedex[-1].defesa = 1
            self.state["mensagem"] = ["you switch to " + self.state["activePokemon"].nome]
            self.state["mensagem"] += mensagem
            self.tela = -1
            self.selectPos = [0,0]
            self.state["activePokemon"].condition = self.pokedex[-1].condition
            self.pokedex[-1].condition = " "
            self.state["action"] = False
        elif self.state["activePokemon"] != self.state["player"].team[0]:
            self.state["activePokemon"] = self.state["player"].team[0]
            
        if self.state["activePokemon"].hp <1:
            self.state["activePokemon"].hp = 0




        if self.state["activePokemon"].hp <1:
            morto = True
            for i in self.state["player"].team:
                if i.hp >0:
                    morto = False

            if morto == True:
                return "gameover",None
            else:
                return "troca", 0

        

        keys = pygame.key.get_pressed() 

        if keys[pygame.K_a] or keys[pygame.K_LEFT]:
            if self.selectPos[0] == 1:
                self.selectPos[0] = 0
        if keys[pygame.K_w]or keys[pygame.K_UP]:
            if self.selectPos[1] == 1:
                self.selectPos[1] = 0
        if keys[pygame.K_s] or keys[pygame.K_DOWN]:
            if self.selectPos[1] == 0:
                self.selectPos[1] = 1
        if keys[pygame.K_d] or keys[pygame.K_RIGHT]:
            if self.selectPos[0] == 0:
                self.selectPos[0] = 1



            


        for evento in pygame.event.get():
            if evento.type == pygame.QUIT:
                return "sair",None
            elif evento.type == pygame.KEYDOWN:
                if evento.key == pygame.K_SPACE:
                    if self.tela == 1:
                        if self.state["activePokemon"].moveset[TelaBatalha.posicoesF(self.selectPos)].PP != 0:
                            mensagem = loopBatalha.battleLoop(self.state["activePokemon"],self.state["opponentPokemon"],TelaBatalha.posicoesF(self.selectPos))
                            
                            self.tela = -1
                            self.selectPos = [0,0]
                            self.state["mensagem"] = mensagem
                        else:
                            self.state["mensagem"] = ["no PP"]
                    elif TelaBatalha.posicoesF(self.selectPos) == 0 and self.tela == 0:
                        self.tela = 1

                    elif self.tela == -1:
                        self.state["msgPos"] += 1
                        if self.state["msgPos"] == len(self.state["mensagem"]):
                            self.tela = 0
                            self.state["msgPos"] = 0
                            self.state["mensagem"] = ["what will you do?"]
                            if self.state["vitoria"] == True :
                                return "vitoria",None


                    elif self.tela == 0 and TelaBatalha.posicoesF(self.selectPos) == 2:
                        self.state["action"] = True
                        return "troca", [1,self.state["opponentPokemon"]]
                    elif self.tela == 0 and TelaBatalha.posicoesF(self.selectPos) == 1:
                        return "bag", "batalha"
                if evento.key == pygame.K_ESCAPE:
                    if self.tela == 1:
                        self.tela = 0
                    
        

        
        self.state["action"] = False


        if self.state["opponentPokemon"].hp <1 and self.state["vitoria"] == False:
            if len(self.state["enemy"].team) == self.state["EnemyPokemon"] + 1:
                if self.state["enemy"].nome in ["AGATHA", "LANCE","LORELEI", "BRUNO"]:
                    self.state["mensagem"] += [self.state["player"].nome   + " defeated ELITE FOUR "+ self.state["enemy"].nome]
                else:
                    self.state["mensagem"] += [self.state["player"].nome   + " defeated CHAMPION "+ self.state["enemy"].nome]

                self.state["mensagem"] += [self.state["enemy"].mensagem]
                self.tela = -1
                self.state["vitoria"] = True

            else:
                self.state["EnemyPokemon"] += 1
                self.state["opponentPokemon"] = self.state["enemy"].team[self.state["EnemyPokemon"]]
                self.state["mensagem"] += [self.state["enemy"].nome + " sent out a " + self.state["opponentPokemon"].nome]
                        


        return "batalha", None