import pygame

class Player(pygame.sprite.Sprite):
    def __init__(self, team, nome):
        pygame.sprite.Sprite.__init__(self)
        self.assets = {
            'frente' : pygame.transform.scale(pygame.image.load('assets/player/player_frente_parado.png'), (42, 57)),
            'costas' : pygame.transform.scale(pygame.image.load('assets/player/player_costas_parado.png'), (42, 57)),
            'esquerda' : pygame.transform.scale(pygame.image.load('assets/player/player_esquerda_parado.png'), (42, 57)),
            'direita' : pygame.transform.scale(pygame.image.load('assets/player/player_direita_parado.png'), (42, 57)),
            'f1' : pygame.transform.scale(pygame.image.load('assets/player/player_frente_andando1.png'), (45, 57)),
            'f2' : pygame.transform.scale(pygame.image.load('assets/player/player_frente_andando2.png'), (45, 57)),
            'c1' : pygame.transform.scale(pygame.image.load('assets/player/player_costas_andando1.png'), (42, 60)),
            'c2' : pygame.transform.scale(pygame.image.load('assets/player/player_costas_andando2.png'), (42, 60)),
            'e1' : pygame.transform.scale(pygame.image.load('assets/player/player_esquerda_andando1.png'), (42, 54)),
            'e2' : pygame.transform.scale(pygame.image.load('assets/player/player_esquerda_andando2.png'), (42, 54)),
            'd1' : pygame.transform.scale(pygame.image.load('assets/player/player_direita_andando1.png'), (42, 54)),
            'd2' : pygame.transform.scale(pygame.image.load('assets/player/player_direita_andando2.png'), (42, 54)),
        }

        self.clock = pygame.time.Clock()

        self.image = self.assets['frente']
        self.rect = self.image.get_rect()
        self.nome = nome
        self.money = 100000
        self.bag = []
        self.cheater = False
        

        self.team = team
        self.state = {
            'olhando' : 'frente',
            'andando' : False,
            'i' : 1,
            'conta' : 0,
        }


    def update(self, paredes, fala):

        dt = self.clock.tick(60) / 1000

        
        self.image = self.assets[self.state['olhando']]
        self.state['andando'] = False

        accel = 200*dt
        speed = [0,0]
        keys = pygame.key.get_pressed() 

        if keys[pygame.K_LSHIFT]:
            accel *= 2

        if (keys[pygame.K_a] or keys[pygame.K_LEFT]) and self.rect.x >=0 and fala == 0:
            speed[0] -= accel 
            self.state['olhando'] = 'esquerda'
            self.state['andando'] = True

        elif (keys[pygame.K_d] or keys[pygame.K_RIGHT]) and self.image.get_width() + self.rect.x <= 624 and fala == 0:
            speed[0] += accel
            self.state['olhando'] = 'direita'
            self.state['andando'] = True

        elif (keys[pygame.K_w] or keys[pygame.K_UP]) and self.rect.y >= 0 and fala == 0:
            speed[1] -= accel
            self.state['olhando'] = 'costas'
            self.state['andando'] = True

        elif (keys[pygame.K_s] or keys[pygame.K_DOWN]) and self.image.get_height() + self.rect.y <= 672 and fala == 0:
            speed[1] += accel
            self.state['olhando'] = 'frente'
            self.state['andando'] = True
        
        self.rect.x += speed[0]


        self.state['conta'] += dt
        if self.state['conta'] > 0.2:
            if self.state['i'] == 1:
                self.state['i'] = 2
            else:
                self.state['i'] = 1
            self.state['conta'] = 0


        if self.state['andando']:
            self.image = self.assets[self.state['olhando'][0] + str(self.state['i'])]

        for parede in paredes:
            if pygame.Rect.colliderect(self.rect, parede):
                speed[0] *= -1
                self.rect.x += speed[0]

        
        self.rect.y += speed[1]
        for parede in paredes:
            if pygame.Rect.colliderect(self.rect, parede):
                speed[1] *= -1
                self.rect.y += speed[1]
                


        