/// RFC 28 验收 — stylesheet 使 #title 变绿、.sub 变黄

use lv_tui::prelude::*;
use lv_tui::style_parser::StyleSheet;
use lv_tui::widgets::{Column, Label};

struct Root { body: Column }

impl Root {
    fn new(body: Column) -> Self { Self { body } }
}

impl Component for Root {
    fn render(&self, cx: &mut RenderCx) { self.body.render(cx); }
    fn layout(&mut self, r: Rect, cx: &mut LayoutCx) { self.body.layout(r, cx); }
    fn focusable(&self) -> bool { false }
    fn event(&mut self, e: &Event, cx: &mut EventCx) {
        if e.is_key(Key::Char('q')) { cx.quit(); }
        self.body.event(e, cx);
    }
    fn for_each_child(&self, f: &mut dyn FnMut(&Node)) { self.body.for_each_child(f); }
    fn for_each_child_mut(&mut self, f: &mut dyn FnMut(&mut Node)) { self.body.for_each_child_mut(f); }
}

fn main() -> lv_tui::Result<()> {
    let css = "#title { fg: green; bold: true; } .sub { fg: yellow; }";
    // Box::leak gives 'static lifetime
    let sheet: &'static StyleSheet = Box::leak(Box::new(StyleSheet::parse(css).unwrap()));

    // 用样式表解析为每个 Label 手动设样式
    let resolve = |id: Option<&str>, class: Option<&str>| -> Style {
        sheet.resolve("Label", id, class, &lv_tui::style_parser::WidgetState::default())
    };

    let body = Column::new().padding(1).gap(1)
        .child(Label::new("#title: green bold").id("title").style(resolve(Some("title"), None)))
        .child(Label::new(".sub: yellow").class("sub").style(resolve(None, Some("sub"))))
        .child(Label::new("default").style(resolve(None, None)))
        .child(Label::new("press q to quit"));

    lv_tui::app::App::new(Root::new(body)).run()
}
