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

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

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

impl Component for Counter {
fn render(&self, cx: &mut RenderCx) {
        cx.line(format!("count: {}", self.count()));
    }
fn event(&mut self, event: &Event, cx: &mut EventCx) {
        if cx.phase() != lv_tui::event::EventPhase::Target { return; }
        if event.is_key(Key::Char('+')) {
            self.update_count(cx, |n| *n += 1);
        }
        if event.is_key(Key::Char('q')) {
            cx.quit();
        }
    }
}

fn main() -> lv_tui::Result<()> {
    let inner = Column::new()
        .padding(1)
        .gap(0)
        .child(Label::new("  inner column"))
        .child(Label::new("  another label"))
        .child(Counter::new());

    App::new(
        Column::new()
            .padding(1)
            .gap(1)
            .child(Label::new("=== Nested Component Tree ==="))
            .child(inner)
            .child(Label::new("press + to increment, q to quit")),
    )
    .run()
}
