use super::*;

pub(super) fn material_list(
    world: &mut World,
    ui: &mut egui::Ui,
    rows: &[MaterialLibraryRow],
    selected: &Option<AssetSelection>,
    cache: &ThumbnailCacheSnapshot,
    selected_entities: &mut SelectedEntities,
) {
    for row in rows {
        let document_status = row.asset.path.as_deref().and_then(|path| {
            world
                .get_resource::<crate::asset_documents::AuthoredAssetDocumentStore>()
                .and_then(|store| store.snapshot_for_path(path))
        });
        let status = AssetCardStatus::for_document(
            document_status.as_ref(),
            row.dirty,
            row.error.is_some() || cache.is_failed(&row.asset),
        );
        let is_selected = selected.as_ref() == Some(&row.selection);
        let (rect, response) = ui.allocate_exact_size(
            egui::vec2(ui.available_width(), 42.0),
            egui::Sense::click_and_drag(),
        );
        ui.painter().rect(
            rect,
            3.0,
            if is_selected {
                crate::ui::theme::SELECTION_BG_MUTED
            } else if response.hovered() {
                ELEVATED_BG
            } else {
                WIDGET_BG
            },
            egui::Stroke::new(1.0_f32, if is_selected { SELECTION } else { BORDER }),
            egui::StrokeKind::Inside,
        );
        let thumb_rect = egui::Rect::from_min_size(
            rect.left_top() + egui::vec2(5.0, 5.0),
            egui::vec2(32.0, 32.0),
        );
        if let Some(texture) = cache.texture_for(&row.asset) {
            ui.painter().image(
                texture,
                thumb_rect,
                egui::Rect::from_min_max(egui::Pos2::ZERO, egui::pos2(1.0, 1.0)),
                egui::Color32::WHITE,
            );
        } else {
            ui.painter().text(
                thumb_rect.center(),
                egui::Align2::CENTER_CENTER,
                icons::PALETTE.as_str(),
                egui::FontId::new(20.0, egui::FontFamily::Name("phosphor-regular".into())),
                if row.error.is_some() { ERROR } else { TEXT },
            );
        }
        let metadata_width = if rect.width() >= 500.0 { 190.0 } else { 0.0 };
        let text_rect = egui::Rect::from_min_max(
            egui::pos2(rect.left() + 44.0, rect.top() + 4.0),
            egui::pos2(rect.right() - metadata_width - 24.0, rect.bottom() - 4.0),
        );
        let text_painter = ui.painter().with_clip_rect(text_rect);
        text_painter.text(
            egui::pos2(text_rect.left(), rect.center().y - 7.0),
            egui::Align2::LEFT_CENTER,
            row.asset.label.as_str(),
            egui::FontId::proportional(13.0),
            TEXT,
        );
        text_painter.text(
            egui::pos2(text_rect.left(), rect.center().y + 9.0),
            egui::Align2::LEFT_CENTER,
            row.asset.path.as_deref().unwrap_or(""),
            egui::FontId::monospace(10.0),
            TEXT_MUTED,
        );
        if metadata_width > 0.0 {
            let metadata_rect = egui::Rect::from_min_max(
                egui::pos2(rect.right() - metadata_width - 18.0, rect.top()),
                egui::pos2(rect.right() - 18.0, rect.bottom()),
            );
            ui.painter().with_clip_rect(metadata_rect).text(
                metadata_rect.right_center(),
                egui::Align2::RIGHT_CENTER,
                format!(
                    "{}  ·  {} scene  ·  {} instances",
                    row.kind.label(),
                    row.scene_users.len(),
                    row.dependent_instances
                ),
                egui::FontId::proportional(11.0),
                if row.error.is_some() { ERROR } else { TEXT_DIM },
            );
        }
        draw_asset_status_rail(ui, rect, status);
        handle_material_response(world, response, row, selected_entities);
    }
}
