/// RFC 30 验收 — Async task
/// 按 f 启动后台任务（2秒后完成），完成后显示 result

use lv_tui::prelude::*;
use lv_tui::Component;

#[derive(Component)]
struct App {
    #[reactive(paint)]
    status: String,
}

impl App {
    fn new() -> Self { Self { status: "idle".into() } }
}

impl Component for App {


    fn render(&self, cx: &mut RenderCx) {
        cx.line("=== Async Task Demo ===");
        cx.line(format!("Status: {}", self.status()));
        cx.line("");
        cx.line("press f to spawn task, q to quit");
    }

    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.quit(); }
        if event.is_key(Key::Char('f')) {
            self.set_status("running...".into(), cx);
            cx.spawn(|| {
                std::thread::sleep(std::time::Duration::from_secs(2));
                "task completed!".to_string()
            });
        }
        if let Event::TaskComplete(data) = event {
            self.set_status(data.clone(), cx);
        }
    }
}

fn main() -> lv_tui::Result<()> { lv_tui::app::App::new(App::new()).run() }
