use pumpkin_data::BlockDirection;
use pumpkin_util::{
    math::position::BlockPos,
    random::{RandomGenerator, RandomImpl},
};

use crate::generation::proto_chunk::GenerationCache;
use crate::{generation::block_state_provider::BlockStateProvider, world::WorldPortalExt};

pub struct AttachedToLogsTreeDecorator {
    pub probability: f32,
    pub block_provider: BlockStateProvider,
    pub directions: Vec<BlockDirection>,
}

impl AttachedToLogsTreeDecorator {
    pub fn generate<T: GenerationCache>(
        &self,
        chunk: &mut T,
        block_registry: &dyn WorldPortalExt,
        random: &mut RandomGenerator,
        log_positions: &[BlockPos],
    ) {
        // TODO: shuffle
        for pos in log_positions {
            // TODO: random
            let pos = pos.offset(self.directions[0].to_offset());
            if random.next_f32() > self.probability
                || !GenerationCache::get_block_state(chunk, &pos.0)
                    .to_state()
                    .is_air()
            {
                continue;
            }
            chunk.set_block_state(
                &pos.0,
                self.block_provider.get(random, pos, chunk, block_registry),
            );
        }
    }
}
