import pygame
from random import randint
import math

SCREEN_X = 1280
SCREEN_Y = 720


# pygame setup
pygame.init()
pygame.display.set_caption("Rouglite")
screen = pygame.display.set_mode((SCREEN_X, SCREEN_Y))
clock = pygame.time.Clock()
running = True


class Player(pygame.sprite.Sprite):
    def __init__(self, pos, group):
        super().__init__(group)
        self.idle_image = self.image = pygame.image.load("Sprites/Idle/Player_right.png").convert_alpha()
        self.rect = self.image.get_rect(center = pos)
        self.direction = pygame.math.Vector2()
        self.speed = 2.25
        self.time = 0
        self.time_2 = 0
        self.SCORE = 0


        #health
        self.current_health = 1000 #health
        self.maximum_health = 1000
        self.health_bar_lenght = 500
        self.health_ratio = self.maximum_health / self.health_bar_lenght
        self.target_health = 1000
        self.health_change_speed = 5

        # Shooting
        self.can_shoot = True
        self.shoot_cooldown_time = 100  # Milliseconds
        self.shoot_cooldown = 0

        

        # animations
        self.image_direction = 1
        self.animation_timer = 0
        self.current_sprite = 0
        self.running_sprites = []
        for i in range(6):
            self.running_sprites.append(pygame.image.load("Sprites/Running/Player_run_right_"+str(i+1)+".png").convert_alpha())

    

    def get_damage(self, amount):
        if self.target_health > 0:
            self.target_health -= amount
        if self.target_health <= 0:
            self.target_health = 0


    def get_health(self, amnount):
        if self.target_health < self.maximum_health:
            self.target_health += amnount
        if self.target_health >= self.maximum_health:
            self.target_health = self.maximum_health

    

    def ahealth_bar(self):

        self.current_health += (self.target_health-self.current_health)/10
        transition_colour = (0, 255, 0)
        health_bar_width = self.current_health/self.health_ratio
        transition_bar_width = self.target_health/self.health_ratio

        if self.current_health > self.target_health:
            health_bar_width = self.target_health/self.health_ratio
            transition_bar_width = self.current_health/self.health_ratio
            transition_colour = (255, 255, 0)

        health_bar_rect = pygame.Rect(10,45,health_bar_width,25)
        transition_bar_rect = pygame.Rect(10,45,transition_bar_width,25)
        
        pygame.draw.rect(screen, transition_colour, transition_bar_rect)
        pygame.draw.rect(screen,(255,0,0),health_bar_rect)
        pygame.draw.rect(screen,(255,255,255), (10,45,self.health_bar_lenght,25),4)
        
    def input(self):
        keys = pygame.key.get_pressed()
        speed_multiplier_x = 0
        speed_multiplier_y = 0

        if self.direction.x > 0:
            self.image_direction = 1
        elif self.direction.x < 0:
            self.image_direction = -1

        if keys[pygame.K_w]:
            speed_multiplier_y = -1
        elif keys[pygame.K_s]:
            speed_multiplier_y = 1
    
   # Check if A and D keys are pressed
        if keys[pygame.K_a]:
            speed_multiplier_x = -1
        elif keys[pygame.K_d]:
            speed_multiplier_x = 1

         # Calculate movement direction based on keys pressed
        self.direction.x = speed_multiplier_x
        self.direction.y = speed_multiplier_y


        self.direction.y = (keys[pygame.K_s]) - (keys[pygame.K_w])
        self.direction.x = (keys[pygame.K_d]) - (keys[pygame.K_a])

        if self.direction.x != 0 and self.direction.y != 0:
            self.direction.normalize_ip()
    
        self.direction.x *= self.speed
        self.direction.y *= self.speed


        if pygame.mouse.get_pressed()[0] and self.can_shoot:
                    direction = -(pygame.math.Vector2(self.rect.topleft) - pygame.mouse.get_pos() - camera_group.offset)
                    direction = pygame.math.Vector2.normalize(direction)
                    new_projectile = Projectile(self.rect.center, direction, camera_group)
                    self.can_shoot = False
                    self.shoot_cooldown = pygame.time.get_ticks()
                    self.shoot = True
            

    def update(self):
        self.input()
        self.ahealth_bar()
        self.rect.center += self.direction * self.speed

        now = pygame.time.get_ticks()
        if now - self.shoot_cooldown > self.shoot_cooldown_time:
            self.can_shoot = True

        if now - self.animation_timer > 50:
            if pygame.Vector2.length(self.direction) == 0:
                self.image = self.idle_image
                self.current_sprite = 0
            else:
                self.animation_timer = pygame.time.get_ticks()
                self.current_sprite += 1
                if self.current_sprite > len(self.running_sprites)-1:
                    self.current_sprite = 0
                self.image = self.running_sprites[self.current_sprite]

