
import batalha
import random
import pygame

game = True

pokedex, x,y = batalha.init()






def battleLoop(activepokemon,opponentpokemon, chosenatak): #passa por cada passo de uma batalha, devolvendo as mensagens para serem mostradas e dando o dano

    mensagem1 = []
    mensagem2= []



    spd1 = activepokemon.spd
    spd2 = opponentpokemon.spd



    if activepokemon.condition == "paralized":
        spd1 /=2
    if opponentpokemon.condition == "paralized":
        spd2 /=2

    



    if activepokemon.moveset[chosenatak].effeito[0] == "priority":
        spd1 += 8000




    if spd1 > spd2:
        first = [activepokemon,chosenatak,0,"player"]
        second = [opponentpokemon,random.randint(0,len(opponentpokemon.moveset)-1),1,"opponent"]
    elif spd1 < spd2:
        first = [opponentpokemon,random.randint(0,len(opponentpokemon.moveset)-1),1,"opponent"]
        second = [activepokemon,chosenatak,0,"player"]
    else:
        if random.random() >= 0.5:
            first = [activepokemon,chosenatak,0,"player"]
            second = [opponentpokemon,random.randint(0,len(opponentpokemon.moveset)-1),1,"opponent"]
        else:
            first = [opponentpokemon,random.randint(0,len(opponentpokemon.moveset)-1),1,"opponent"]
            second = [activepokemon,chosenatak,0,"player"]

        


    dmg, mensagem1= batalha.damageCalc(first[0].moveset[first[1]],first[0],second[0],first[2])
    second[0].hp -= dmg
    if first[0] == activepokemon:
        first[0].moveset[first[1]].PP -= 1

    if batalha.checkDeath(second[0]):
        dmg , mensagem2= batalha.damageCalc(second[0].moveset[second[1]],second[0],first[0],second[2])
        first[0].hp -= dmg
        if second[0] == activepokemon:
            second[0].moveset[second[1]].PP -= 1
        




    batalha.DOT(activepokemon)
    if  batalha.checkDeath(activepokemon):
        batalha.DOT(opponentpokemon)
    else:
        pygame.mixer.Sound('assets/sound/effect/fainting.mp3').play()
        mensagem2 += [activepokemon.nome + " has fainted"]
        activepokemon.consition = None
        



    if not batalha.checkDeath(opponentpokemon):
        pygame.mixer.Sound('assets/sound/effect/fainting.mp3').play()
        mensagem2 += [opponentpokemon.nome + " has fainted"]
        opponentpokemon.consition = None
    #check death


    return mensagem1 + mensagem2




    

    

    

    
