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

struct App { spinner: Spinner, progress: ProgressBar, count: u32 }

impl App {
    fn new() -> Self {
        Self { spinner: Spinner::new("Loading, please wait...").style(Style::default().fg(Color::Cyan)), progress: ProgressBar::new().width(30), count: 0 }
    }
}

impl Component for App {
    fn render(&self, cx: &mut RenderCx) {
        cx.line("Spinner + Progress Demo");
        cx.line("");
        self.spinner.render(cx);
        cx.line("");
        self.progress.render(cx);
        cx.line("");
        cx.line(format!("Tick count: {}", self.count));
        cx.line("");
        cx.line("q to quit");
    }

    fn event(&mut self, e: &Event, cx: &mut EventCx) {
        if e.is_key(Key::Char('q')) { cx.quit(); return; }
        if let Event::Tick = e {
            self.count += 1;
            self.progress.set_ratio((self.count % 101) as f64 / 100.0, cx);
        }
        self.spinner.event(e, cx);
    }

    fn layout(&mut self, r: lv_tui::geom::Rect, cx: &mut lv_tui::component::LayoutCx) { self.spinner.layout(r, 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()
}
