// file: poc-qt/poc001/src/bridge.rs

use core::pin::Pin;
use cxx_qt_lib::QString;

#[derive(Default)]
pub struct GreeterRust {
    name: QString,
    message: QString,
}

#[cxx_qt::bridge]
pub mod ffi {
    unsafe extern "C++" {
        include!("cxx-qt-lib/qstring.h");
        type QString = cxx_qt_lib::QString;
    }

    extern "RustQt" {
        #[qobject]
        #[qml_element]
        #[qproperty(QString, name)]
        #[qproperty(QString, message)]
        type Greeter = super::GreeterRust;

        #[qinvokable]
        #[cxx_name = "sayHello"]
        fn say_hello(self: Pin<&mut Greeter>);
    }

    impl cxx_qt::Initialize for Greeter {}
}

impl cxx_qt::Initialize for ffi::Greeter {
    fn initialize(self: Pin<&mut Self>) {
        self.set_message(QString::from("Hello"));
    }
}

impl ffi::Greeter {
    pub fn say_hello(self: Pin<&mut Self>) {
        let current_name = self.as_ref().name().to_string();
        let trimmed_name = current_name.trim();

        let next_message = if trimmed_name.is_empty() {
            QString::from("Hello")
        } else {
            QString::from(format!("Hello {}", trimmed_name))
        };

        self.set_message(next_message);
    }
}
