use super::components::{GrassConfig, GrassInteractor, GrassRegion, GrassSpecies};
use crate::ecs::GRASS_REGION;
use crate::ecs::world::World;
use freecs::Entity;
use nalgebra_glm::Vec3;

pub fn spawn_grass_region(world: &mut World, config: GrassConfig) -> Entity {
    let entity = world.spawn_entities(GRASS_REGION, 1)[0];
    let region = GrassRegion::new().with_config(config);
    world.set_grass_region(entity, region);
    entity
}

pub fn add_grass_species(world: &mut World, entity: Entity, species: GrassSpecies, weight: f32) {
    if let Some(region) = world.get_grass_region_mut(entity) {
        region.add_species(species, weight);
    }
}

pub fn update_grass_player_position(world: &mut World, entity: Entity, position: Vec3) {
    if let Some(region) = world.get_grass_region_mut(entity) {
        region.set_player_position(position);
    }
}

pub fn enable_grass(world: &mut World, entity: Entity, enabled: bool) {
    if let Some(region) = world.get_grass_region_mut(entity) {
        region.enabled = enabled;
    }
}

pub fn enable_grass_interactors(world: &mut World, entity: Entity, enabled: bool) {
    if let Some(region) = world.get_grass_region_mut(entity) {
        region.config.interactors_enabled = enabled;
    }
}

#[cfg(feature = "terrain")]
pub fn set_grass_terrain(
    world: &mut World,
    entity: Entity,
    terrain_config: crate::ecs::terrain::TerrainConfig,
) {
    if let Some(region) = world.get_grass_region_mut(entity) {
        region.set_terrain(terrain_config);
    }
}

pub fn attach_grass_interactor(world: &mut World, entity: Entity, radius: f32, strength: f32) {
    world.set_grass_interactor(entity, GrassInteractor::new(radius, strength));
}
