use crate::{generation::proto_chunk::GenerationCache, world::WorldPortalExt};
use pumpkin_data::tag;
use pumpkin_util::{
    math::{position::BlockPos, vector3::Vector3},
    random::{RandomGenerator, RandomImpl},
};

use super::CoralFeature;

pub struct CoralMushroomFeature;

impl CoralMushroomFeature {
    #[allow(clippy::too_many_arguments)]
    pub fn generate<T: GenerationCache>(
        chunk: &mut T,
        block_registry: &dyn WorldPortalExt,
        _min_y: i8,
        _height: u16,
        _feature: pumpkin_data::placed_feature::PlacedFeature, // This placed feature
        random: &mut RandomGenerator,
        pos: BlockPos,
    ) -> bool {
        // First lets get a random coral
        let block = CoralFeature::get_random_tag_entry(tag::Block::MINECRAFT_CORAL_BLOCKS, random);

        let i = random.next_bounded_i32(3) + 3;
        let j = random.next_bounded_i32(3) + 3;
        let k = random.next_bounded_i32(3) + 3;
        let l = random.next_bounded_i32(3) + 1;

        for m in 0..=j {
            for n in 0..=i {
                for o in 0..=k {
                    let mut pos = pos;
                    pos = pos.offset(Vector3::new(pos.0.x + m, pos.0.y + n, pos.0.z + o));
                    pos = pos.down_height(l);

                    let condition_a = (m != 0 && m != j) || (n != 0 && n != i);
                    let condition_b = (o != 0 && o != k) || (n != 0 && n != i);
                    let condition_c = (m != 0 && m != j) || (o != 0 && o != k);
                    let condition_d = m == 0 || m == j || n == 0 || n == i || o == 0 || o == k;
                    let random_check = random.next_f32() < 0.1f32;

                    if !((condition_a && condition_b && condition_c && condition_d)
                        && !random_check
                        && CoralFeature::generate_coral_piece(
                            chunk,
                            block_registry,
                            random,
                            block,
                            pos,
                        ))
                    {}
                }
            }
        }
        true
    }
}