class Arm(pygame.sprite.Sprite):
    def __init__(self, pos, group):
        super().__init__(group)
        self.base_image = pygame.image.load("Sprites/minigun.png").convert_alpha()
        self.base_image = pygame.transform.scale(self.base_image, (40, 15))
        
        self.image_direction = 1
        
        self.direction = pygame.Vector2(0, 0)
        self.speed = 10
        angle = math.degrees(math.atan2(-self.direction.y, self.direction.x)) 
        self.image = pygame.transform.rotate(self.base_image, angle)
        self.rect = self.image.get_rect(center = pos)

    def update(self):
        self.rect.center = player.rect.center
        self.direction = -(pygame.math.Vector2(self.rect.topleft) - pygame.mouse.get_pos() - camera_group.offset)
        self.direction = pygame.math.Vector2.normalize(self.direction)
        angle = math.degrees(math.atan2(-self.direction.y, self.direction.x))
        if(abs(angle)<90):
            self.image_direction = 1
        else:
            self.image_direction = -1
        self.rect.center -= pygame.Vector2(math.sin(math.radians(angle-90))*20, math.cos(math.radians(angle-90))*20)
        if(abs(angle)>90):
            angle = math.degrees(math.atan2(self.direction.y, self.direction.x))+180
        self.image = pygame.transform.rotate(self.base_image, angle)
        

class Projectile(pygame.sprite.Sprite):
    def __init__(self, pos, direction, group):
        super().__init__(group)
        self.image = pygame.image.load("Sprites/bullet.png").convert_alpha()
        self.image_direction = 1
        
        self.direction = direction
        self.speed = 15
        angle = math.degrees(math.atan2(-self.direction.y, self.direction.x)) 

        self.image = pygame.transform.rotate(self.image, angle)
        self.rect = self.image.get_rect(center = pos)
        self.rect.center += self.direction * 50

        self.life_timer = 10000 # Milliseconds
        self.spawned_time = pygame.time.get_ticks()

    def collision(self):
        for enemy in enemies:
            distance = pygame.math.Vector2.length(pygame.math.Vector2(enemy.rect.center) - self.rect.center)
            if distance < 20:
                enemy.damage(1)
                self.kill()
                break
        for enemy in enemies2:
            distance = pygame.math.Vector2.length(pygame.math.Vector2(enemy.rect.center) - self.rect.center)
            if distance < 20:
                enemy.damage(1)
                self.kill()
                break

    def update(self):
        self.rect.center += self.direction * self.speed
        self.collision()

        now = pygame.time.get_ticks()
        if now - self.spawned_time > self.life_timer:
            self.kill()

class Enemy(pygame.sprite.Sprite):
    def __init__(self, pos, group, scale):
        super().__init__(group)
        self.original_image = pygame.image.load("Sprites/bomb.png").convert_alpha()
        self.image = pygame.transform.scale(self.original_image, (int(self.original_image.get_width() * scale), int(self.original_image.get_height() * scale)))
        self.phase_2 = pygame.image.load("Sprites/bomb_phase2.png").convert_alpha()
        self.phase_2 = pygame.transform.scale(self.phase_2, (int(self.phase_2.get_width() * scale), int(self.phase_2.get_height() * scale)))
        self.rect = self.image.get_rect(center=pos)
        self.pos = self.rect.center
        self.direction = pygame.math.Vector2()
        self.speed = 2
        self.health = 3
        self.death = pygame.mixer.Sound("Sounds/explosion.wav")

        self.image_direction = 1

    def damage(self, damage):
        self.health -= 1
        if self.health == 1:
            self.image = self.phase_2
        if self.health <= 0:
            enemies.remove(self)
            self.kill()
            self.death.play()
            player.SCORE += 1


    def update(self):
        self.direction = pygame.math.Vector2(player.rect.center) - self.rect.center
        if pygame.math.Vector2.length(self.direction) < 20:
            if self.image == self.phase_2:
                player.get_damage(100)
                self.kill()
                enemies.remove(self)
                player.time = 0
                self.death.play()
            else:
                player.get_damage(200)
                self.kill()
                enemies.remove(self)
                player.time = 0
                self.death.play()

        self.pos += self.direction.normalize() * self.speed
        self.rect.center = (round(self.pos.x), round(self.pos.y))


