import pygame
import math
import os

class Player:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.base_speed = 5
        self.speed_bonus = 0
        self.speed = self.base_speed
        self.radius = 20
        
        # Health system
        self.max_health = 100
        self.health = 100
        self.invincible_frames = 0
        self.invincible_duration = 30
        
        # Shooting stats
        self.fire_delay = 8
        self.fire_timer = 0
        self.bullet_count = 1
        self.pierce = 0
        self.damage_bonus = 0.0
        
        # Special abilities
        self.exploding_kills = False
        self.damage_reduction = 0.0
        self.lifesteal = 0
        self.invincible_on_hit = False
        self.score_multiplier = 1.0
        
        self.active_powerups = []
        self.rect = pygame.Rect(x - self.radius, y - self.radius, self.radius * 2, self.radius * 2)
        
        # Load player image
        self.image = None
        try:
            script_dir = os.path.dirname(os.path.abspath(__file__))
            image_path = os.path.join(script_dir, "assets", "green.png")
            if os.path.exists(image_path):
                self.image = pygame.image.load(image_path).convert_alpha()
                self.image = pygame.transform.scale(self.image, (40, 40))
                print("✓ green.png loaded!")
            else:
                print(f"green.png not found at {image_path}, using circle")
        except Exception as e:
            print(f"Could not load green.png: {e}, using circle")

    def update(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_w]: self.y -= self.speed
        if keys[pygame.K_s]: self.y += self.speed
        if keys[pygame.K_a]: self.x -= self.speed
        if keys[pygame.K_d]: self.x += self.speed

        # Boundaries
        self.x = max(self.radius, min(800 - self.radius, self.x))
        self.y = max(self.radius, min(600 - self.radius, self.y))
        self.rect.center = (self.x, self.y)

        if self.fire_timer > 0:
            self.fire_timer -= 1
        
        if self.invincible_frames > 0:
            self.invincible_frames -= 1

    def take_damage(self):
        if self.invincible_frames > 0:
            return False
        
        damage = 20 * (1 - self.damage_reduction)
        self.health -= damage
        
        if self.invincible_on_hit:
            self.invincible_frames = self.invincible_duration
        
        return self.health <= 0

    def heal(self, amount):
        self.health = min(self.max_health, self.health + amount)

    def can_shoot(self):
        return self.fire_timer == 0

    def shoot(self, target_pos):
        self.fire_timer = self.fire_delay
        
        from bullet import Bullet
        bullets = []
        
        if self.bullet_count == 1:
            angles = [0]
        elif self.bullet_count == 2:
            angles = [-0.15, 0.15]
        elif self.bullet_count == 3:
            angles = [-0.2, 0, 0.2]
        elif self.bullet_count == 4:
            angles = [-0.25, -0.1, 0.1, 0.25]
        else:
            angles = [-0.3, -0.15, 0, 0.15, 0.3]
        
        for i in range(min(self.bullet_count, len(angles))):
            bullets.append(Bullet(self.x, self.y, target_pos[0], target_pos[1], 
                                  angles[i], self.pierce, self.damage_bonus))
        return bullets

    def draw(self, screen):
        if self.image:
            if self.invincible_frames > 0:
                if (pygame.time.get_ticks() % 100) < 50:
                    temp_image = self.image.copy()
                    temp_image.set_alpha(100)
                    screen.blit(temp_image, (self.x - self.radius, self.y - self.radius))
                else:
                    screen.blit(self.image, (self.x - self.radius, self.y - self.radius))
            else:
                screen.blit(self.image, (self.x - self.radius, self.y - self.radius))
        else:
            if self.invincible_frames > 0 and (pygame.time.get_ticks() % 100) < 50:
                pygame.draw.circle(screen, (200, 200, 200), (int(self.x), int(self.y)), self.radius)
            else:
                pygame.draw.circle(screen, (0, 255, 0), (int(self.x), int(self.y)), self.radius)
        
        # Health bar
        bar_width = 40
        bar_height = 6
        health_percent = self.health / self.max_health
        pygame.draw.rect(screen, (100, 0, 0), (self.x - bar_width//2, self.y - self.radius - 8, bar_width, bar_height))
        pygame.draw.rect(screen, (0, 255, 0), (self.x - bar_width//2, self.y - self.radius - 8, bar_width * health_percent, bar_height))