use chrono::Datelike;
use i_slint_core::api::Global;
use log::debug;
use crate::{GreeterData, GreeterDisplay};
use crate::config::Config;

pub fn register_timers(
    _display: &GreeterDisplay,
    data: &GreeterData,
    _config: &Config,
) -> slint::Timer {
    debug!(target: "ckgreeter::timers", "Registering timers...");
    let data_weak = data.as_weak();

    let timer = slint::Timer::default();
    timer.start(
        slint::TimerMode::Repeated,
        std::time::Duration::from_secs(1),
        {
            let data_weak = data_weak.clone();
            move || {
                let data = data_weak.unwrap();
                let now = chrono::Local::now();

                data.set_current_time(now.format("%H:%M:%S").to_string().into());
                data.set_current_date(
                    format!("{} {}", now.weekday(), now.format("%d/%m/%Y")).into(),
                );
            }
        },
    );

    debug!(target: "ckgreeter::timers", "Clock timer started");

    timer
}