// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright 2024 René Ladan <rene0+codeberg@freedom.nl>

use dcf77_utils::DCF77Utils;

fn main() {
    let mut dcf77 = DCF77Utils::default();

    // The 'N' is there to indicate the end-of-minute marker
    const MSGS: [&str; 1] =
        ["0 0101.0010.1100.10 0 010 0 1 1110.000 1 1100.01 1 1001.00 010 0010.0 0010.0100 0N"];

    for msg in MSGS {
        for m in msg.chars() {
            match m {
                '0' => dcf77.set_current_bit(Some(false)),
                '1' => dcf77.set_current_bit(Some(true)),
                '_' => dcf77.set_current_bit(None),
                'N' => dcf77.force_new_minute(),
                _ => continue,
                // skip increasing the second counter, as this character is only syntactic sugar
            }
            if !dcf77.increase_second() {
                println!("Bad increase_second at second {}", dcf77.get_old_second());
            }
        }
        // cache because decode_time() clears this on a successful decode
        dcf77.decode_time(true, false);
        let mut rdt = dcf77.get_radio_datetime();
        println!(
            "Date/time (local)={:?}-{:?}-{:?} {:?}:{:?} {:?} {:#2x?}",
            rdt.get_year(),
            rdt.get_month(),
            rdt.get_day(),
            rdt.get_hour(),
            rdt.get_minute(),
            rdt.get_weekday(),
            rdt.get_dst()
        );
        rdt = rdt.get_utc().unwrap();
        println!(
            "Date/time (UTC)=  {:?}-{:?}-{:?} {:?}:{:?} {:?} {:#2x?}",
            rdt.get_year(),
            rdt.get_month(),
            rdt.get_day(),
            rdt.get_hour(),
            rdt.get_minute(),
            rdt.get_weekday(),
            rdt.get_dst()
        );
    }
}
