use crate::render::Spotlight;
use crate::world::World;

pub fn spotlight_sync_system(world: &World) -> Vec<Spotlight>
{
    let mut entities = world.spotlights.all();
    entities.sort();

    let mut spotlights = Vec::new();

    for entity in entities
    {
        if let Some(spotlight_component) = world.spotlights.get(entity)
        {
            if let Some(transform) = world.transforms.get(entity)
            {
                let position = transform.position + spotlight_component.offset;
                let direction = transform.rotation * spotlight_component.direction;

                spotlights.push(Spotlight::new(
                    position,
                    direction,
                    spotlight_component.inner_angle,
                    spotlight_component.outer_angle,
                    spotlight_component.range,
                ));
            }
        }
    }

    spotlights
}
