/// RFC 12 验收 example — Stack 层叠布局

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

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, rect: Rect, cx: &mut LayoutCx) {
        self.body.layout(rect, cx);
    }

    fn event(&mut self, event: &Event, cx: &mut EventCx) {
        if event.is_key(Key::Char('q')) {
            cx.quit();
        }
        if let Event::Key(key_event) = event {
            if key_event.modifiers.ctrl && key_event.key == Key::Char('c') {
                cx.quit();
            }
        }
        self.body.event(event, cx);
    }
}

fn main() -> lv_tui::Result<()> {
    let body = Column::new()
        .padding(1)
        .gap(1)
        .child(Label::new("=== Stack Layout Demo ==="))
        .child(
            Stack::new()
                .child(
                    Label::new("BACKGROUND TEXT BACKGROUND TEXT")
                        .style(Style::default().fg(Color::Gray)),
                )
                .child(
                    Label::new(">> FOREGROUND <<")
                        .style(Style::default().fg(Color::Green).bold()),
                ),
        )
        .child(Label::new("press q or Ctrl+C to quit"));

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