/// RFC 32 验收 — Modal/Overlay
/// 按 o 弹出对话框，按 Esc 关闭，按 q 退出

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

struct Root {
    overlay: Overlay,
}

impl Root {
    fn new() -> Self {
        let bg = Column::new().padding(1).gap(1)
            .child(Label::new("=== Overlay Demo ==="))
            .child(Label::new("press o to open overlay"))
            .child(Label::new("press q to quit"));

        let fg = Column::new().padding(1).gap(1)
            .child(Label::new("** MODAL DIALOG **").style(Style::default().bold()))
            .child(Label::new("This is a modal overlay."))
            .child(Label::new("Press Esc to close."));

        Self { overlay: Overlay::new(bg, fg) }
    }
}

impl Component for Root {
    fn render(&self, cx: &mut RenderCx) { self.overlay.render(cx); }
    fn layout(&mut self, r: Rect, cx: &mut LayoutCx) { self.overlay.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(); }
        if e.is_key(Key::Char('o')) { self.overlay.show(); cx.invalidate_paint(); }
        self.overlay.event(e, cx);
    }
    fn for_each_child(&self, f: &mut dyn FnMut(&Node)) { self.overlay.for_each_child(f); }
    fn for_each_child_mut(&mut self, f: &mut dyn FnMut(&mut Node)) { self.overlay.for_each_child_mut(f); }
}

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