use crate::measure::ListItemMarker;
use super::{
    BulletShape, LayoutItem, LayoutList, LayoutListMarker, LayoutPage, ListLayoutItem,
    PageCustomProperties, ensure_block_fits, layout_block,
};

pub(super) fn layout_list_block(
    list: &crate::measure::MeasuredList,
    x: f32,
    y: &mut f32,
    available_width: f32,
    bottom_limit: f32,
    page_margins_top: f32,
    pages: &mut Vec<LayoutPage>,
) {
    ensure_block_fits(pages, y, list.height, page_margins_top, bottom_limit, false);

    let start_y = *y;
    let content_x = x + list.gap_size;
    let content_width = (available_width - list.gap_size).max(0.0);
    let mut layout_items = Vec::new();

    let bullet_shape = match list.marker_type.as_str() {
        "circle" => BulletShape::Circle,
        "square" => BulletShape::Square,
        _ => BulletShape::Disc,
    };

    for (i, item_blocks) in list.items.iter().enumerate() {
        let item_start_y = *y;
        let mut temp_pages = vec![LayoutPage { items: Vec::new(), background: None, watermark: None, header: None, footer: None, custom_properties: PageCustomProperties::default() }];
        let mut temp_y = item_start_y;

        for block in item_blocks {
            layout_block(block, content_x, &mut temp_y, content_width, bottom_limit, page_margins_top, &mut temp_pages);
        }

        let mut item_content = Vec::new();
        for page in &temp_pages {
            item_content.extend(page.items.clone());
        }

        let marker = match &list.item_markers[i] {
            ListItemMarker::Bullet { width: _w, height: _h } => {
                if list.marker_type == "none" {
                    None
                } else {
                    let font_size = list.font_size;
                    let size = font_size * 0.3;
                    let baseline_y = item_start_y + font_size;
                    let bullet_y = baseline_y - font_size * 0.4;
                    Some(LayoutListMarker::Bullet {
                        x: x + (list.gap_size - size) / 2.0,
                        y: bullet_y,
                        size,
                        shape: bullet_shape,
                        color: list.marker_color.clone(),
                    })
                }
            }
            ListItemMarker::Number { text, text_width: _tw, width: mw, height: _h } => {
                if text.is_empty() {
                    None
                } else {
                    let marker_x = x + list.gap_size - mw - 2.0;
                    Some(LayoutListMarker::Number {
                        text: text.clone(),
                        x: marker_x,
                        y: item_start_y,
                        font_size: list.font_size,
                        font_family: list.font_family.clone(),
                        color: list.marker_color.clone(),
                    })
                }
            }
        };

        layout_items.push(ListLayoutItem {
            content: item_content,
            marker,
        });

        *y = temp_y;
    }

    pages.last_mut().unwrap().items.push(LayoutItem::List(LayoutList {
        x,
        y: start_y,
        width: available_width,
        height: list.height,
        items: layout_items,
        marker_color: list.marker_color.clone(),
    }));
}
