use crate::layout::LayoutItem;

#[derive(Debug, Clone)]
pub(super) struct LinkAnnotation {
    pub(super) x: f32,
    pub(super) y: f32,
    pub(super) width: f32,
    pub(super) height: f32,
    pub(super) url: String,
    pub(super) destination: Option<String>,
}

pub(super) fn collect_links_from_items(items: &[LayoutItem], page_height: f32, links: &mut Vec<LinkAnnotation>) {
    for item in items {
        match item {
            LayoutItem::Text(text) => {
                for run in &text.runs {
                    if let Some(ref url) = run.style.link {
                        let x = text.x + run.x_offset;
                        let y = page_height - text.y - run.font_size;
                        links.push(LinkAnnotation {
                            x, y, width: run.width, height: run.font_size, url: url.clone(), destination: None,
                        });
                    }
                }
                if let Some(ref target_id) = text.page_reference {
                    let x = text.x;
                    let y = page_height - text.y - text.runs.first().map(|r| r.font_size).unwrap_or(12.0);
                    let height = text.line_height;
                    links.push(LinkAnnotation {
                        x, y, width: text.width, height, url: String::new(), destination: Some(target_id.clone()),
                    });
                }
            }
            LayoutItem::Table(table) => {
                for row in &table.rows {
                    for cell in &row.cells {
                        for run in &cell.runs {
                            if let Some(ref url) = run.style.link {
                                let y = page_height - run.y_offset - run.font_size;
                                links.push(LinkAnnotation {
                                    x: run.x_offset, y, width: run.width, height: run.font_size, url: url.clone(), destination: None,
                                });
                            }
                        }
                        collect_links_from_items(&cell.items, page_height, links);
                    }
                }
            }
            LayoutItem::Columns(cols) => {
                for col in &cols.columns {
                    collect_links_from_items(col, page_height, links);
                }
            }
            LayoutItem::List(list) => {
                for list_item in &list.items {
                    collect_links_from_items(&list_item.content, page_height, links);
                }
            }
            LayoutItem::Image(image) => {
                if let Some(ref url) = image.link {
                    let y = page_height - image.y - image.height;
                    links.push(LinkAnnotation {
                        x: image.x, y, width: image.width, height: image.height,
                        url: url.clone(), destination: None,
                    });
                }
                if let Some(page) = image.link_to_page {
                    let y = page_height - image.y - image.height;
                    links.push(LinkAnnotation {
                        x: image.x, y, width: image.width, height: image.height,
                        url: String::new(), destination: Some(format!("__page_{}", page)),
                    });
                }
                if let Some(ref dest) = image.link_to_destination {
                    let y = page_height - image.y - image.height;
                    links.push(LinkAnnotation {
                        x: image.x, y, width: image.width, height: image.height,
                        url: String::new(), destination: Some(dest.clone()),
                    });
                }
            }
            LayoutItem::Svg(svg) => {
                if let Some(ref url) = svg.link {
                    let y = page_height - svg.y - svg.height;
                    links.push(LinkAnnotation {
                        x: svg.x, y, width: svg.width, height: svg.height,
                        url: url.clone(), destination: None,
                    });
                }
                if let Some(page) = svg.link_to_page {
                    let y = page_height - svg.y - svg.height;
                    links.push(LinkAnnotation {
                        x: svg.x, y, width: svg.width, height: svg.height,
                        url: String::new(), destination: Some(format!("__page_{}", page)),
                    });
                }
                if let Some(ref dest) = svg.link_to_destination {
                    let y = page_height - svg.y - svg.height;
                    links.push(LinkAnnotation {
                        x: svg.x, y, width: svg.width, height: svg.height,
                        url: String::new(), destination: Some(dest.clone()),
                    });
                }
            }
            _ => {}
        }
    }
}
