import pygame
import sprites
import setup

pygame.init()

SCREEN_WIDTH = 1800
SCREEN_HEIGHT = 1000

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()

player = pygame.sprite.GroupSingle()
player.add(sprites.Player())

streets = pygame.sprite.Group()
streets_list = setup.setup_streets()

lanterns = pygame.sprite.Group()
detectors = pygame.sprite.Group()
lantern_list, detectors_list = setup.setup_lanterns_detectors_new(SCREEN_WIDTH)
    
for item in lantern_list:
    lanterns.add(item)
for item in detectors_list:
    detectors.add(item)
for item in streets_list:
    streets.add(item)

if __name__ == "__main__":
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
        
        screen.fill("Grey")  
        detectors.draw(screen)
        streets.draw(screen)
        lanterns.draw(screen)
        player.draw(screen)
        player.update()

        for lantern in lanterns:
            lantern.reset_light()

        # Check for collision and light up corresponding lantern
        collisions = pygame.sprite.spritecollide(player.sprite, detectors, False)
        for detector in collisions:
            for lantern in lanterns:
                if detector.crossing_id == lantern.crossing_id and detector.crossing_id is not None:
                    lantern.light_up(255, 255, 0, 255)
                if lantern.street == detector.street:
                    if lantern.id == detector.id - 1 or lantern.id == detector.id + 1:
                        lantern.light_up(176, 179, 0, 255)
                    if lantern.id == detector.id - 2 or lantern.id == detector.id + 2:
                        lantern.light_up(176, 179, 0, 128)
                    if lantern.id == detector.id:
                        lantern.light_up(255, 255, 0, 255)
                

        if pygame.sprite.spritecollide(player.sprite, streets, False):
            player.sprite.set_back()


        pygame.display.update()
        clock.tick(60)

    pygame.quit()