#  https://www.youtube.com/watch?v=f4coFYbYQzw&list=PLjcN1EyupaQkAQyBCYKyf1jt1M1PiRJEp 
import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
from pygame.sprite import Group
import pygame
from pygame.locals import *
import random


WIDTH = 600
HEIGHT = 800
WINDOW_SIZE = (WIDTH, HEIGHT)
FPS = 60

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

ROWS = 5
COLS = 5
alien_cooldown = 1000
last_alien_shot = pygame.time.get_ticks()

# load background image
bg = pygame.image.load("img/bg.png")  # FUCK THIS IN THE ASS FIXXED IT FUCK YEA
def draw_bg():
    screen.blit(bg, (0, 0))

# create spaceship class
class Spaceship(pygame.sprite.Sprite):
    def __init__(self,x ,y, health):
        pygame.sprite.Sprite.__init__(self) # review init function 
        self.image = pygame.image.load("img/spaceship.png")
        self.rect = self.image.get_rect()
        self.rect.center = [x,y]
        self.health_start = health
        self.health_remaining = health
        self.last_shot = pygame.time.get_ticks()
    def update(self):
        speed = 8
        cooldown = 400 # millisconds
        key =  pygame.key.get_pressed()
        if key[pygame.K_a] and self.rect.left > 0: # Why zero not width tried width would not move
            self.rect.x -= speed   # why speed not pixal + or - like others??
        if key[pygame.K_d] and self.rect.right < WIDTH:
            self.rect.x += speed
        
        time_now = pygame.time.get_ticks()

        # shoot
        if key[pygame.K_w] and time_now - self.last_shot > cooldown:
            bullet = Bullets(self.rect.centerx, self.rect.top)
            bullet_group.add(bullet)
            self.last_shot = time_now

        # update mask
        self.mask = pygame.mask.from_surface(self.image) # this does pixel perfect collison becasue it ignores anything that is not spaceship
        
        

        pygame.draw.rect(screen, RED, (self.rect.x, (self.rect.bottom +10), self.rect.width, 15))
        if self.health_remaining > 0:
            pygame.draw.rect(screen, GREEN, (self.rect.x, (self.rect.bottom +10), int(self.rect.width * (self.health_remaining / self.health_start)), 15) )


class Bullets(pygame.sprite.Sprite):
    def __init__(self,x ,y):
        pygame.sprite.Sprite.__init__(self) # review init function 
        self.image = pygame.image.load("img/bullet.png")
        self.rect = self.image.get_rect()
        self.rect.center = [x, y]
    def update(self):
        self.rect.y -= 5
        if self.rect.bottom < 0:
            self.kill() 
        if pygame.sprite.spritecollide(self, alien_group, True):
            self.kill()
    
# video for this https://www.youtube.com/watch?v=mqz1_wRSMwo
class Aliens(pygame.sprite.Sprite): 
    def __init__(self,x ,y):
        pygame.sprite.Sprite.__init__(self) 
        self.image = pygame.image.load("img/alien" + str(random.randint(1, 5)) + ".png")   # List this will call 1-5 but thought it would be 0-4 or is that place holders
        self.rect = self.image.get_rect()
        self.rect.center = [x, y]
        self.move_counter = 0
        self.move_direction = 1
    def update(self): 
        self.rect.x += self.move_direction
        self.move_counter += 1
        if abs(self.move_counter) > 75:
            self.move_direction *= -1
            self.move_counter *= self.move_direction

class Alien_Bullets(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self) 
        self.image = pygame.image.load("img/alien_bullet.png")
        self.rect = self.image.get_rect()
        self.rect.center = [x, y]
    def update(self):
        self.rect.y += 2
        if self.rect.top > HEIGHT:
            self.kill()
        if pygame.sprite.spritecollide(self, spaceship_group, False, pygame.sprite.collide_mask):
           self.kill()
           spaceship.health_remaining -= 1  

# create sprite groups
spaceship_group = pygame.sprite.Group()
bullet_group = pygame.sprite.Group()
alien_group = pygame.sprite.Group()
alien_bullet_group = pygame.sprite.Group()
# create player
spaceship = Spaceship(int(WIDTH / 2), HEIGHT - 100, 3)
spaceship_group.add(spaceship)




def create_aliens():
    for row in range(ROWS):
        for item in range(COLS):
            alien = Aliens(100 + item * 100, 100 + row * 70)
            alien_group.add(alien)

create_aliens()


pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption("Spave Invaders")
clock = pygame.time.Clock()



running = True   # IF RUNNING USING THE BUTTON NOT CONSOLE IT CRASHES WHY??????
while running:
    clock.tick(FPS)
 
    draw_bg()

    # create randomw alien bullets
    #record current time
    time_now = pygame.time.get_ticks()
    #shoot
    if time_now - last_alien_shot > alien_cooldown and len(alien_bullet_group) < 5 and len(alien_group) > 0:
        attacking_alien = random.choice(alien_group.sprites())
        alien_bullet = Alien_Bullets(attacking_alien.rect.centerx, attacking_alien.rect.bottom)
        alien_bullet_group.add(alien_bullet)
        last_alien_shot = time_now 
        

    # event handlers
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    
    spaceship.update()

    bullet_group.update()
    alien_group.update()
    alien_bullet_group.update()
    # draw sprite groups on screen
    spaceship_group.draw(screen)
    bullet_group.draw(screen)
    alien_group.draw(screen)
    alien_bullet_group.draw(screen)
    pygame.display.update() 



pygame.quit()