use crate::theme;
use cvkg_core::{
    Never, Rect, Renderer, Size, View,
    layout::{LayoutCache, LayoutView, SizeProposal},
};

/// Memory cluster for semantic visualization
pub struct MemoryCluster {
    pub id: String,
    pub topic: String,
    pub strength: f32,
    pub connections: Vec<String>,
}

/// Semantic memory explorer for visualizing concept relationships
pub struct SemanticMemoryExplorer {
    pub(crate) clusters: Vec<MemoryCluster>,
    pub(crate) highlighted: Option<String>,
}

impl Default for SemanticMemoryExplorer {
    fn default() -> Self {
        Self::new()
    }
}

impl SemanticMemoryExplorer {
    pub fn new() -> Self {
        Self {
            clusters: Vec::new(),
            highlighted: None,
        }
    }

    pub fn cluster(mut self, id: &str, topic: &str, strength: f32) -> Self {
        self.clusters.push(MemoryCluster {
            id: id.to_string(),
            topic: topic.to_string(),
            strength,
            connections: Vec::new(),
        });
        self
    }

    pub fn connect(mut self, from: &str, to: &str) -> Self {
        if let Some(c) = self.clusters.iter_mut().find(|c| c.id == from) {
            c.connections.push(to.to_string());
        }
        self
    }
}

impl View for SemanticMemoryExplorer {
    type Body = Never;
    fn body(self) -> Self::Body {
        unreachable!()
    }

    fn render(&self, renderer: &mut dyn Renderer, rect: Rect) {
        // Title
        renderer.fill_rect(
            Rect {
                x: rect.x,
                y: rect.y,
                width: rect.width,
                height: 28.0,
            },
            theme::surface_elevated(),
        );
        renderer.draw_text(
            "Semantic Memory",
            rect.x + 8.0,
            rect.y + 8.0,
            14.0,
            theme::text(),
        );

        // Cluster visualization area
        let viz_rect = Rect {
            x: rect.x + 10.0,
            y: rect.y + 40.0,
            width: rect.width - 20.0,
            height: rect.height - 50.0,
        };

        // Draw connections (lines between related clusters)
        for (i, cluster) in self.clusters.iter().enumerate() {
            for target in &cluster.connections {
                if let Some(j) = self.clusters.iter().position(|c| &c.id == target) {
                    let x1 = viz_rect.x + 40.0 + i as f32 * 80.0;
                    let y1 = viz_rect.y + viz_rect.height / 2.0;
                    let x2 = viz_rect.x + 40.0 + j as f32 * 80.0;
                    let y2 = y1;
                    renderer.draw_line(
                        x1,
                        y1,
                        x2,
                        y2,
                        theme::with_alpha(theme::border(), 0.6),
                        1.5,
                    );
                }
            }
        }

        // Draw clusters
        for (i, cluster) in self.clusters.iter().enumerate() {
            let cx = viz_rect.x + 40.0 + i as f32 * 80.0;
            let cy = viz_rect.y + viz_rect.height / 2.0;
            let radius = 20.0 + cluster.strength * 15.0;

            let color = if self.highlighted.as_deref() == Some(&cluster.id) {
                theme::accent()
            } else {
                theme::info()
            };

            let cluster_rect = Rect {
                x: cx - radius,
                y: cy - radius,
                width: radius * 2.0,
                height: radius * 2.0,
            };
            renderer.fill_ellipse(cluster_rect, color);
            renderer.stroke_ellipse(cluster_rect, theme::with_alpha(theme::accent(), 0.8), 2.0);
            renderer.draw_text(
                &cluster.topic,
                cx - 20.0,
                cy + radius + 4.0,
                11.0,
                theme::text(),
            );
        }
    }
}

impl LayoutView for SemanticMemoryExplorer {
    fn size_that_fits(
        &self,
        _proposal: SizeProposal,
        _subviews: &[&dyn LayoutView],
        _cache: &mut LayoutCache,
    ) -> Size {
        Size {
            width: 400.0,
            height: 300.0,
        }
    }
    fn place_subviews(
        &self,
        _bounds: Rect,
        _subviews: &mut [&mut dyn LayoutView],
        _cache: &mut LayoutCache,
    ) {
    }
}
