use crate::ui::theme::{self, pal};
use crate::ui::widgets::{lbl, mono};
use crate::workflow::model::Workflow;
use crate::workflow::review::relations;
use rune::{El, col, row, text};

pub fn view(wf: &Workflow, i: usize, right: El) -> El {
    let p = pal();
    let t = &wf.tasks[i];
    let (after, with, _) = relations(wf, t);
    let mut meta = format!("step {}", t.step);
    if !with.is_empty() {
        meta += &format!(" · ∥ {}", with.join(" "));
    }
    if !after.is_empty() {
        meta += &format!(" · after {}", after.join(" "));
    }
    let head = row()
        .gap(12.0)
        .center_y()
        .child(text(t.id.clone()).font(theme::DISPLAY).color(p.accent))
        .child(
            col()
                .child(
                    text(t.title.clone())
                        .font(theme::TITLE)
                        .color(p.strong)
                        .selectable(true),
                )
                .child(mono(meta, p.muted)),
        );
    col()
        .gap(12.0)
        .child(row().justify_between().center_y().child(head).child(right))
        .child(
            row()
                .gap(24.0)
                .items_start()
                .child(section("Decisions", &t.decisions, "◆", p.muted))
                .child(section("Acceptance", &t.acceptance, "☐", p.muted))
                .child(section("Evidence", &t.evidence, "⎘", p.muted)),
        )
}

fn section(title: &str, items: &[String], glyph: &str, color: rune::Color) -> El {
    let p = pal();
    col()
        .grow(1.0)
        .min_w(0.0)
        .gap(2.0)
        .selection_group(true)
        .child(lbl(&format!("{title} · {}", items.len())).mb(4.0))
        .children(items.iter().map(|x| {
            row().gap(6.0).child(mono(glyph, color)).child(
                text(x.clone())
                    .font(theme::BODY)
                    .color(p.fg)
                    .wrap()
                    .selectable(true)
                    .grow(1.0)
                    .min_w(0.0),
            )
        }))
}
