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

#[derive(Component)]
struct Counter {
    #[reactive(paint, copy)]
    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.get_count()));
        cx.line("press + to increment, q/Ctrl+C to quit");
    }

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

fn main() -> lv_tui::Result<()> {
    App::new(
        Tabs::new()
            .tab("计数器", Counter::new())
            .tab("输入", Column::new()
                .child(Label::new("请输入文字:"))
                .child(Input::new().placeholder("在这里输入...")))
            .tab("关于", Label::new("lv-tui Tabs 示例 v0.3"))
            .header_style(Style::default().bold())
            .selected_style(Style::default().bg(Color::White).fg(Color::Black)),
    ).run()
}
