use core::{fmt::Write, any::Any};

use defmt;

use crate::uart::AmlibUart;
use core::arch::asm;

#[defmt::global_logger]
struct DefmtLogger;

unsafe impl defmt::Logger for DefmtLogger {
    fn acquire() {
        // Sync methods left empty because we don't use any interrupts
    }

    unsafe fn flush() {
        // Sync methods left empty because we don't use any interrupts
    }

    unsafe fn release() {
        // Sync methods left empty because we don't use any interrupts
    }

    unsafe fn write(bytes: &[u8]) {
        static mut UART: Option<AmlibUart> = None;
        if UART.is_none() {
            UART = Some(AmlibUart::new(0x0200_0040));
        }


        let mut dev = UART.unwrap();
        //writeln!(dev, "a").unwrap();
        //writeln!(dev, "length: {}", bytes.len());
        for byte in bytes {
            while let Err(_) = dev.try_put_char(*byte) {}
        }
    }
}
