import pygame
import random
from particle import Particle

pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Fireplace Simulation")
clock = pygame.time.Clock()

particles = []

# Define the fireplace area (a simple rectangle to simulate a brick fireplace)
fireplace_rect = pygame.Rect(300, 400, 200, 200)  # x, y, width, height

running = True
while running:
    dt = clock.tick() / 1000.0  # Delta time in seconds

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

    # Clear the screen.
    screen.fill((0, 0, 0))
    
    # Draw the fireplace background:
    # Fill with a dark brick color and draw a border to simulate the fireplace frame.
    # pygame.draw.rect(screen, (70, 20, 20), fireplace_rect)       # Fireplace interior
    # pygame.draw.rect(screen, (150, 50, 50), fireplace_rect, 5)     # Fireplace border

    # Determine a random fire origin along the bottom edge inside the fireplace.
    

    # Spawn fire particles from the fire origin.
    particles_to_spawn = (16000 - len(particles))

    

    for _ in range(particles_to_spawn):
        fire_origin = (
            random.uniform(0, 800),
            600
        )
        fire_particle = Particle(
            pos=(fire_origin[0] + random.uniform(-5, 5), fire_origin[1] + random.uniform(-2, 2)),
            vel=(random.uniform(-20, 20), random.uniform(-80, -120)),  # Upward motion
            lifetime=1.5,
            decay_rate=1,
            size=random.uniform(5, 8),
            size_decay=5,
            glow=True,
            glow_intensity=1.0,
            friction=0.05,
            gravity=-30,  # Negative gravity makes the particle rise
            fade=True,
            particle_type="fire"
        )
        particles.append(fire_particle)
    
        # smoke_particle = Particle(
        #     pos=(fire_origin[0] + random.uniform(-10, 10), fire_origin[1] + random.uniform(-2, 2)),
        #     vel=(random.uniform(-10, 10), random.uniform(-40, -60)),  # Slower upward motion
        #     lifetime=2.5,
        #     decay_rate=0.8,
        #     size=random.uniform(8, 12),
        #     size_decay=1,
        #     glow=False,
        #     friction=0.02,
        #     gravity=-10,
        #     fade=True,
        #     particle_type="smoke"
        # )
        # particles.append(smoke_particle)

    # Update and draw each particle.
    for particle in particles[:]:
        particle.update(dt, screen.get_rect())
        particle.draw(screen)
        if particle.is_dead():
            particles.remove(particle)
    
    pygame.display.flip()

pygame.quit()