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

struct App { content: Column, dark: bool }

impl App {
    fn new() -> Self {
        Self { dark: true, content: Column::new()
            .child(Label::new("Theme Demo").style(Style::default().bold()))
            .child(Label::new(""))
            .child(Label::new("Red text").style(Style::default().fg(Color::Red)))
            .child(Label::new("Green text").style(Style::default().fg(Color::Green)))
            .child(Label::new("Blue text").style(Style::default().fg(Color::Blue)))
            .child(Label::new("Yellow text").style(Style::default().fg(Color::Yellow)))
            .child(Label::new(""))
            .child(Label::new(""))
            .child(Label::new(""))  // placeholder for theme indicator
            .child(Label::new(""))
            .child(Label::new("t: toggle theme  q: quit")
                .style(Style::default().fg(Color::Gray))),
        }
    }
}

impl Component for App {
    fn render(&self, cx: &mut RenderCx) {
        self.content.render(cx);
        // Render theme indicator directly — more visible than a Label
        let r = cx.rect;
        let y = r.y.saturating_add(8);
        let label = if self.dark { "  [ Dark theme ]  " } else { "  [ Light theme ]  " };
        let style = if self.dark {
            Style::default().bg(Color::Black).fg(Color::White).bold()
        } else {
            Style::default().bg(Color::White).fg(Color::Black).bold()
        };
        cx.buffer.write_text(
            lv_tui::geom::Pos { x: r.x, y }, r, label, &style,
        );
    }
    fn event(&mut self, e: &Event, cx: &mut EventCx) {
        if e.is_key(Key::Char('q')) { cx.quit(); return; }
        if e.is_key(Key::Char('t')) {
            self.dark = !self.dark;
            set_theme(if self.dark { Theme::Dark } else { Theme::Light });
            cx.invalidate_paint();
            return;
        }
        self.content.event(e, cx);
    }
    fn layout(&mut self, r: lv_tui::geom::Rect, cx: &mut lv_tui::component::LayoutCx) { self.content.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()
}
