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

struct App { char_section: Column, word_section: Column }

impl App {
    fn new() -> Self {
        Self {
            char_section: Column::new().gap(1)
                .child(Label::new("Char wrap:").style(Style::default().bold()))
                .child(Label::new("This long line wraps at every character when terminal is narrow.")),
            word_section: Column::new().gap(1)
                .child(Label::new("Word wrap:").style(Style::default().bold()))
                .child(Label::new("This long line wraps at word boundaries when terminal is narrow.")),
        }
    }
}

impl Component for App {
    fn render(&self, cx: &mut RenderCx) {
        cx.wrap = TextWrap::Char;
        self.char_section.render(cx);
        cx.wrap = TextWrap::Word;
        self.word_section.render(cx);
        cx.line("q to quit");
    }
    fn event(&mut self, e: &Event, cx: &mut EventCx) {
        if e.is_key(Key::Char('q')) { cx.quit(); }
    }
    fn layout(&mut self, r: lv_tui::geom::Rect, cx: &mut lv_tui::component::LayoutCx) {
        let half = r.height / 2;
        self.char_section.layout(Rect { x: r.x, y: r.y, width: r.width, height: half }, cx);
        self.word_section.layout(Rect { x: r.x, y: r.y + half, width: r.width, height: r.height - half }, cx);
    }
    fn measure(&self, c: lv_tui::layout::Constraint, _cx: &mut lv_tui::component::MeasureCx) -> lv_tui::geom::Size {
        lv_tui::geom::Size { width: c.max.width, height: c.max.height }
    }
    fn focusable(&self) -> bool { false }
}

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