/// RFC 31 验收 — Command system
/// 按 q dispatch Command::Quit，按 f dispatch Command::FocusNext

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

#[derive(Component)]
struct App {
    #[reactive(paint, copy)]
    count: i32,
}

impl App {
fn new() -> Self { Self { count: 0 } }
}

impl Component for App {
fn render(&self, cx: &mut RenderCx) {
        cx.line(format!("Count: {} (press + to increment)", self.get_count()));
        cx.line("");
        cx.line("q → dispatch Quit | f → dispatch FocusNext");
    }
fn event(&mut self, event: &Event, cx: &mut EventCx) {
        if cx.phase() != lv_tui::event::EventPhase::Target { return; }
        if event.is_key(Key::Char('q')) { cx.dispatch(Command::Quit); }
        if event.is_key(Key::Char('+')) { self.set_count(self.get_count() + 1, cx); }
    }
}

fn main() -> lv_tui::Result<()> {
    let body = Column::new().padding(1).gap(1)
        .child(Label::new("=== Command Demo ==="))
        .child(App::new());

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