use glam::Vec3;

use crate::bundles::Bundle;
use crate::components::RotateComponent;
use crate::entity::EntityHandle;
use crate::loaders::lights::LightData;
use crate::world::{Transform, World};

pub struct SpotlightBundle
{
    pub light_data: LightData,
}

impl Bundle for SpotlightBundle
{
    fn spawn(self, world: &mut World) -> Result<EntityHandle, String>
    {
        let entity = world.spawn();
        let transform = Transform::from_matrix(self.light_data.transform);
        world.transforms.insert(entity, transform);
        world.spotlights.insert(entity, self.light_data.component);
        world.names.insert(entity, "Spotlight".to_string());
        if let Some(tag) = self.light_data.tag
        {
            if tag == "lighthouse"
            {
                world
                    .rotates
                    .insert(entity, RotateComponent::new(Vec3::Y, 1.0));
            }
        }
        Ok(entity)
    }
}

pub fn spawn_spotlights(world: &mut World, spotlights: Vec<LightData>)
{
    for (index, light_data) in spotlights.into_iter().enumerate()
    {
        let entity = world.spawn();
        let transform = Transform::from_matrix(light_data.transform);
        world.transforms.insert(entity, transform);
        world.spotlights.insert(entity, light_data.component);
        world.names.insert(entity, format!("Spotlight_{}", index));
        if let Some(tag) = light_data.tag
        {
            if tag == "lighthouse"
            {
                world
                    .rotates
                    .insert(entity, RotateComponent::new(Vec3::Y, 1.0));
            }
        }
    }
}
