/// lv-tui Demo — Settings Panel + Task List
/// Tab/Shift+Tab: switch focus  Space: toggle checkbox  Enter: submit input  q: quit

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

#[derive(Component)]
struct Settings {
    #[reactive(paint, copy)]
    notify: bool,
    #[reactive(paint, copy)]
    autosave: bool,
    #[reactive(paint, copy)]
    focused: bool,
}

impl Settings {
    fn new() -> Self { Self { notify: false, autosave: true, focused: false } }
}

impl Component for Settings {
    fn render(&self, cx: &mut RenderCx) {
        cx.set_style(Style::default().fg(Color::Cyan).bold());
        cx.line("Settings");
        cx.set_style(Style::default());
        cx.line(format!("  Notifications: {}", if self.get_notify() { "ON " } else { "OFF" }));
        cx.line(format!("  Auto-save:     {}", if self.get_autosave() { "ON " } else { "OFF" }));
    }
    fn event(&mut self, e: &Event, cx: &mut EventCx) {
        match e {
            Event::Focus => { self.set_focused(true, cx); }
            Event::Blur => { self.set_focused(false, cx); }
            _ => {}
        }
        if e.is_key(Key::Char('n')) && cx.phase() == EventPhase::Target { self.set_notify(!self.get_notify(), cx); }
        if e.is_key(Key::Char('s')) && cx.phase() == EventPhase::Target { self.set_autosave(!self.get_autosave(), cx); }
    }
}

#[derive(Component)]
struct Tasks {
    #[reactive(tree)]
    items: Vec<String>,
    #[reactive(paint, copy)]
    focused: bool,
}

impl Tasks {
    fn new() -> Self { Self { items: vec!["Buy groceries".into(), "Write report".into(), "Call dentist".into()], focused: false } }
}

impl Component for Tasks {
    fn render(&self, cx: &mut RenderCx) {
        cx.set_style(Style::default().fg(Color::Cyan).bold());
        cx.line("Tasks");
        cx.set_style(Style::default());
        for t in self.items() { cx.line(format!("  • {}", t)); }
    }
    fn event(&mut self, e: &Event, cx: &mut EventCx) {
        match e {
            Event::Focus => { self.set_focused(true, cx); }
            Event::Blur => { self.set_focused(false, cx); }
            _ => {}
        }
        if e.is_key(Key::Char('a')) && cx.phase() == EventPhase::Target { self.update_items(cx, |v| v.push("New task".into())); }
    }
}

struct App { left: Column, right: Column }

impl App {
    fn new() -> Self {
        let left = Column::new().gap(1).padding(1)
            .child(Label::new("SETTINGS").style(Style::default().fg(Color::Yellow).bold()))
            .child(Checkbox::new("Enable notifications"))
            .child(Checkbox::new("Auto-save").checked())
            .child(Label::new(""))
            .child(Label::new("THEME").style(Style::default().fg(Color::Yellow).bold()))
            .child(RadioGroup::new(vec![Text::from("Dark"), Text::from("Light")]))
            .child(Label::new(""))
            .child(Label::new("INPUT").style(Style::default().fg(Color::Yellow).bold()))
            .child(Input::new().placeholder("Type here..."));

        let right = Column::new().gap(1).padding(1)
            .child(Block::new(Tasks::new()).border(Border::Rounded).padding(1).title("Tasks").title_alignment(TextAlign::Center))
            .child(Block::new(Settings::new()).border(Border::Plain).padding(1).title("Shortcuts").title_alignment(TextAlign::Center));

        Self { left, right }
    }
}

impl Component for App {
    fn render(&self, cx: &mut RenderCx) {
        let mid = cx.rect.width / 2;
        let lr = Rect { x: cx.rect.x, y: cx.rect.y, width: mid, height: cx.rect.height };
        let rr = Rect { x: cx.rect.x + mid, y: cx.rect.y, width: cx.rect.width - mid, height: cx.rect.height };
        cx.buffer.draw_border(lr, Border::Plain, &Style::default().fg(Color::Gray));
        cx.buffer.draw_border(rr, Border::Plain, &Style::default().fg(Color::Gray));
        let saved = cx.rect;
        cx.rect = Rect { x: lr.x + 1, y: lr.y + 1, width: lr.width - 2, height: lr.height - 2 };
        self.left.render(cx);
        cx.rect = Rect { x: rr.x + 1, y: rr.y + 1, width: rr.width - 2, height: rr.height - 2 };
        self.right.render(cx);
        cx.rect = saved;

        cx.buffer.write_text(Pos { x: cx.rect.x, y: cx.rect.y + cx.rect.height - 1 }, cx.rect,
            "Tab: focus  Space: toggle  Enter: submit  q: quit", &Style::default().fg(Color::Gray));
    }
    fn event(&mut self, e: &Event, cx: &mut EventCx) {
        if e.is_key(Key::Char('q')) { cx.quit(); return; }
        self.left.event(e, cx);
        self.right.event(e, cx);
    }
    fn layout(&mut self, r: Rect, cx: &mut LayoutCx) {
        let mid = r.width / 2;
        self.left.layout(Rect { x: r.x + 1, y: r.y + 1, width: mid - 2, height: r.height - 3 }, cx);
        self.right.layout(Rect { x: r.x + mid + 1, y: r.y + 1, width: r.width - mid - 2, height: r.height - 3 }, cx);
    }
    fn measure(&self, c: Constraint, _cx: &mut lv_tui::component::MeasureCx) -> Size { Size { width: c.max.width, height: c.max.height } }
    fn focusable(&self) -> bool { false }
    fn for_each_child(&self, f: &mut dyn FnMut(&Node)) { self.left.for_each_child(f); self.right.for_each_child(f); }
    fn for_each_child_mut(&mut self, f: &mut dyn FnMut(&mut Node)) { self.left.for_each_child_mut(f); self.right.for_each_child_mut(f); }
}

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