/// RFC 34 验收 — Layout debug view

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

struct Root { body: Column, debug: bool }

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

impl Component for Root {
    fn render(&self, cx: &mut RenderCx) {
        if self.debug {
            cx.set_style(Style::default().fg(Color::Yellow).bold());
            cx.line("[DEBUG ON - press d to toggle]");
        }
        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 cx.phase() == EventPhase::Capture {
            if e.is_key(Key::Char('q')) { cx.quit(); }
            if e.is_key(Key::Char('d')) {
                EventCx::toggle_debug();
                self.debug = !self.debug;
                cx.invalidate_paint();
            }
        }
        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 body = Column::new().padding(1).gap(1)
        .child(Label::new("=== Debug View Demo ==="))
        .child(Label::new("Name:"))
        .child(Input::new().placeholder("enter name"))
        .child(Label::new("press d to toggle debug, q to quit"));

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