use crate::components::lights::spot::SpotlightComponent;
use crate::render::Spotlight;
use crate::utility::transform::Transform;
use crate::world::Storage;

pub fn spotlight_sync_system(
    spotlights: &Storage<SpotlightComponent>,
    transforms: &Storage<Transform>,
) -> Vec<Spotlight>
{
    let mut entities = spotlights.all();
    entities.sort();

    let mut result = Vec::new();

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

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

    result
}
