// SPDX-License-Identifier: MIT
//
// Generated by `rlvgl-creator qt emit --target rlvgl` from `tests/fixtures/qt/clickable.qml`.
// Do not edit by hand — regenerate with:
//   cargo run --features creator --bin rlvgl-creator -- \
//       qt emit --target rlvgl <input.qml> <out_dir>
//
// Emit-shape contract: docs/qt-support/03b-rlvgl-widget-mapping.md
// QT-03b notes:
//   * helper functions named per §8 (`build_<sanitized_id>` /
//     `build_node_<index>`) so reviewers have stable handles.
//   * bounds resolved per §7 trivial path; non-`fill`/`margins`
//     anchors are deferred to QT-03c.
//   * property lowering per §6; unsupported lower to TODOs.
// QT-04b notes (docs/qt-support/04b-properties-bindings.md):
//   * `pub struct ScreenState` carries every QML root-level
//     property declaration (see §3 / §5).
//   * `build_screen` returns `(WidgetNode, Rc<RefCell<ScreenState>>)`
//     and threads `state` through every helper.
//   * Handler bodies that match the §7 grammar lower to
//     `state.borrow_mut()...` mutations under a `// QT-04b body:`
//     marker; non-matching bodies fall through to `// QT-04 body:`.

#![allow(dead_code)]
#![allow(unused_imports)]
#![allow(unused_variables)]

extern crate alloc;

use alloc::rc::Rc;
use alloc::string::String;
use alloc::vec::Vec;
use core::cell::RefCell;

use rlvgl_core::WidgetNode;
use rlvgl_core::widget::{Color, Rect, Widget};
use rlvgl_widgets::button::Button;
use rlvgl_widgets::click_area::ClickArea;
use rlvgl_widgets::container::Container;
use rlvgl_widgets::label::Label;

/// rlvgl-target emit-shape version. Bumping is Specification-Required
/// (see `docs/qt-support/04b-properties-bindings.md` §11).
pub const QT_EMIT_VERSION: u32 = 13;

/// `qt-ir` schema version this module was generated from.
pub const QT_IR_VERSION: u32 = 2;

/// Source `.qml` file path as recorded at emit time.
pub const QT_SOURCE: &str = "tests/fixtures/qt/clickable.qml";

/// State threaded through every helper. One field per
/// QT-04b §5-supported property declared on the QML root.
/// `#[rustfmt::skip]` so the field order matches QT-01a's IR
/// declaration order rather than rustfmt's alphabetical sort.
#[rustfmt::skip]
#[derive(Debug, Clone)]
pub struct ScreenState {
    // QT-04b: root declares no QT-04b §5-supported properties.
}

/// Reactive Label-text binding emitted by QT-04e §3. Each
/// entry pairs a concrete `Rc<RefCell<Label>>` with an
/// accessor that reads the bound state field. Use
/// [`refresh_bindings`] to re-apply every binding in one call.
pub struct LabelBinding {
    pub label: Rc<RefCell<Label>>,
    pub accessor: fn(&ScreenState) -> String,
}

impl LabelBinding {
    /// Re-apply this binding from the supplied state.
    pub fn refresh(&self, state: &ScreenState) {
        self.label.borrow_mut().set_text((self.accessor)(state));
    }
}

/// Build the screen widget tree at `bounds` and return it
/// alongside the `ScreenState` handle (QT-04b) and the
/// `Vec<LabelBinding>` of reactive bindings (QT-04e §3).
/// Callers ignore the third element with a `_` if reactivity
/// is not needed.
#[rustfmt::skip]
pub fn build_screen(
    bounds: Rect,
) -> (WidgetNode, Rc<RefCell<ScreenState>>, Vec<LabelBinding>) {
    let state = Rc::new(RefCell::new(ScreenState {}));
    let mut label_bindings: Vec<LabelBinding> = Vec::new();
    let node = build_node_0(bounds, Rc::clone(&state), &mut label_bindings);
    (node, state, label_bindings)
}

/// Re-apply every QT-04e binding from the current state.
/// Idempotent; safe to call after every `state.borrow_mut()`
/// mutation. No-op when `bindings` is empty.
pub fn refresh_bindings(state: &Rc<RefCell<ScreenState>>, bindings: &[LabelBinding]) {
    let s = state.borrow();
    for b in bindings {
        b.refresh(&s);
    }
}

// QML type: `Button`
#[rustfmt::skip]
fn build_node_0(
    bounds: Rect,
    state: Rc<RefCell<ScreenState>>,
    label_bindings: &mut Vec<LabelBinding>,
) -> WidgetNode {
    let mut button = Button::new("Press me", bounds);
    // QT-04 body: count += 1
    button.set_on_click(|_b| {
        // TODO QT-04e: lower QML expression to Rust.
    });
    let widget: Rc<RefCell<dyn Widget>> = Rc::new(RefCell::new(button));
    let node = WidgetNode {
        widget,
        children: Vec::new(),
        tag: None,
    };
    node
}
