use pumpkin_data::BlockState;
use pumpkin_util::{
    math::{int_provider::IntProvider, position::BlockPos},
    random::{RandomGenerator, RandomImpl},
};

use super::{FoliagePlacer, LeaveValidator};
use crate::generation::feature::features::tree::TreeNode;
use crate::generation::proto_chunk::GenerationCache;

pub struct SpruceFoliagePlacer {
    pub trunk_height: IntProvider,
}

impl SpruceFoliagePlacer {
    #[expect(clippy::too_many_arguments)]
    pub fn generate<T: GenerationCache>(
        &self,
        chunk: &mut T,
        random: &mut RandomGenerator,
        node: &TreeNode,
        foliage_height: i32,
        iradius: i32,
        offset: i32,
        foliage_provider: &BlockState,
    ) -> Vec<BlockPos> {
        let mut foliage_positions = Vec::new();
        let mut radius = random.next_bounded_i32(2);
        let mut max = 1;
        let mut next = 0;
        for y in ((-foliage_height)..=offset).rev() {
            FoliagePlacer::generate_square(
                &mut foliage_positions,
                self,
                chunk,
                random,
                node.center,
                radius,
                y,
                node.giant_trunk,
                foliage_provider,
            );
            if radius >= max {
                radius = next;
                next = 1;
                max = (iradius + node.foliage_radius).min(max + 1);
                continue;
            }
            radius += 1;
        }
        foliage_positions
    }
    pub fn get_random_height(&self, random: &mut RandomGenerator, trunk_height: i32) -> i32 {
        (trunk_height - self.trunk_height.get(random)).max(4)
    }
}

impl LeaveValidator for SpruceFoliagePlacer {
    fn is_invalid_for_leaves(
        &self,
        _random: &mut pumpkin_util::random::RandomGenerator,
        dx: i32,
        _y: i32,
        dz: i32,
        radius: i32,
        _giant_trunk: bool,
    ) -> bool {
        dx == radius && dz == radius && radius > 0
    }
}