class Enemy_2(pygame.sprite.Sprite):
    def __init__(self, pos, group, scale):
        super().__init__(group)
        self.original_image = pygame.image.load("Sprites/atom_bomb.png").convert_alpha()
        self.image = pygame.transform.scale(self.original_image, (int(self.original_image.get_width() * scale), int(self.original_image.get_height() * scale)))
        self.rect = self.image.get_rect(center = pos)
        self.pos = self.rect.center
        self.direction = pygame.math.Vector2()
        self.speed = 1
        self.health = 10

        self.image_direction = 1
        self.death = pygame.mixer.Sound("Sounds/explosion_atomic.wav")

    def damage(self, damage):
        self.health -= 1
        if self.health <= 0:
            enemies2.remove(self)
            self.kill()
            self.death.play()
            player.SCORE += 3

    def update(self):
        self.direction = pygame.math.Vector2(player.rect.center) - self.rect.center
        if pygame.math.Vector2.length(self.direction) < 20:
                player.get_damage(500)
                self.kill()
                enemies2.remove(self)
                player.time = 0
                self.death.play()

        self.pos += self.direction.normalize() * self.speed
        self.rect.center = (round(self.pos.x), round(self.pos.y))

class CameraGroup(pygame.sprite.Group):
    def __init__(self):
        super().__init__()
        self.display_surface = pygame.display.get_surface()

        self.offset = pygame.math.Vector2(300, 100)
        self.half_w = self.display_surface.get_size()[0] // 2
        self.half_h = self.display_surface.get_size()[1] // 2

    def center_target_camera(self, target):
        self.offset.x = target.rect.centerx - self.half_w
        self.offset.y = target.rect.centery - self.half_h

    def custom_draw(self, player):
        self.center_target_camera(player)

        for sprite in sorted(self.sprites(), key=lambda sprite: sprite.rect.centery):
            offset_pos = sprite.rect.topleft - self.offset
            if sprite.image_direction == -1:
                self.display_surface.blit(pygame.transform.flip(sprite.image, True, False), offset_pos)
            else:
                self.display_surface.blit(sprite.image, offset_pos)
            
def spawn_enemy():
        if len(enemies) < 5 + (player.time_2):
            random_x = randint(-1000, 1000)
            random_y = randint(-1000, 1000)
            enemies.append(Enemy((player.rect.centerx + random_x, player.rect.centery + random_y), camera_group, 0.05))
           

def spawn_enemy2():
        if len(enemies2) < 2 + int(player.time_2):
            random_x = randint(-1000, 1000)
            random_y = randint(-1000, 1000)
            enemies2.append(Enemy_2((player.rect.centerx + random_x, player.rect.centery + random_y), camera_group, 0.5))

spawn_enemy_event = pygame.USEREVENT + 1

smallfont = pygame.font.Font("assets/font.ttf", 25)

def score(score):
    text = smallfont.render("Score: " + str(score), True, "black")
    screen.blit(text, [10,15])



#Setup
    
camera_group = CameraGroup()
enemies = []
enemies2 = []




player = Player((500, 500), camera_group)
Arm((0, 0), camera_group)
pygame.time.set_timer(spawn_enemy_event, 100)

crosshair_img = pygame.transform.scale(pygame.image.load('Sprites/crosshair.png'), (30, 30))
cursor = pygame.cursors.Cursor((0, 0), crosshair_img)
pygame.mouse.set_cursor(cursor)
pygame.mouse.set_visible(True | False)


while running:
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            running = False
            pygame.quit()

        if e.type == spawn_enemy_event:
            spawn_enemy()
      

        if e.type == spawn_enemy_event:
            spawn_enemy2()

    if player.time > 3:
        if player.current_health < 999 and player.target_health > 0:
            player.get_health(100)
            player.time -= 1
        else:
            pass
    if player.target_health <= 0:
        running == False
        print("Your score was " + str(player.SCORE))
        pygame.quit()

    screen.fill("cyan")

    score(player.SCORE)


    camera_group.update()
    camera_group.custom_draw(player)



    pygame.display.flip()

    clock.tick(60)
    player.time += 0.016
    player.time_2 += 0.005